Completed
Push — master ( 15c9e2...d1079a )
by wen
03:11
created

Handler   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 23
lcom 1
cbo 7
dl 0
loc 136
rs 10
c 2
b 1
f 1
ccs 0
cts 22
cp 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A report() 0 4 1
B render() 0 18 5
A renderForConsole() 0 4 1
A prepareException() 0 10 4
A unauthenticated() 0 10 3
A renderHttpException() 0 10 4
A isAdmin() 0 4 2
A renderAdminException() 0 6 2
1
<?php
2
3
namespace Sco\Admin\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\Access\AuthorizationException;
7
use Illuminate\Auth\AuthenticationException;
8
use Illuminate\Contracts\Debug\ExceptionHandler as ExceptionHandlerContract;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13
class Handler implements ExceptionHandlerContract
14
{
15
    protected $parentHandler;
16
17
    public function __construct(ExceptionHandlerContract $parentHandler)
18
    {
19
        $this->parentHandler = $parentHandler;
20
    }
21
22
    /**
23
     * Report or log an exception.
24
     *
25
     * @param  \Exception $exception
26
     *
27
     * @return void
28
     * @throws \Exception
29
     */
30
    public function report(Exception $exception)
31
    {
32
        $this->parentHandler->report($exception);
33
    }
34
35
    /**
36
     * Render an exception into a response.
37
     *
38
     * @param  \Illuminate\Http\Request $request
39
     * @param  \Exception               $exception
40
     *
41
     * @return \Symfony\Component\HttpFoundation\Response
42
     */
43
    public function render($request, Exception $exception)
44
    {
45
        $exception = $this->prepareException($exception);
46
47
        if ($exception instanceof AuthenticationException) {
48
            $response = $this->unauthenticated($request, $exception);
49
        } elseif ($exception instanceof HttpException) {
50
            $response = $this->renderHttpException($request, $exception);
51
        } elseif ($exception instanceof AdminException) {
52
            $response = $this->renderAdminException($request, $exception);
53
        }
54
55
        if (isset($response)) {
56
            return $response;
57
        }
58
59
        return $this->parentHandler->render($request, $exception);
60
    }
61
62
    /**
63
     * Render an exception to the console.
64
     *
65
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
66
     * @param  \Exception                                        $exception
67
     *
68
     * @return void
69
     */
70
    public function renderForConsole($output, Exception $exception)
71
    {
72
        $this->parentHandler->renderForConsole($output, $exception);
73
    }
74
75
    /**
76
     * Prepare exception for rendering.
77
     *
78
     * @param  \Exception $exception
79
     *
80
     * @return \Exception
81
     */
82
    protected function prepareException(Exception $exception)
83
    {
84
        if ($exception instanceof ModelNotFoundException) {
85
            $exception = new NotFoundHttpException($exception->getMessage(), $exception);
86
        } elseif ($exception instanceof AuthorizationException) {
87
            $exception = new HttpException(403, $exception->getMessage() ?: 'Forbidden');
88
        }
89
90
        return $exception;
91
    }
92
93
    /**
94
     * Convert an authentication exception into an unauthenticated response.
95
     *
96
     * @param  \Illuminate\Http\Request                 $request
97
     * @param  \Illuminate\Auth\AuthenticationException $exception
98
     *
99
     * @return \Illuminate\Http\Response
100
     */
101
    protected function unauthenticated($request, AuthenticationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        if ($request->expectsJson()) {
104
            return response('Unauthenticated.', 401);
105
        }
106
107
        if ($this->isAdmin($request)) {
108
            return redirect()->guest(route('admin.login'));
109
        }
110
    }
111
112
    /**
113
     * Render the given HttpException.
114
     *
115
     * @param  \Illuminate\Http\Request                              $request
116
     * @param  \Symfony\Component\HttpKernel\Exception\HttpException $exception
117
     *
118
     * @return \Symfony\Component\HttpFoundation\Response
119
     */
120
    protected function renderHttpException($request, HttpException $exception)
121
    {
122
        $status = $exception->getStatusCode();
123
        if ($request->expectsJson()) {
124
            return response($exception->getMessage() ?: 'Not Found', $status);
125
        }
126
        if ($this->isAdmin($request)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
127
            //return response()->view('admin::app', ['exception' => $exception], $status, $exception->getHeaders());
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
        }
129
    }
130
131
    protected function isAdmin($request)
132
    {
133
        return $request->route() && strpos($request->route()->getPrefix(), 'admin') === 0;
134
    }
135
136
    /**
137
     * @param \Illuminate\Http\Request             $request
138
     * @param \Sco\Admin\Exceptions\AdminException $exception
139
     *
140
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
141
     */
142
    protected function renderAdminException($request, AdminException $exception)
143
    {
144
        if ($request->expectsJson()) {
145
            return response($exception->getMessage(), 500);
146
        }
147
    }
148
}
149