Passed
Push — develop ( dfa246...179c36 )
by Francisco
02:27
created

Handler::unauthenticated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Session\TokenMismatchException;
8
use Illuminate\Validation\ValidationException;
9
use Illuminate\Auth\Access\AuthorizationException;
10
use Illuminate\Database\Eloquent\ModelNotFoundException;
11
use Symfony\Component\HttpKernel\Exception\HttpException;
12
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
13
14
class Handler extends ExceptionHandler
15
{
16
    /**
17
     * A list of the exception types that should not be reported.
18
     *
19
     * @var array
20
     */
21
    protected $dontReport = [
22
        AuthenticationException::class,
23
        AuthorizationException::class,
24
        HttpException::class,
25
        ModelNotFoundException::class,
26
        TokenMismatchException::class,
27
        ValidationException::class,
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
     */
37
    public function report(Exception $exception)
38
    {
39
        parent::report($exception);
40
    }
41
42
    /**
43
     * Render an exception into an HTTP response.
44
     *
45
     * @param \Illuminate\Http\Request $request
46
     * @param \Exception               $exception
47
     *
48
     * @return mixed
49
     */
50
    public function render($request, Exception $exception)
51
    {
52
        if ($exception instanceof AuthorizationException) {
53
            flash('You do not have permissions to perform this action.')->error();
54
55
            return redirect()->route('home');
56
        }
57
58
        if ($exception instanceof TokenMismatchException) {
59
            flash('Your session has expired and is now restored. Please, try again.')->error();
60
61
            return redirect()->back();
62
        }
63
64
        return parent::render($request, $exception);
65
    }
66
67
    /**
68
     * Convert an authentication exception into an unauthenticated response.
69
     *
70
     * @param \Illuminate\Http\Request $request
71
     * @param AuthenticationException  $exception
72
     *
73
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
74
     */
75
    protected function unauthenticated($request, AuthenticationException $exception)
0 ignored issues
show
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

75
    protected function unauthenticated($request, /** @scrutinizer ignore-unused */ AuthenticationException $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...
76
    {
77
        if ($request->expectsJson()) {
78
            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\Redirect...lluminate\Http\Response.
Loading history...
79
        }
80
81
        return redirect()->guest(route('login'));
82
    }
83
}
84