Passed
Push — master ( 295fd2...d0507a )
by Andrea Marco
02:54 queued 13s
created

JsonApiErrorServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 36
ccs 11
cts 12
cp 0.9167
rs 10
c 2
b 0
f 0
wmc 4

1 Method

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