Completed
Push — master ( 8bfd95...2626ba )
by Dan Michael O.
12:11
created

Handler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 17 4
A unauthenticated() 0 8 2
1
<?php
2
3
namespace Colligator\Exceptions;
4
5
use Exception;
6
use Illuminate\Auth\AuthenticationException;
7
use Illuminate\Database\Eloquent\ModelNotFoundException;
8
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9
use Illuminate\Validation\ValidationException;
10
use Illuminate\Auth\Access\AuthorizationException;
11
use Symfony\Component\HttpKernel\Exception\HttpException;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
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
        AuthorizationException::class,
24
        HttpException::class,
25
        ModelNotFoundException::class,
26
        ValidationException::class,
27
    ];
28
29
    /**
30
     * Render an exception into an HTTP response.
31
     *
32
     * @param \Illuminate\Http\Request $request
33
     * @param \Exception               $e
34
     *
35
     * @return \Symfony\Component\HttpFoundation\Response
36
     */
37
    public function render($request, Exception $e)
38
    {
39
        if ($e instanceof NotFoundHttpException) {
40
            return \Response::make([
0 ignored issues
show
Documentation introduced by
array('error' => 'not_fo...the URL you submitted') is of type array<string,string,{"er...ror_message":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
                'error' => 'not_found',
42
                'error_message' => 'Please check the URL you submitted'
43
            ], 404);
44
        }
45
        if ($e instanceof ModelNotFoundException) {
46
            abort(404);
47
        }
48
        if (method_exists($e, 'render')) {
49
            return $e->render();
50
        }
51
52
        return parent::render($request, $e);
53
    }
54
55
    /**
56
     * Convert an authentication exception into an unauthenticated response.
57
     *
58
     * @param  \Illuminate\Http\Request  $request
59
     * @param  \Illuminate\Auth\AuthenticationException  $exception
60
     * @return \Illuminate\Http\Response
61
     */
62
    protected function unauthenticated($request, AuthenticationException $exception)
0 ignored issues
show
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        if ($request->expectsJson()) {
65
            return response()->json(['error' => 'Unauthenticated.'], 401);
66
        }
67
68
        return redirect()->guest('login');
69
    }
70
}
71