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

64 lines
1.7 KiB
PHP
Raw Normal View History

2025-06-29 08:50:03 +02:00
<?php
namespace App\Providers;
2025-08-05 21:15:17 +02:00
use App\Enums\LogLevelEnum;
use App\Events\ActionPerformed;
2025-06-29 18:33:18 +02:00
use App\Events\ExceptionOccurred;
use App\Events\NewArticleFetched;
use App\Events\RouteArticleApproved;
use App\Listeners\LogActionListener;
2025-06-29 18:33:18 +02:00
use App\Listeners\LogExceptionToDatabase;
use App\Listeners\PublishApprovedArticleListener;
use App\Listeners\ValidateArticleListener;
2025-07-05 18:26:04 +02:00
use Error;
2025-06-29 18:33:18 +02:00
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-07-05 18:26:04 +02:00
use InvalidArgumentException;
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 {}
2025-06-29 08:50:03 +02:00
public function boot(): void
{
Event::listen(
ActionPerformed::class,
LogActionListener::class,
);
2025-06-29 18:33:18 +02:00
Event::listen(
ExceptionOccurred::class,
LogExceptionToDatabase::class,
);
2025-08-03 20:59:09 +02:00
Event::listen(
NewArticleFetched::class,
ValidateArticleListener::class,
2025-08-03 20:59:09 +02:00
);
Event::listen(
RouteArticleApproved::class,
PublishApprovedArticleListener::class,
);
2025-08-03 20:59:09 +02:00
2025-06-29 18:33:18 +02:00
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) {
2025-07-05 18:26:04 +02:00
$exception instanceof Error => LogLevelEnum::CRITICAL,
$exception instanceof InvalidArgumentException => LogLevelEnum::WARNING,
2025-06-29 18:33:18 +02:00
default => LogLevelEnum::ERROR,
};
2025-06-29 08:50:03 +02:00
}
}