Renderer::render()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 71
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 45
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 71
ccs 0
cts 38
cp 0
crap 72
rs 7.9555

How to fix   Long Method   

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
2
3
namespace Napp\Core\Api\Exceptions\Renderer;
4
5
use Illuminate\Http\JsonResponse;
6
use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException;
7
8
/**
9
 * Class Renderer.
10
 */
11
class Renderer implements RendererInterface
12
{
13
    /**
14
     * @var \Exception
15
     */
16
    protected $exception;
17
18
    /**
19
     * @var int
20
     */
21
    protected $statusCode;
22
23
    /**
24
     * @var string
25
     */
26
    protected $statusMessage;
27
28
    /**
29
     * @var int
30
     */
31
    protected $responseCode;
32
33
    /**
34
     * @return JsonResponse
35
     */
36
    public function render(): JsonResponse
37
    {
38
        if (true === $this->exception instanceof NappException) {
39
            if ($this->exception instanceof \JsonSerializable) {
40
                return response()->json($this->exception->jsonSerialize(), $this->responseCode);
41
            }
42
43
            return response()->json(
44
                [
45
                    'error' => [
46
                        'code'    => $this->statusCode,
47
                        'message' => $this->statusMessage,
48
                    ], ],
49
                $this->responseCode
50
            );
51
        }
52
53
        switch ($this->responseCode) {
54
            case 400:
55
                return response()->json(
56
                    [
57
                        'error' => [
58
                            'code'    => $this->statusCode,
59
                            'message' => 'Unprocessable Entity',
60
                        ], ],
61
                    $this->responseCode
62
                );
63
            case 401:
64
                return response()->json(
65
                    [
66
                        'error' => [
67
                            'code'    => $this->statusCode,
68
                            'message' => 'Authentication credentials were missing or incorrect',
69
                        ], ],
70
                    $this->responseCode
71
                );
72
            case 403:
73
                return response()->json(
74
                    [
75
                        'error' => [
76
                            'code'    => $this->statusCode,
77
                            'message' => 'Forbidden',
78
                        ], ],
79
                    $this->responseCode
80
                );
81
            case 404:
82
                return response()->json(
83
                    [
84
                        'error' => [
85
                            'code'    => $this->statusCode,
86
                            'message' => 'Not Found',
87
                        ], ],
88
                    $this->responseCode
89
                );
90
            case 405:
91
                return response()->json(
92
                    [
93
                        'error' => [
94
                            'code'    => $this->statusCode,
95
                            'message' => 'Method Not Allowed',
96
                        ], ],
97
                    $this->responseCode
98
                );
99
            default:
100
                return response()->json(
101
                    [
102
                        'error' => [
103
                            'code'    => $this->statusCode,
104
                            'message' => 'Internal Server Error',
105
                        ], ],
106
                    500
107
                );
108
        }
109
    }
110
111
    /**
112
     * @param \Throwable $e
113
     *
114
     * @return void
115
     */
116
    public function setException(\Throwable $e)
117
    {
118
        $this->exception = $e;
0 ignored issues
show
Documentation Bug introduced by
$e is of type Throwable, but the property $exception was declared to be of type Exception. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
119
    }
120
121
    /**
122
     * @param int $responseCode
123
     *
124
     * @return void
125
     */
126
    public function setResponseCode($responseCode)
127
    {
128
        $this->responseCode = $responseCode;
129
    }
130
131
    /**
132
     * @param int $statusCode
133
     *
134
     * @return void
135
     */
136
    public function setStatusCode($statusCode)
137
    {
138
        $this->statusCode = $statusCode;
139
    }
140
141
    /**
142
     * @param string $statusMessage
143
     *
144
     * @return void
145
     */
146
    public function setStatusMessage($statusMessage)
147
    {
148
        $this->statusMessage = $statusMessage;
149
    }
150
}
151