Passed
Push — develop ( 99647f...e3783d )
by Nikita
10:44
created

Handler::renderJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
ccs 3
cts 3
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Gameap\Exceptions;
4
5
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6
use Illuminate\Http\Response;
7
8
class Handler extends ExceptionHandler
9
{
10
    private const MAP_EXCEPTION_HTTP_CODE = [
11
        \Gameap\Exceptions\GdaemonAPI\InvalidApiKeyException::class          => Response::HTTP_UNAUTHORIZED,
12
        \Gameap\Exceptions\GdaemonAPI\InvalidTokenExeption::class            => Response::HTTP_UNAUTHORIZED,
13
        \Gameap\Exceptions\Repositories\RecordExistExceptions::class         => Response::HTTP_UNPROCESSABLE_ENTITY,
14
        \Gameap\Exceptions\Repositories\RepositoryValidationException::class => Response::HTTP_BAD_REQUEST,
15
        \Illuminate\Validation\ValidationException::class                    => Response::HTTP_UNPROCESSABLE_ENTITY,
16
    ];
17
18
    public function render($request, \Throwable $exception)
19
    {
20
        if ($request->expectsJson() || $request->isJson()) {
21
            return $this->renderJson($request, $exception);
22
        }
23
24
        if ($exception instanceof \Gameap\Exceptions\GdaemonAPI\InvalidSetupTokenExeption) {
25
            if (app()->has('debugbar')) {
26
                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

26
                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...
27
            }
28
29
            // Return bash
30
            return response()->make('echo "' . $exception->getMessage() . '"', 401);
31
        }
32
33
        return parent::render($request, $exception);
34
    }
35
36
    private function renderJson($request, \Throwable $exception)
37
    {
38
        foreach (self::MAP_EXCEPTION_HTTP_CODE as $instance => $httpCode) {
39 27
            if ($exception instanceof $instance) {
40
                return response()->json([
41 27
                    'message'   => $exception->getMessage(),
42 27
                    'http_code' => $httpCode,
43
                ], $httpCode);
44
            }
45
        }
46
47
        return parent::render($request, $exception);
48
    }
49
}
50