Passed
Pull Request — master (#71)
by Adeniyi
05:43
created

Handler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 16 5
A report() 0 3 1
A unauthenticated() 0 7 2
1
<?php
2
3
namespace App\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8
use Illuminate\Support\Facades\Log;
9
use Symfony\Component\HttpKernel\Exception\HttpException;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
/**
13
 * @codeCoverageIgnore
14
 */
15
class Handler extends ExceptionHandler
16
{
17
    /**
18
     * A list of the exception types that should not be reported.
19
     *
20
     * @var array
21
     */
22
    protected $dontReport = [
23
        \Illuminate\Auth\AuthenticationException::class,
24
        \Illuminate\Auth\Access\AuthorizationException::class,
25
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
26
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
27
        \Illuminate\Session\TokenMismatchException::class,
28
        \Illuminate\Validation\ValidationException::class,
29
    ];
30
31
    /**
32
     * Report or log an exception.
33
     *
34
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
35
     *
36
     * @param  \Exception  $exception
37
     * @return void
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
     * @return \Illuminate\Http\Response
50
     */
51
    public function render($request, Exception $exception)
52
    {
53
        if ($exception instanceof NotFoundHttpException) {
54
            return response()->view('layouts.errors.404', [], 404);
55
        } elseif ($exception instanceof HttpException && $exception->getStatusCode() == 403) {
56
            return response()->view(
57
                'layouts.errors.403',
58
                ['error' => 'Sorry, this page is restricted to authorized users only.'],
59
                403
60
            );
61
        } elseif ($exception instanceof HttpException) {
62
            Log::info($exception->getMessage());
63
            return response()->view('layouts.errors.503', ['error' => $exception->getTrace()], 500);
64
        }
65
66
        return parent::render($request, $exception);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::render($request, $exception) also could return the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
67
    }
68
69
    /**
70
     * Convert an authentication exception into an unauthenticated response.
71
     *
72
     * @param  \Illuminate\Http\Request  $request
73
     * @param  \Illuminate\Auth\AuthenticationException  $exception
74
     * @return \Illuminate\Http\Response
75
     */
76
    protected function unauthenticated($request, AuthenticationException $exception)
77
    {
78
        if ($request->expectsJson()) {
79
            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...
80
        }
81
82
        return redirect()->guest(route('login'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->guest(route('login')) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
83
    }
84
}
85