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; |
|
|
|
|
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
|
|
|
|
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.