Completed
Pull Request — master (#19)
by Şəhriyar
33:48
created

Handler::render()   D

Complexity

Conditions 19
Paths 260

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 23.7935

Importance

Changes 18
Bugs 5 Features 7
Metric Value
c 18
b 5
f 7
dl 0
loc 55
ccs 29
cts 38
cp 0.7632
rs 4.8838
cc 19
eloc 37
nc 260
nop 2
crap 23.7935

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function report(\Exception $e)
33 20
    {
34
        parent::report($e);
35 20
    }
36 20
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
    public function render($request, \Exception $e)
46 20
    {
47
        if ($e instanceof ModelNotFoundException) {
48 20
            $e = new NotFoundHttpException($e->getMessage(), $e);
49 7
        }
50 7
51
        if ($request->ajax() || $request->wantsJson()) {
52
            $exceptionClass = get_class($e);
53
            $responseBody = ['exception' => $exceptionClass, 'message' => $e->getMessage()];
54
55
            # FileStream exceptions response body
56
            $preventRetry = true;
57 20
            $resetUpload = false;
58 12
            if ($e instanceof FileStream\UploadFilenameIsEmptyException) {
59 12
                $preventRetry = false;
60
            } elseif ($e instanceof FileStream\UploadIncompleteException) {
61 12
                $preventRetry = false;
62 1
                $resetUpload = true;
63 1
            } elseif ($e instanceof FileStream\UploadAttemptFailedException) {
64 11
                $preventRetry = false;
65
            }
66
            if (strpos($exceptionClass, 'App\Exceptions\FileStream') !== false) {
67 11
                $responseBody = ['exception' => $exceptionClass, 'error' => $e->getMessage(), 'preventRetry' => $preventRetry, 'reset' => $resetUpload];
68 5
            }
69 5
70 10
            # Status codes
71 2
            if ($e instanceof UnauthorizedHttpException) {
72 2
                $statusCode = IlluminateResponse::HTTP_UNAUTHORIZED;
73 8
            } elseif ($e instanceof NotActivatedException) {
74 8
                $statusCode = IlluminateResponse::HTTP_FORBIDDEN;
75 8
            } elseif ($e instanceof NotFoundHttpException) {
76 8
                $statusCode = IlluminateResponse::HTTP_NOT_FOUND;
77 8
            } elseif ($e instanceof \BadMethodCallException) {
78 8
                $statusCode = IlluminateResponse::HTTP_METHOD_NOT_ALLOWED;
79 8
            } elseif ($e instanceof \UnexpectedValueException || $e instanceof TokenMismatchException) {
80 8
                $statusCode = IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY;
81
            } elseif ($e instanceof \OverflowException) {
82
                $statusCode = IlluminateResponse::HTTP_REQUEST_ENTITY_TOO_LARGE;
83
            } else {
84
                $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : $e->getCode();
85 12
            }
86
87
            if (empty($statusCode)) {
88
                $statusCode = IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR;
89 12
            }
90
91
            return response()->json($responseBody)->setStatusCode($statusCode);
92 8
        }
93 8
94
        if ($request->method() != 'GET' && $request->header('content-type') == 'application/x-www-form-urlencoded') {
95
            return redirect()->back()->withInput($request->all())->withErrors($e->getMessage());
96
        }
97
98
        return parent::render($request, $e);
99
    }
100
}
101