Passed
Pull Request — stable (#145)
by Nuno
02:33
created

ServiceProvider::boot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.125

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
nc 2
nop 1
dl 0
loc 21
ccs 7
cts 14
cp 0.5
crap 4.125
rs 9.3142
c 2
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework\Providers\ErrorHandler;
4
5
use Throwable;
6
use NunoMaduro\Collision\Provider;
7
use NunoMaduro\Collision\Adapters\Laravel\Inspector;
8
use Symfony\Component\Console\Exception\ExceptionInterface;
9
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
10
use LaravelZero\Framework\Contracts\Application as ApplicationContract;
11
use LaravelZero\Framework\Contracts\Providers\ErrorHandler as ErrorHandlerContract;
12
13
/**
14
 * This is the Laravel Zero Framework error handler service provider class.
15
 *
16
 * @author Nuno Maduro <[email protected]>
17
 */
18
class ServiceProvider extends BaseServiceProvider
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 51
    public function boot(ErrorHandlerContract $errorHandler): void
24
    {
25 51
        if ($this->app->environment() === 'production') {
26
            $this->app->make(ApplicationContract::class)->setCatchExceptions(true);
27
        } else {
28 51
            $errorHandler->register();
29 51
            set_exception_handler(
30 51
                function (Throwable $e) use ($errorHandler) {
31
                    $handler = $this->app->make(ErrorHandlerContract::class)->getProvider()->getHandler();
32
                    if ($e instanceof ExceptionInterface) {
33
                        $this->app->make(ApplicationContract::class)->renderException(
34
                            $e, $handler->getWriter()->getOutput()
35
                        );
36
                    } else {
37
                        $handler->setInspector(new Inspector($e));
38
                        $handler->handle();
39
                    }
40 51
                }
41
            );
42
        }
43 51
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 51
    public function register(): void
49
    {
50 51
        $this->app->singleton(
51 51
            ErrorHandlerContract::class, function () {
52 51
                return new ErrorHandler;
53 51
            }
54
        );
55 51
    }
56
}
57