Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.76% |
24 / 29 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AppServiceProvider | |
82.76% |
24 / 29 |
|
33.33% |
1 / 3 |
6.18 | |
0.00% |
0 / 1 |
| register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| boot | |
92.00% |
23 / 25 |
|
0.00% |
0 / 1 |
1.00 | |||
| mapExceptionToLogLevel | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Providers; |
| 4 | |
| 5 | use App\Events\ExceptionOccurred; |
| 6 | use App\Listeners\LogExceptionToDatabase; |
| 7 | use App\LogLevelEnum; |
| 8 | use Error; |
| 9 | use Illuminate\Contracts\Debug\ExceptionHandler; |
| 10 | use Illuminate\Support\Facades\Event; |
| 11 | use Illuminate\Support\ServiceProvider; |
| 12 | use InvalidArgumentException; |
| 13 | use Throwable; |
| 14 | |
| 15 | class AppServiceProvider extends ServiceProvider |
| 16 | { |
| 17 | public function register(): void |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | public function boot(): void |
| 22 | { |
| 23 | Event::listen( |
| 24 | ExceptionOccurred::class, |
| 25 | LogExceptionToDatabase::class, |
| 26 | ); |
| 27 | |
| 28 | Event::listen( |
| 29 | \App\Events\NewArticleFetched::class, |
| 30 | \App\Listeners\ValidateArticleListener::class, |
| 31 | ); |
| 32 | |
| 33 | Event::listen( |
| 34 | \App\Events\ArticleApproved::class, |
| 35 | \App\Listeners\PublishApprovedArticle::class, |
| 36 | ); |
| 37 | |
| 38 | Event::listen( |
| 39 | \App\Events\ArticleReadyToPublish::class, |
| 40 | \App\Listeners\PublishArticle::class, |
| 41 | ); |
| 42 | |
| 43 | Event::listen( |
| 44 | \App\Events\ExceptionLogged::class, |
| 45 | \App\Listeners\LogExceptionToDatabase::class, |
| 46 | ); |
| 47 | |
| 48 | app()->make(ExceptionHandler::class) |
| 49 | ->reportable(function (Throwable $e) { |
| 50 | $level = $this->mapExceptionToLogLevel($e); |
| 51 | |
| 52 | ExceptionOccurred::dispatch($e, $level, $e->getMessage(), []); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | private function mapExceptionToLogLevel(Throwable $exception): LogLevelEnum |
| 57 | { |
| 58 | return match (true) { |
| 59 | $exception instanceof Error => LogLevelEnum::CRITICAL, |
| 60 | $exception instanceof InvalidArgumentException => LogLevelEnum::WARNING, |
| 61 | default => LogLevelEnum::ERROR, |
| 62 | }; |
| 63 | } |
| 64 | } |