Passed
Push — ft/appmove ( db87fd...97613e )
by Philippe
45:05 queued 26:47
created

Handler::unauthenticated()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 4
nop 2
dl 0
loc 13
c 0
b 0
f 0
cc 6
rs 9.2222
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Exceptions;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Auth\AuthenticationException;
8
use Illuminate\Validation\ValidationException;
9
use Illuminate\Auth\Access\AuthorizationException;
10
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
11
12
class Handler extends ExceptionHandler
13
{
14
    /**
15
     * A list of the exception types that should not be reported.
16
     *
17
     * @var array
18
     */
19
    protected $dontReport = [
20
        //
21
    ];
22
23
    /**
24
     * A list of the inputs that are never flashed for validation exceptions.
25
     *
26
     * @var array
27
     */
28
    protected $dontFlash = [
29
        'password',
30
        'password_confirmation',
31
    ];
32
33
    /**
34
     * Report or log an exception.
35
     *
36
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
37
     *
38
     * @param  \Exception  $exception
39
     * @return void
40
     */
41
    public function report(Exception $exception)
42
    {
43
        parent::report($exception);
44
    }
45
46
    /**
47
     * Render an exception into an HTTP response.
48
     *
49
     * @param  \Illuminate\Http\Request  $request
50
     * @param  \Exception  $exception
51
     * @return \Illuminate\Http\Response
52
     */
53
    public function render($request, Exception $exception)
54
    {
55
        if ($exception instanceof AuthorizationException) {
56
            return $this->unauthorized($request, $exception);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->unauthorized($request, $exception) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
57
        }
58
59
        //could use some code cleanup
60
        if ((strpos(url()->previous(), 'admin') || strpos(url()->current(), 'admin')) && !$exception instanceof AuthenticationException && !$exception instanceof ValidationException) {
61
            return $this->renderChiefException($request, $exception);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderChie...n($request, $exception) also could return the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
62
        }
63
64
        return parent::render($request, $exception);
65
    }
66
67
    protected function renderChiefException($request, Exception $exception)
68
    {
69
        if (!config('app.debug')) {
70
            if ($request->expectsJson()) {
71
                return response()->json(['error' => 'Something went wrong.'], 404);
72
            }
73
74
            return response()->view('chief::back.errors.custom');
75
        }
76
        
77
        return parent::render($request, $exception);
78
    }
79
80
81
    protected function unauthorized($request, AuthorizationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

81
    protected function unauthorized(/** @scrutinizer ignore-unused */ $request, AuthorizationException $exception)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

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

81
    protected function unauthorized($request, /** @scrutinizer ignore-unused */ AuthorizationException $exception)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        return redirect()->route('chief.back.dashboard')
84
                         ->with('messages.error', 'Oeps. Het lijkt erop dat je geen toegang hebt tot dit deel van chief. Vraag even de beheerder voor meer info.');
85
    }
86
87
    /**
88
     * Convert an authentication exception into an unauthenticated response.
89
     *
90
     * @param  \Illuminate\Http\Request  $request
91
     * @param  \Illuminate\Auth\AuthenticationException  $exception
92
     * @return \Illuminate\Http\Response
93
     */
94
    protected function unauthenticated($request, AuthenticationException $exception)
95
    {
96
        if ($request->expectsJson()) {
97
            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...
98
        }
99
100
        if (!empty($exception->guards()) && Arr::first($exception->guards()) == 'chief') {
101
            return redirect()->guest(route('chief.back.login'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->guest...te('chief.back.login')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
        }
103
104
        return $request->expectsJson()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request->expects...on->redirectTo() : '/') returns the type Illuminate\Http\JsonResp...e\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
            ? response()->json(['message' => $exception->getMessage()], 401)
106
            : redirect()->guest(method_exists($exception, 'redirectTo') ? $exception->redirectTo() : '/');
107
    }
108
}
109