Passed
Push — dependabot/npm_and_yarn/string... ( b56eb5...bc569b )
by
unknown
45:46 queued 33s
created

Handler::render()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 5
nc 3
nop 2
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Auth\Access\AuthorizationException;
8
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9
use Illuminate\Validation\ValidationException;
10
11
class Handler extends ExceptionHandler
12
{
13
    /**
14
     * A list of the exception types that should not be reported.
15
     *
16
     * @var array
17
     */
18
    protected $dontReport = [
19
        //
20
    ];
21
22
    /**
23
     * A list of the inputs that are never flashed for validation exceptions.
24
     *
25
     * @var array
26
     */
27
    protected $dontFlash = [
28
        'password',
29
        'password_confirmation',
30
    ];
31
32
    /**
33
     * Report or log an exception.
34
     *
35
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
36
     *
37
     * @param  \Exception  $exception
38
     * @return void
39
     */
40
    public function report(Exception $exception)
41
    {
42
        parent::report($exception);
43
    }
44
45
    /**
46
     * Render an exception into an HTTP response.
47
     *
48
     * @param  \Illuminate\Http\Request  $request
49
     * @param  \Exception  $exception
50
     * @return \Illuminate\Http\Response
51
     */
52
    public function render($request, Exception $exception)
53
    {
54
        if ($exception instanceof AuthorizationException) {
55
            return $this->unauthorized($request, $exception);
56
        }
57
58
        //could use some code cleanup
59
        if ((strpos(url()->previous(), 'admin') || strpos(url()->current(), 'admin')) && !$exception instanceof AuthenticationException && !$exception instanceof ValidationException) {
60
            return $this->renderChiefException($request, $exception);
61
        }
62
63
        return parent::render($request, $exception);
64
    }
65
66
    protected function renderChiefException($request, Exception $exception)
67
    {
68
        if (!config('app.debug')) {
69
            if ($request->expectsJson()) {
70
                return response()->json(['error' => 'Something went wrong.'], 404);
71
            }
72
73
            return response()->view('chief::back.errors.custom');
74
        }
75
        
76
        return parent::render($request, $exception);
77
    }
78
79
80
    protected function unauthorized($request, AuthorizationException $exception)
81
    {
82
        return redirect()->route('chief.back.dashboard')
83
                         ->with('messages.error', 'Oeps. Het lijkt erop dat je geen toegang hebt tot dit deel van chief. Vraag even de beheerder voor meer info.');
84
    }
85
86
    /**
87
     * Convert an authentication exception into an unauthenticated response.
88
     *
89
     * @param  \Illuminate\Http\Request  $request
90
     * @param  \Illuminate\Auth\AuthenticationException  $exception
91
     * @return \Illuminate\Http\Response
92
     */
93
    protected function unauthenticated($request, AuthenticationException $exception)
94
    {
95
        if ($request->expectsJson()) {
96
            return response()->json(['error' => 'Unauthenticated.'], 401);
97
        }
98
99
        if (!empty($exception->guards()) && array_first($exception->guards()) == 'chief') {
100
            return redirect()->guest(route('chief.back.login'));
101
        }
102
103
        return $request->expectsJson()
104
            ? response()->json(['message' => $exception->getMessage()], 401)
105
            : redirect()->guest($exception->redirectTo() ?? '/');
106
    }
107
}
108