Handler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
c 0
b 0
f 0
dl 0
loc 52
ccs 6
cts 11
cp 0.5455
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 7 2
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\Access\AuthorizationException;
7
use Illuminate\Auth\AuthenticationException;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10
use Illuminate\Http\Request;
11
use Illuminate\Validation\ValidationException;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\HttpException;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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
        AuthorizationException::class,
25
        HttpException::class,
26
        ModelNotFoundException::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
     * @throws Exception
36
     */
37 6
    public function report(Exception $e): void
38
    {
39 6
        parent::report($e);
40 6
    }
41
42
    /**
43
     * Render an exception into an HTTP response.
44
     *
45
     * @param Request $request
46
     */
47 6
    public function render($request, Exception $e): Response
48
    {
49 6
        if ($e instanceof ModelNotFoundException) {
50
            $e = new NotFoundHttpException($e->getMessage(), $e);
51
        }
52
53 6
        return parent::render($request, $e);
54
    }
55
56
    /**
57
     * Convert an authentication exception into an unauthenticated response.
58
     *
59
     * @param Request $request
60
     */
61
    protected function unauthenticated($request, AuthenticationException $exception): Response
62
    {
63
        if ($request->expectsJson()) {
64
            return response()->json(['error' => 'Unauthenticated.'], 401);
65
        }
66
67
        return redirect()->guest('login');
68
    }
69
}
70