JsonApiErrorServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 39
ccs 12
cts 13
cp 0.9231
rs 10
c 4
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A boot() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\JsonApiError\Providers;
6
7
use Cerbero\JsonApiError\Contracts\JsonApiErrorsAware;
8
use Cerbero\JsonApiError\Contracts\JsonApiSafe;
9
use Cerbero\JsonApiError\Exceptions\JsonApiException;
10
use Cerbero\JsonApiError\JsonApiError;
11
use Cerbero\JsonApiError\Services\TestResponseMixin;
12
use Illuminate\Contracts\Debug\ExceptionHandler;
13
use Illuminate\Contracts\Http\Kernel;
14
use Illuminate\Http\Request;
15
use Illuminate\Support\ServiceProvider;
16
use Illuminate\Testing\TestResponse;
17
use Throwable;
18
19
/**
20
 * The package service provider.
21
 */
22
final class JsonApiErrorServiceProvider extends ServiceProvider
23
{
24
    /**
25
     * The name of the package.
26
     */
27
    public const NAME = 'json-api-error';
28
29
    /**
30
     * The path to the translation files.
31
     */
32
    public const PATH_LANG = __DIR__ . '/../../lang';
33
34
    /**
35
     * Bootstrap the package services.
36
     *
37
     * @param \Illuminate\Foundation\Exceptions\Handler $handler
38
     * @param \Illuminate\Foundation\Http\Kernel $kernel
39
     */
40 12
    public function boot(ExceptionHandler $handler, Kernel $kernel): void
41
    {
42 12
        $handler->renderable(function (Throwable $e, Request $request): ?JsonApiError {
43 11
            return JsonApiError::handlesRequest($request) ? JsonApiError::from($e) : null;
44 12
        });
45
46 12
        $handler->reportable(function (JsonApiException|JsonApiErrorsAware|JsonApiSafe $e) use ($handler): bool {
47 1
            if ($e instanceof Throwable && $previous = $e->getPrevious()) {
48
                $handler->report($previous);
49
            }
50
51 1
            return false;
52 12
        });
53
54 12
        $kernel->prependMiddlewareToGroup('api', JsonApiError::$middleware);
55
56 12
        $this->loadTranslationsFrom(self::PATH_LANG, self::NAME);
57
58 12
        $this->publishes([self::PATH_LANG => $this->app->langPath('vendor/json-api-error')], self::NAME);
59
60 12
        TestResponse::mixin(new TestResponseMixin());
61
    }
62
}
63