Handler   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 36
c 3
b 1
f 0
dl 0
loc 103
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A unauthenticated() 0 7 2
A sendEmail() 0 10 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 Exception;
7
use Illuminate\Auth\AuthenticationException;
8
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9
use Illuminate\Support\Facades\Log;
10
use Mail;
11
use Response;
12
use Symfony\Component\Debug\Exception\FlattenException;
13
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
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
     * Report or log an exception.
33
     *
34
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
35
     *
36
     * @param \Exception $exception
37
     *
38
     * @return void
39
     */
40
    public function report(Exception $exception)
41
    {
42
        $enableEmailExceptions = config('exceptions.emailExceptionEnabled');
43
44
        if ($enableEmailExceptions === '') {
45
            $enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault');
46
        }
47
48
        if ($enableEmailExceptions && $this->shouldReport($exception)) {
49
            $this->sendEmail($exception);
50
        }
51
52
        parent::report($exception);
53
    }
54
55
    /**
56
     * Render an exception into an HTTP response.
57
     *
58
     * @param \Illuminate\Http\Request $request
59
     * @param \Exception               $exception
60
     *
61
     * @return \Illuminate\Http\Response
62
     */
63
    public function render($request, Exception $exception)
64
    {
65
        $userLevelCheck = $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException ||
66
            $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\RoleDeniedException ||
67
            $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\PermissionDeniedException ||
68
            $exception instanceof \jeremykenedy\LaravelRoles\Exceptions\LevelDeniedException;
69
70
        if ($userLevelCheck) {
71
            if ($request->expectsJson()) {
72
                return Response::json([
0 ignored issues
show
Bug Best Practice introduced by
The expression return Response::json(ar... 'Unauthorized.'), 403) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Bug introduced by
array('error' => 403, 'm...ge' => 'Unauthorized.') of type array<string,integer|string> is incompatible with the type string expected by parameter $| of Illuminate\Support\Facades\Response::json(). ( Ignorable by Annotation )

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

72
                return Response::json(/** @scrutinizer ignore-type */ [
Loading history...
73
                    'error'     => 403,
74
                    'message'   => 'Unauthorized.',
75
                ], 403);
76
            }
77
78
            abort(403);
79
        }
80
81
        return parent::render($request, $exception);
82
    }
83
84
    /**
85
     * Convert an authentication exception into an unauthenticated response.
86
     *
87
     * @param \Illuminate\Http\Request                 $request
88
     * @param \Illuminate\Auth\AuthenticationException $exception
89
     *
90
     * @return \Illuminate\Http\Response
91
     */
92
    protected function unauthenticated($request, AuthenticationException $exception)
93
    {
94
        if ($request->expectsJson()) {
95
            return response()->json(['error' => 'Unauthenticated.'], 401);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...nauthenticated.'), 401) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
96
        }
97
98
        return redirect()->guest(route('login'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->guest(route('login')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
99
    }
100
101
    /**
102
     * Sends an email upon exception.
103
     *
104
     * @param \Exception $exception
105
     *
106
     * @return void
107
     */
108
    public function sendEmail(Exception $exception)
109
    {
110
        try {
111
            $e = FlattenException::create($exception);
112
            $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

112
            $handler = /** @scrutinizer ignore-deprecated */ new SymfonyExceptionHandler();
Loading history...
113
            $html = $handler->getHtml($e);
114
115
            Mail::send(new ExceptionOccured($html));
116
        } catch (Exception $exception) {
117
            Log::error($exception);
118
        }
119
    }
120
}
121