Handler::unauthenticated()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
9
class Handler extends ExceptionHandler
10
{
11
    /**
12
     * A list of the exception types that are not reported.
13
     *
14
     * @var array
15
     */
16
    protected $dontReport = [
17
        //
18
    ];
19
20
    /**
21
     * A list of the inputs that are never flashed for validation exceptions.
22
     *
23
     * @var array
24
     */
25
    protected $dontFlash = [
26
        'password',
27
        'password_confirmation',
28
    ];
29
30
    /**
31
     * Report or log an exception.
32
     *
33
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
34
     *
35
     * @param  \Exception  $exception
36
     * @return void
37
     */
38
    public function report(Exception $exception)
39
    {
40
        parent::report($exception);
41
    }
42
43
    /**
44
     * Render an exception into an HTTP response.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @param  \Exception  $exception
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function render($request, Exception $exception)
51
    {
52
        return parent::render($request, $exception);
53
    }
54
55
    /**
56
     * Convert an authentication exception into a response.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @param  \Illuminate\Auth\AuthenticationException  $exception
60
     * @return \Illuminate\Http\Response
61
     */
62
    protected function unauthenticated($request, AuthenticationException $exception)
63
    {
64
        $middleware = \Route::getCurrentRoute()->computedMiddleware;
65
        if (in_array('api', $middleware) || $request->expectsJson()) {
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

65
        if (in_array('api', /** @scrutinizer ignore-type */ $middleware) || $request->expectsJson()) {
Loading history...
66
            $response = response()->json(['message' => 'Unauthenticated.'], 401);
67
        } else {
68
            $response = redirect()->guest(route('authentication.index'));
69
        }
70
71
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Illuminate\Http\JsonResp...e\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
72
    }
73
}
74