ApiHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 63
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B render() 0 53 6
1
<?php
2
3
namespace Napp\Core\Api\Exceptions;
4
5
use Illuminate\Auth\AuthenticationException;
6
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7
use Illuminate\Validation\ValidationException;
8
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
12
/**
13
 * Class ApiHandler.
14
 */
15
class ApiHandler extends ExceptionHandler
16
{
17
    /**
18
     * @param \Illuminate\Http\Request $request
19
     * @param \Exception|\Throwable $e
20
     *
21
     * @throws \ReflectionException
22
     *
23
     * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
24
     */
25
    public function render($request, $e)
26
    {
27
        if (true === app()->isDownForMaintenance()) {
0 ignored issues
show
introduced by
The method isDownForMaintenance() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

27
        if (true === app()->/** @scrutinizer ignore-call */ isDownForMaintenance()) {
Loading history...
28
            return response()->json([
29
                'error' => [
30
                    'code'    => 503,
31
                    'message' => 'Service is down for scheduled maintenance. Be right back!',
32
                ],
33
            ], Response::HTTP_SERVICE_UNAVAILABLE);
34
        }
35
36
        if ($e instanceof NotFoundHttpException) {
37
            return response()->json(
38
                [
39
                    'code' => 34,
40
                    'message' => 'Not Found',
41
                ],
42
                Response::HTTP_NOT_FOUND
43
            );
44
        }
45
46
        if ($e instanceof ValidationException) {
47
            return response()->json(
48
                [
49
                    'code' => 215,
50
                    'message' => 'Validation failed',
51
                    'errors' => $e->validator->errors()->toArray(),
52
                ],
53
                Response::HTTP_UNPROCESSABLE_ENTITY
54
            );
55
        }
56
57
        if ($e instanceof AuthenticationException) {
58
            return response()->json([
59
                'error' => [
60
                    'code'    => 64,
61
                    'message' => 'Forbidden',
62
                ],
63
            ], Response::HTTP_FORBIDDEN);
64
        }
65
66
        if ($e instanceof ApiInternalCallValidationException) {
67
            $response = response([
68
                'error' => [
69
                    'code'    => 215,
70
                    'message' => 'Validation failed',
71
                ],
72
            ], 400);
73
74
            return $response->withException($e);
75
        }
76
77
        return (new NappApiHandler($e))->render();
78
    }
79
}
80