Passed
Push — develop ( e6f115...fe9a15 )
by Nikita
07:48
created

Handler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A report() 0 3 1
A render() 0 16 5
A renderJson() 0 25 5
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
9
class Handler extends ExceptionHandler
10
{
11
    /**
12
     * A list of the exception types that are not reported.
13
     *
14
     * @var array
15
     */
16
    protected $dontReport = [
17
        //
18
    ];
19
20
    /**
21
     * A list of the inputs that are never flashed for validation exceptions.
22
     *
23
     * @var array
24
     */
25
    protected $dontFlash = [
26
        'password',
27
        'password_confirmation',
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  $exception
36
     * @return void
37
     */
38
    public function report(Exception $exception)
39
    {
40
        parent::report($exception);
41
    }
42
43
    /**
44
     * Render an exception into an HTTP response.
45
     *
46
     * @param  \Illuminate\Http\Request  $request
47
     * @param  \Exception  $exception
48
     * @return \Illuminate\Http\Response
49
     */
50
    public function render($request, Exception $exception)
51
    {
52
        if ($request->expectsJson() || $request->isJson()) {
53
            return $this->renderJson($request, $exception);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->renderJson($request, $exception) also could return the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
54
        } else {
55
            if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidSetupTokenExeption) {
56
                if (app()->has('debugbar')) {
57
                    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

57
                    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...
58
                }
59
60
                // Return bash
61
                return response()->make('echo "' . $exception->getMessage() . '"', 401);
62
            }
63
        }
64
        
65
        return parent::render($request, $exception);
66
    }
67
    
68
    public function renderJson($request, Exception $exception)
69
    {
70
        if ($exception instanceof \Gameap\Exceptions\Repositories\RecordExistExceptions) {
71
            return response()->json([
72
                'message' => $exception->getMessage(),
73
                'http_code' => Response::HTTP_UNPROCESSABLE_ENTITY
74
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
75
        }
76
77
        // Gdaemon API
78
        if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidApiKeyException
79
            || $exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidTokenExeption
80
        ) {
81
            return response()->json([
82
                'message' => $exception->getMessage(),
83
                'http_code' => Response::HTTP_UNAUTHORIZED
84
            ], Response::HTTP_UNAUTHORIZED);
85
        } else if ($exception instanceof \Illuminate\Validation\ValidationException) {
86
            return response()->json([
87
                'message' => $exception->getMessage() . ' ' . $exception->validator->errors()->first(),
88
                'http_code' => Response::HTTP_UNPROCESSABLE_ENTITY
89
            ], Response::HTTP_UNPROCESSABLE_ENTITY);
90
        }
91
92
        return parent::render($request, $exception);
93
    }
94
}
95