Passed
Push — master ( e63f1d...295fd2 )
by Andrea Marco
03:11 queued 14s
created

JsonApiErrorServiceProvider::boot()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\JsonApiError\Providers;
6
7
use Cerbero\JsonApiError\JsonApiError;
8
use Cerbero\JsonApiError\Services\TestResponseMixin;
9
use Illuminate\Contracts\Debug\ExceptionHandler;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\ServiceProvider;
12
use Illuminate\Testing\TestResponse;
13
use Throwable;
14
15
/**
16
 * The package service provider.
17
 */
18
final class JsonApiErrorServiceProvider extends ServiceProvider
19
{
20
    /**
21
     * The name of the package.
22
     */
23
    public const NAME = 'json-api-error';
24
25
    /**
26
     * The path to the translation files.
27
     */
28
    public const PATH_LANG = __DIR__ . '/../../lang';
29
30
    /**
31
     * Bootstrap the package services.
32
     *
33
     * @param \Illuminate\Foundation\Exceptions\Handler $handler
34
     */
35 12
    public function boot(ExceptionHandler $handler): void
36
    {
37 12
        $handler->renderable(function (Throwable $e, Request $request) {
38 11
            return $request->expectsJson() ? JsonApiError::from($e)->response() : null;
39 12
        });
40
41 12
        $this->loadTranslationsFrom(self::PATH_LANG, self::NAME);
42
43 12
        $this->publishes([self::PATH_LANG => $this->app->langPath('vendor/json-api-error')], self::NAME);
44
45 12
        TestResponse::mixin(new TestResponseMixin());
46
    }
47
}
48