Completed
Push — master ( a9a224...08e3df )
by wen
08:25
created

Handler::render()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 19
rs 8.8571
c 1
b 0
f 1
ccs 0
cts 9
cp 0
cc 5
eloc 10
nc 5
nop 2
crap 30
1
<?php
2
3
namespace Sco\Admin\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
11
class Handler implements ExceptionHandlerContract
12
{
13
    protected $parentHandler;
14
15
    public function __construct(ExceptionHandlerContract $parentHandler)
16
    {
17
        $this->parentHandler = $parentHandler;
18
    }
19
20
    /**
21
     * Report or log an exception.
22
     *
23
     * @param  \Exception $exception
24
     *
25
     * @return void
26
     * @throws \Exception
27
     */
28
    public function report(Exception $exception)
29
    {
30
        $this->parentHandler->report($exception);
31
    }
32
33
    /**
34
     * Render an exception into a response.
35
     *
36
     * @param  \Illuminate\Http\Request $request
37
     * @param  \Exception               $exception
38
     *
39
     * @return \Symfony\Component\HttpFoundation\Response
40
     */
41
    public function render($request, Exception $exception)
42
    {
43
        if ($exception instanceof AuthenticationException) {
44
            if ($request->expectsJson()) {
45
                return response('Unauthenticated.', 401);
46
            }
47
            return redirect()->guest(route('admin.login'));
48
        }
49
50
        if ($exception instanceof ModelNotFoundException) {
51
            return response($exception->getMessage(), 404);
52
        }
53
54
        if ($exception instanceof HttpException) {
55
            return response($exception->getMessage(), $exception->getStatusCode());
56
        }
57
58
        return $this->parentHandler->render($request, $exception);
59
    }
60
61
    /**
62
     * Render an exception to the console.
63
     *
64
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
65
     * @param  \Exception                                        $exception
66
     *
67
     * @return void
68
     */
69
    public function renderForConsole($output, Exception $exception)
70
    {
71
        $this->parentHandler->renderForConsole($output, $exception);
72
    }
73
}
74