fedi-feed-router/app/Providers/AppServiceProvider.php

59 lines
1.5 KiB
PHP
Raw Normal View History

2025-06-29 08:50:03 +02:00
<?php
namespace App\Providers;
2025-06-29 09:48:45 +02:00
use App\Events\ArticleFetched;
2025-06-29 09:53:45 +02:00
use App\Events\ArticleReadyToPublish;
2025-06-29 18:33:18 +02:00
use App\Events\ExceptionOccurred;
2025-06-29 09:48:45 +02:00
use App\Listeners\CheckArticleKeywords;
2025-06-29 18:33:18 +02:00
use App\Listeners\LogExceptionToDatabase;
2025-06-29 09:53:45 +02:00
use App\Listeners\PublishArticle;
2025-06-29 18:33:18 +02:00
use App\LogLevelEnum;
use Illuminate\Contracts\Debug\ExceptionHandler;
2025-06-29 09:48:45 +02:00
use Illuminate\Support\Facades\Event;
2025-06-29 08:50:03 +02:00
use Illuminate\Support\ServiceProvider;
2025-06-29 18:33:18 +02:00
use Throwable;
2025-06-29 08:50:03 +02:00
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
//
}
public function boot(): void
{
2025-06-29 09:48:45 +02:00
Event::listen(
ArticleFetched::class,
CheckArticleKeywords::class,
);
2025-06-29 18:33:18 +02:00
2025-06-29 09:53:45 +02:00
Event::listen(
ArticleReadyToPublish::class,
PublishArticle::class,
);
2025-06-29 18:33:18 +02:00
Event::listen(
ExceptionOccurred::class,
LogExceptionToDatabase::class,
);
app()->make(ExceptionHandler::class)
->reportable(function (Throwable $e) {
$level = $this->mapExceptionToLogLevel($e);
ExceptionOccurred::dispatch($e, $level, $e->getMessage(), []);
});
}
private function mapExceptionToLogLevel(Throwable $exception): LogLevelEnum
{
return match (true) {
$exception instanceof \Error => LogLevelEnum::CRITICAL,
$exception instanceof \RuntimeException => LogLevelEnum::ERROR,
$exception instanceof \InvalidArgumentException => LogLevelEnum::WARNING,
default => LogLevelEnum::ERROR,
};
2025-06-29 08:50:03 +02:00
}
}