Passed
Push — dev ( 160f79...6c8965 )
by Chris
08:45 queued 12s
created

Handler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
eloc 57
c 2
b 0
f 1
dl 0
loc 143
ccs 18
cts 22
cp 0.8182
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A context() 0 12 2
A render() 0 11 3
A report() 0 15 2
A unauthenticated() 0 11 3
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException as AuthenticationException;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
9
use Facades\App\Services\WhichPortal;
10
use Illuminate\Session\TokenMismatchException;
11
use Illuminate\Support\Facades\Lang;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Request;
14
use Illuminate\Support\Facades\Auth;
15
16
class Handler extends ExceptionHandler
17
{
18
    /**
19
     * A list of the exception types that are not reported.
20
     *
21
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Exceptions\Handler::$dontReport does not specify type hint for its items.
Loading history...
22
     */
23
    protected $dontReport = [];
24
25
    /**
26
     * A list of the inputs that are never flashed for validation exceptions.
27
     *
28
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Exceptions\Handler::$dontFlash does not specify type hint for its items.
Loading history...
29
     */
30
    protected $dontFlash = [
31
        'password',
32
        'password_confirmation',
33
        'current_password',
34
        'new_password',
35
        'new_password_confirmation',
36
    ];
37
38
    /**
39
     * OVERRIDE
40
     * A list of the internal exception types that should not be reported.
41 11
     *
42
     * @var array
0 ignored issues
show
introduced by
@var annotation of property \App\Exceptions\Handler::$internalDontReport does not specify type hint for its items.
Loading history...
43 11
     */
44 11
    protected $internalDontReport = [
45
        AuthenticationException::class,
46
        AuthorizationException::class,
47
        HttpException::class,
0 ignored issues
show
Bug introduced by
The type App\Exceptions\HttpException was not found. Did you mean HttpException? If so, make sure to prefix the type with \.
Loading history...
48
        HttpResponseException::class,
0 ignored issues
show
Bug introduced by
The type App\Exceptions\HttpResponseException was not found. Did you mean HttpResponseException? If so, make sure to prefix the type with \.
Loading history...
49
        ModelNotFoundException::class,
50
        SuspiciousOperationException::class,
51
        // TokenMismatchException::class,
52
        ValidationException::class,
53 11
    ];
54
55 11
    /**
56
     * Report or log an exception.
57
     *
58 11
     * @param  \Exception  $exception
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
59
     * @return void
60
     */
61
    public function report(Exception $exception)
0 ignored issues
show
introduced by
Method \App\Exceptions\Handler::report() does not have return type hint for its return value but it should be possible to add it based on @return annotation "void".
Loading history...
62
    {
63
        if ($exception instanceof TokenMismatchException) {
64
            $logData = [
65
                'requestToken' => request()->header('x-csrf-token'),
66
                'sessionToken' => session()->token(),
67
                'session' => session()->all(),
68 1
                'user' => request()->user(),
69
                'requestUrl' => request()->url()
70 1
            ];
71
            $message = '419 CSRF Token Mismatch. ' . collect($logData)->toJson();
72
            Log::debug($message);
73 1
        }
74
75
        parent::report($exception);
76 1
    }
77
78 1
    /**
79
     * OVERRIDE
80
     * Get the default context variables for logging.
81
     *
82
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \App\Exceptions\Handler::context() does not specify type hint for items of its traversable return value.
Loading history...
83
     */
84
    protected function context()
0 ignored issues
show
introduced by
Method \App\Exceptions\Handler::context() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
85
    {
86
        try {
87
            return array_filter([
88 2
                'userId' => Auth::id(),
89
                // 'email' => optional(Auth::user())->email,
90 2
                'url' => Request::path(),
91 2
                'method' => Request::method(),
92 2
                'referer' => Request::header('referer', '')
93 2
            ]);
94 2
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by
The type App\Exceptions\Throwable was not found. Did you mean Throwable? If so, make sure to prefix the type with \.
Loading history...
95
            return [];
96
        }
97
    }
98 2
99
    /**
100
     * Render an exception into an HTTP response.
101
     *
102
     * @param  \Illuminate\Http\Request  $request
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
103
     * @param  \Exception  $exception
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 15 spaces after parameter type; 2 found
Loading history...
104
     * @return \Illuminate\Http\Response
105
     */
106
    public function render($request, Exception $exception)
1 ignored issue
show
introduced by
Method \App\Exceptions\Handler::render() does not have parameter type hint for its parameter $request but it should be possible to add it based on @param annotation "\Illuminate\Http\Request".
Loading history...
introduced by
Method \App\Exceptions\Handler::render() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
Coding Style introduced by
Type hint "\Illuminate\Http\Request" missing for $request
Loading history...
107
    {
108
        if ($exception instanceof AdminException) {
109
            return $exception->render($request);
110
        }
111
        if ($exception instanceof TokenMismatchException) {
112
            $newMessage = $exception->getMessage() . ' ' . Lang::get('errors.refresh_page');
113
            $modifiedException = new TokenMismatchException($newMessage, $exception->getCode(), $exception);
114
            return parent::render($request, $modifiedException);
115
        }
116
        return parent::render($request, $exception);
117
    }
118
119
    /**
120
     * Convert an authentication exception into an unauthenticated response.
121
     *
122
     * @param  \Illuminate\Http\Request  $request
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter type; 2 found
Loading history...
123
     * @param  \Illuminate\Auth\AuthenticationException  $exception
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
124
     * @return \Illuminate\Http\Response
125
     */
126
    protected function unauthenticated($request, AuthenticationException $exception)
1 ignored issue
show
introduced by
Method \App\Exceptions\Handler::unauthenticated() does not have parameter type hint for its parameter $request but it should be possible to add it based on @param annotation "\Illuminate\Http\Request".
Loading history...
introduced by
Method \App\Exceptions\Handler::unauthenticated() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
Coding Style introduced by
Type hint "\Illuminate\Http\Request" missing for $request
Loading history...
127
    {
128
        if ($request->expectsJson()) {
129
            return response()->json(['error' => 'Unauthenticated.'], 401);
130
        }
131
        if (WhichPortal::isManagerPortal()) {
132
            $loginRoute = route('manager.login');
133
        } else {
134
            $loginRoute = route('login');
135
        }
136
        return redirect()->guest($loginRoute);
137
    }
138
139
    /**
140
     * OVERRIDE
141
     * Render the given HttpException.
142
     *
143
     * @param  \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface  $e
2 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
144
     * @return \Symfony\Component\HttpFoundation\Response
145
     */
146
    protected function renderHttpException(HttpExceptionInterface $e)
0 ignored issues
show
introduced by
Method \App\Exceptions\Handler::renderHttpException() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Symfony\Component\HttpFoundation\Response".
Loading history...
147
    {
148
        if (!view()->exists("errors.{$e->getStatusCode()}")) {
149
            return response()->view('errors.default', [
150
                'exception' => $e,
151
                'goc' => Lang::get('common/goc'),
152
                'alert' => Lang::get('common/alert'),
153
                'error' => [
154
                    'title' => 'Error'
155
                ]
156
            ], $e->getStatusCode(), $e->getHeaders());
157
        }
158
        return parent::renderHttpException($e);
159
    }
160
}
161