Passed
Push — develop ( a18085...de8258 )
by Francisco
14:47
created

Handler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 72
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A report() 0 3 1
A unauthenticated() 0 7 2
A render() 0 19 4
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
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
15
16
class Handler extends ExceptionHandler
17
{
18
    /**
19
     * A list of the exception types that should not be reported.
20
     *
21
     * @var array
22
     */
23
    protected $dontReport = [
24
        AuthenticationException::class,
25
        AuthorizationException::class,
26
        HttpException::class,
27
        ModelNotFoundException::class,
28
        TokenMismatchException::class,
29
        ValidationException::class,
30
    ];
31
32
    /**
33
     * Report or log an exception.
34
     *
35
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
36
     *
37
     * @param \Exception $exception
38
     */
39
    public function report(Exception $exception)
40
    {
41
        parent::report($exception);
42
    }
43
44
    /**
45
     * Render an exception into an HTTP response.
46
     *
47
     * @param \Illuminate\Http\Request $request
48
     * @param \Exception               $exception
49
     *
50
     * @return mixed
51
     */
52
    public function render($request, Exception $exception)
53
    {
54
        if ($exception instanceof AuthorizationException) {
55
            flash('You do not have permissions to perform this action.')->error();
56
57
            return redirect()->route('home');
58
        }
59
60
        if ($exception instanceof TokenMismatchException) {
61
            flash('Your session has expired and is now restored. Please, try again.')->error();
62
63
            return redirect()->back();
64
        }
65
66
        if ($exception instanceof MethodNotAllowedHttpException) {
67
            throw new NotFoundHttpException($exception->getMessage(), $exception);
68
        }
69
70
        return parent::render($request, $exception);
71
    }
72
73
    /**
74
     * Convert an authentication exception into an unauthenticated response.
75
     *
76
     * @param \Illuminate\Http\Request $request
77
     * @param AuthenticationException  $exception
78
     *
79
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
80
     */
81
    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

81
    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...
82
    {
83
        if ($request->expectsJson()) {
84
            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...
85
        }
86
87
        return redirect()->guest(route('login'));
88
    }
89
}
90