Passed
Branch master (a9cf56)
by Francisco
07:28
created

Handler::whoopsHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
rs 10
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
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
        if ($this->shouldReport($exception)) {
42
            app('sentry')->captureException($exception);
0 ignored issues
show
Bug introduced by
The method captureException() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

42
            app('sentry')->/** @scrutinizer ignore-call */ captureException($exception);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        }
44
45
        parent::report($exception);
46
    }
47
48
    /**
49
     * Render an exception into an HTTP response.
50
     *
51
     * @param \Illuminate\Http\Request $request
52
     * @param \Exception               $exception
53
     *
54
     * @return mixed
55
     */
56
    public function render($request, Exception $exception)
57
    {
58
        if ($exception instanceof AuthorizationException) {
59
            flash('You do not have permissions to perform this action.')->error();
60
61
            return redirect()->route('dashboard');
62
        }
63
64
        if ($exception instanceof TokenMismatchException) {
65
            flash('Your session has expired and is now restored. Please, try again.')->error();
66
67
            return redirect()->back();
68
        }
69
70
        if ($exception instanceof MethodNotAllowedHttpException) {
71
            throw new NotFoundHttpException($exception->getMessage(), $exception);
72
        }
73
74
        return parent::render($request, $exception);
75
    }
76
77
    /**
78
     * Convert an authentication exception into an unauthenticated response.
79
     *
80
     * @param \Illuminate\Http\Request $request
81
     * @param AuthenticationException  $exception
82
     *
83
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
84
     */
85
    protected function unauthenticated($request, AuthenticationException $exception)
86
    {
87
        if ($request->expectsJson()) {
88
            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...
89
        }
90
91
        return redirect()->guest(route('login'));
92
    }
93
94
    /**
95
     * Get the Whoops handler for the application.
96
     *
97
     * @return \Whoops\Handler\Handler
98
     */
99
    protected function whoopsHandler()
100
    {
101
        try {
102
            return app(\Whoops\Handler\HandlerInterface::class);
103
        } catch (\Illuminate\Contracts\Container\BindingResolutionException $e) {
104
            return parent::whoopsHandler();
105
        }
106
    }
107
}
108