Handler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 40
dl 0
loc 117
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A whoopsHandler() 0 9 1
B render() 0 34 7
A report() 0 6 3
A unauthenticated() 0 7 2
1
<?php
2
3
namespace App\Exceptions;
4
5
use Backpack\CRUD\Exception\AccessDeniedException;
6
use Bugsnag;
7
use Exception;
8
use Illuminate\Auth\AuthenticationException;
9
use Illuminate\Filesystem\Filesystem;
10
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11
use Illuminate\Session\TokenMismatchException;
12
use Illuminate\Support\Arr;
13
use Whoops\Handler\PrettyPageHandler;
14
15
class Handler extends ExceptionHandler
16
{
17
    /**
18
     * A list of the exception types that should not be reported.
19
     *
20
     * @var array
21
     */
22
    protected $dontReport = [
23
        \Illuminate\Auth\AuthenticationException::class,
24
        \Illuminate\Auth\Access\AuthorizationException::class,
25
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
26
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
27
        \Illuminate\Session\TokenMismatchException::class,
28
        \Illuminate\Validation\ValidationException::class,
29
    ];
30
31
    /**
32
     * A list of the inputs that are never flashed for validation exceptions.
33
     *
34
     * @var array
35
     */
36
    protected $dontFlash = [
37
        'password',
38
        'password_confirmation',
39
    ];
40
41
    /**
42
     * Report or log an exception.
43
     *
44
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
45
     *
46
     * @param  \Exception $exception
47
     * @return void
48
     * @throws Exception
49
     */
50
    public function report(Exception $exception)
51
    {
52
        if (class_exists(Bugsnag::class) && method_exists(Bugsnag::class, 'notify_exception')) {
53
            Bugsnag::notifyException($exception);
54
        }
55
        parent::report($exception);
56
    }
57
58
    /**
59
     * Render an exception into an HTTP response.
60
     *
61
     * @param  \Illuminate\Http\Request $request
62
     * @param  \Exception               $exception
63
     *
64
     * @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
65
     */
66
    public function render($request, Exception $exception)
67
    {
68
        /*
69
         * Redirect if token mismatch error
70
         * Usually because user stayed on the same screen too long and their session expired
71
         */
72
        if ($exception instanceof TokenMismatchException) {
73
            return redirect()->route('frontend.auth.login');
74
        }
75
        if ($exception instanceof \Illuminate\Auth\Access\AuthorizationException ||
76
            $exception instanceof AccessDeniedException) {
77
            //todo: do something different for logged in user without sufficient credentials and not-logged-in user
78
            if (request()->project) {
79
                return redirect()
80
                    ->route('frontend.crud.projects.show', [request()->project])
81
                    ->withFlashDanger(trans('backpack::crud.unauthorized_access'));
82
            }
83
            if (request()->project_id) {
84
                return redirect()
85
                    ->route('frontend.crud.projects.show', [request()->project_id])
86
                    ->withFlashDanger(trans('backpack::crud.unauthorized_access'));
87
            }
88
89
            return redirect()->route(homeRoute())->withFlashDanger(trans('backpack::crud.unauthorized_access'));
90
        }
91
92
        /*
93
         * All instances of GeneralException redirect back with a flash message to show a bootstrap alert-error
94
         */
95
        if ($exception instanceof GeneralException) {
96
            return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
97
        }
98
99
        return parent::render($request, $exception);
100
    }
101
102
    /**
103
     * Convert an authentication exception into an unauthenticated response.
104
     *
105
     * @param  \Illuminate\Http\Request                 $request
106
     * @param  \Illuminate\Auth\AuthenticationException $exception
107
     *
108
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
109
     */
110
    protected function unauthenticated($request, AuthenticationException $exception)
111
    {
112
        if ($request->expectsJson()) {
113
            return response()->json(['error' => 'Unauthenticated.'], 401);
114
        }
115
116
        return redirect()->guest(route('frontend.auth.login'));
117
    }
118
119
    /**
120
     * @return mixed|\Whoops\Handler\Handler
121
     * @throws \InvalidArgumentException
122
     */
123
    protected function whoopsHandler()
124
    {
125
        return tap(new PrettyPageHandler,
126
            function (PrettyPageHandler $handler) {
127
                $files = new Filesystem;
128
                $handler->setEditor('phpstorm');
129
                $handler->handleUnconditionally(true);
130
                $handler->setApplicationPaths(array_flip(Arr::except(array_flip($files->directories(base_path())),
131
                        [base_path('vendor')])));
132
            });
133
    }
134
}
135