Completed
Pull Request — master (#19)
by Şəhriyar
14:06 queued 09:06
created

Handler   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 82.76%

Importance

Changes 18
Bugs 5 Features 8
Metric Value
wmc 15
c 18
b 5
f 8
lcom 0
cbo 8
dl 0
loc 72
ccs 24
cts 29
cp 0.8276
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A report() 0 4 1
C render() 0 38 14
1
<?php namespace App\Exceptions;
2
3
use Cartalyst\Sentinel\Checkpoints\NotActivatedException;
4
use Illuminate\Database\Eloquent\ModelNotFoundException;
5
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6
use Illuminate\Http\Response as IlluminateResponse;
7
use Illuminate\Session\TokenMismatchException;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
10
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
11
12
class Handler extends ExceptionHandler
13
{
14
    /**
15
     * A list of the exception types that should not be reported.
16
     *
17
     * @var array
18
     */
19
    protected $dontReport = [
20
        HttpException::class,
21
        ModelNotFoundException::class,
22
    ];
23
24
    /**
25
     * Report or log an exception.
26
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
27
     *
28
     * @param  \Exception $e
29
     *
30
     * @return void
31
     */
32 20
    public function report(\Exception $e)
1 ignored issue
show
Unused Code Comprehensibility introduced by
Overwriting this method does not seem to be necessary. You may want to remove this method.
Loading history...
33
    {
34 20
        parent::report($e);
35 20
    }
36
37
    /**
38
     * Render an exception into an HTTP response.
39
     *
40
     * @param  \Illuminate\Http\Request $request
41
     * @param  \Exception               $e
42
     *
43
     * @return \Illuminate\Http\Response
44
     */
45 20
    public function render($request, \Exception $e)
46
    {
47 20
        if ($e instanceof ModelNotFoundException) {
48 7
            $e = new NotFoundHttpException($e->getMessage(), $e);
49 7
        }
50
51 20
        if ($request->ajax() || $request->wantsJson()) {
52 12
            $exceptionClass = get_class($e);
53 12
            $responseBody = ['exception' => $exceptionClass, 'message' => $e->getMessage()];
54
55
            # General exceptions
56 12
            if ($e instanceof UnauthorizedHttpException) {
57 1
                $statusCode = IlluminateResponse::HTTP_UNAUTHORIZED;
58 12
            } elseif ($e instanceof NotActivatedException) {
59
                $statusCode = IlluminateResponse::HTTP_FORBIDDEN;
60 11
            } elseif ($e instanceof NotFoundHttpException) {
61 5
                $statusCode = IlluminateResponse::HTTP_NOT_FOUND;
62 11
            } elseif ($e instanceof \BadMethodCallException) {
63 2
                $statusCode = IlluminateResponse::HTTP_METHOD_NOT_ALLOWED;
64 10
            } elseif ($e instanceof \UnexpectedValueException || $e instanceof TokenMismatchException) {
65 8
                $statusCode = IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY;
66 8
            } else {
67
                $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode();
68
            }
69
70 12
            if (empty($statusCode)) {
71
                $statusCode = IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR;
72
            }
73
74 12
            return response()->json($responseBody)->setStatusCode($statusCode);
75
        }
76
77 8
        if ($request->method() != 'GET' && $request->header('content-type') == 'application/x-www-form-urlencoded') {
78 8
            return redirect()->back()->withInput($request->all())->withErrors($e->getMessage());
79
        }
80
81
        return parent::render($request, $e);
82
    }
83
}
84