Completed
Push — master ( 43dba6...416142 )
by Mahmoud
09:25 queued 05:38
created

PortExceptionsHandler::report()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Port\Exception\Handler;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\ModelNotFoundException;
7
use Symfony\Component\HttpKernel\Exception\HttpException;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
10
use Illuminate\Auth\AuthenticationException;
11
/**
12
 * Class PortExceptionsHandler
13
 *
14
 * A.K.A (app/Exceptions/Handler.php)
15
 *
16
 * @author  Mahmoud Zalt  <[email protected]>
17
 */
18
class PortExceptionsHandler extends ExceptionHandler
19
{
20
    /**
21
     * A list of the exception types that should not be reported.
22
     *
23
     * @var array
24
     */
25
    protected $dontReport = [
26
        HttpException::class,
27
        ModelNotFoundException::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
     * @param  \Exception  $e
36
     * @return void
37
     */
38
    public function report(Exception $e)
39
    {
40
        return parent::report($e);
41
    }
42
43
    /**
44
     * Render an exception into an HTTP response.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @param  \Exception  $e
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function render($request, Exception $e)
51
    {
52
        if ($e instanceof ModelNotFoundException) {
53
            $e = new NotFoundHttpException($e->getMessage(), $e);
54
        }
55
56
        return parent::render($request, $e);
57
    }
58
59
60
    /**
61
     * Convert an authentication exception into an unauthenticated response.
62
     *
63
     * @param  \Illuminate\Http\Request  $request
64
     * @param  \Illuminate\Auth\AuthenticationException  $exception
65
     * @return \Illuminate\Http\Response
66
     */
67
    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...
68
    {
69
        // TODO: need to be manually tested!
70
71
        if ($request->expectsJson()) {
72
            return response()->json(['error' => 'Unauthenticated.'], 401);
73
        }
74
75
        return redirect()->guest('login');
76
    }
77
}
78