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

ServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 65%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 39
ccs 13
cts 20
cp 0.65
rs 10
c 2
b 0
f 0
wmc 4
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 21 3
A register() 0 8 1
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