Handler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 108
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sendEmail() 0 10 2
A unauthenticated() 0 7 2
A render() 0 19 6
A report() 0 13 4
1
<?php
2
3
namespace App\Exceptions;
4
5
use App\Mail\ExceptionOccured;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
use Illuminate\Support\Facades\Log;
9
use Mail;
10
use Response;
11
use Symfony\Component\Debug\Exception\FlattenException;
12
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
13
use Throwable;
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
        //
24
    ];
25
26
    /**
27
     * A list of the inputs that are never flashed for validation exceptions.
28
     *
29
     * @var array
30
     */
31
    protected $dontFlash = [
32
        'password',
33
        'password_confirmation',
34
    ];
35
36
    /**
37
     * Report or log an exception.
38
     *
39
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
40
     *
41
     * @param \Throwable $exception
42
     *
43
     * @return void
44
     */
45
    public function report(Throwable $exception)
46
    {
47
        $enableEmailExceptions = config('exceptions.emailExceptionEnabled');
48
49
        if ($enableEmailExceptions === '') {
50
            $enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
51
        }
52
53
        if ($enableEmailExceptions && $this->shouldReport($exception)) {
54
            $this->sendEmail($exception);
55
        }
56
57
        parent::report($exception);
58
    }
59
60
    /**
61
     * Render an exception into an HTTP response.
62
     *
63
     * @param \Illuminate\Http\Request $request
64
     * @param \Throwable               $exception
65
     *
66
     * @throws \Throwable
67
     */
68
    public function render($request, Throwable $exception)
69
    {
70
        $userLevelCheck = $exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\RoleDeniedException ||
71
            $exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\RoleDeniedException ||
72
            $exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\PermissionDeniedException ||
73
            $exception instanceof \jeremykenedy\LaravelRoles\App\Exceptions\LevelDeniedException;
74
75
        if ($userLevelCheck) {
76
            if ($request->expectsJson()) {
77
                return Response::json([
78
                    'error'     => 403,
79
                    'message'   => 'Unauthorized.',
80
                ], 403);
81
            }
82
83
            abort(403);
84
        }
85
86
        return parent::render($request, $exception);
87
    }
88
89
    /**
90
     * Convert an authentication exception into an unauthenticated response.
91
     *
92
     * @param \Illuminate\Http\Request                 $request
93
     * @param \Illuminate\Auth\AuthenticationException $exception
94
     *
95
     * @return \Illuminate\Http\Response
96
     */
97
    protected function unauthenticated($request, AuthenticationException $exception)
98
    {
99
        if ($request->expectsJson()) {
100
            return response()->json(['error' => 'Unauthenticated.'], 401);
101
        }
102
103
        return redirect()->guest(route('login'));
104
    }
105
106
    /**
107
     * Sends an email upon exception.
108
     *
109
     * @param \Throwable $exception
110
     *
111
     * @return void
112
     */
113
    public function sendEmail(Throwable $exception)
114
    {
115
        try {
116
            $e = FlattenException::create($exception);
117
            $handler = new SymfonyExceptionHandler();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Debug\ExceptionHandler has been deprecated: since Symfony 4.4, use Symfony\Component\ErrorHandler\ErrorHandler instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

117
            $handler = /** @scrutinizer ignore-deprecated */ new SymfonyExceptionHandler();
Loading history...
118
            $html = $handler->getHtml($e);
119
120
            Mail::send(new ExceptionOccured($html));
121
        } catch (Throwable $exception) {
122
            Log::error($exception);
123
        }
124
    }
125
}
126