Passed
Push — develop ( 7d6347...bbcebd )
by Nikita
06:10
created

Handler::render()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8.125

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 16
ccs 4
cts 8
cp 0.5
crap 8.125
rs 9.6111
1
<?php
2
3
namespace Gameap\Exceptions;
4
5
use Exception;
6
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7
use Illuminate\Http\Response;
8
use Gameap\Exceptions\Repositories\RepositoryValidationException;
9
10
class Handler extends ExceptionHandler
11
{
12
    /**
13
     * A list of the exception types that are not reported.
14
     *
15
     * @var array
16
     */
17
    protected $dontReport = [
18
        //
19
    ];
20
21
    /**
22
     * A list of the inputs that are never flashed for validation exceptions.
23
     *
24
     * @var array
25
     */
26
    protected $dontFlash = [
27
        'password',
28
        'password_confirmation',
29
    ];
30
31
    /**
32
     * Report or log an exception.
33
     *
34
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
35
     *
36
     * @param  \Exception  $exception
37
     * @return void
38
     */
39 42
    public function report(Exception $exception)
40
    {
41 42
        parent::report($exception);
42 42
    }
43
44
    /**
45
     * Render an exception into an HTTP response.
46
     * 
47
     * @param \Illuminate\Http\Request $request
48
     * @param Exception                $exception
49
     *
50
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
51
     */
52 42
    public function render($request, Exception $exception)
53
    {
54 42
        if ($request->expectsJson() || $request->isJson()) {
55
            return $this->renderJson($request, $exception);
56
        } else {
57 42
            if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidSetupTokenExeption) {
58
                if (app()->has('debugbar')) {
59
                    app('debugbar')->disable();
0 ignored issues
show
Bug introduced by
The method disable() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                    app('debugbar')->/** @scrutinizer ignore-call */ disable();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
                }
61
62
                // Return bash
63
                return response()->make('echo "' . $exception->getMessage() . '"', 401);
64
            }
65
        }
66
        
67 42
        return parent::render($request, $exception);
68
    }
69
70
    /**
71
     * @param           $request
72
     * @param Exception $exception
73
     *
74
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
75
     */
76
    public function renderJson($request, Exception $exception)
77
    {
78
        if ($exception instanceof RepositoryValidationException) {
79
            return response()->json([
80
                'message' => $exception->getMessage(),
81
                'http_code' => Response::HTTP_BAD_REQUEST
82
            ], Response::HTTP_BAD_REQUEST);
83
        }
84
85
        if ($exception instanceof \Gameap\Exceptions\Repositories\RecordExistExceptions) {
86
            return response()->json([
87
                'message' => $exception->getMessage(),
88
                'http_code' => Response::HTTP_UNPROCESSABLE_ENTITY
89
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
90
        }
91
92
        // Gdaemon API
93
        if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidApiKeyException
94
            || $exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidTokenExeption
95
        ) {
96
            return response()->json([
97
                'message' => $exception->getMessage(),
98
                'http_code' => Response::HTTP_UNAUTHORIZED
99
            ], Response::HTTP_UNAUTHORIZED);
100
        } else if ($exception instanceof \Illuminate\Validation\ValidationException) {
101
            return response()->json([
102
                'message' => $exception->getMessage() . ' ' . $exception->validator->errors()->first(),
103
                'http_code' => Response::HTTP_UNPROCESSABLE_ENTITY
104
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
105
        }
106
107
        return parent::render($request, $exception);
108
    }
109
}
110