|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MediaMonks\RestApiBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use MediaMonks\RestApiBundle\Exception\AbstractValidationException; |
|
6
|
|
|
use MediaMonks\RestApiBundle\Exception\ExceptionInterface; |
|
7
|
|
|
use MediaMonks\RestApiBundle\Response\Error; |
|
8
|
|
|
use MediaMonks\RestApiBundle\Response\PaginatedResponseInterface; |
|
9
|
|
|
use MediaMonks\RestApiBundle\Util\StringUtil; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
13
|
|
|
|
|
14
|
|
|
class ResponseModel |
|
15
|
|
|
{ |
|
16
|
|
|
const EXCEPTION_GENERAL = 'Exception'; |
|
17
|
|
|
const EXCEPTION_HTTP = 'HttpException'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $statusCode = Response::HTTP_OK; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $returnStatusCode = false; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $data; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Response |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $response; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \Exception |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $exception; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var PaginatedResponseInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $pagination; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return int |
|
51
|
|
|
*/ |
|
52
|
32 |
|
public function getStatusCode() |
|
53
|
|
|
{ |
|
54
|
32 |
|
if (isset($this->response)) { |
|
55
|
6 |
|
return $this->response->getStatusCode(); |
|
56
|
|
|
} |
|
57
|
26 |
|
if (isset($this->exception)) { |
|
58
|
13 |
|
return $this->getExceptionStatusCode(); |
|
59
|
|
|
} |
|
60
|
13 |
|
if ($this->isEmpty()) { |
|
61
|
2 |
|
return Response::HTTP_NO_CONTENT; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
11 |
|
return $this->statusCode; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
13 |
|
protected function getExceptionStatusCode() |
|
71
|
|
|
{ |
|
72
|
13 |
|
if ($this->exception instanceof HttpException) { |
|
73
|
4 |
|
return $this->exception->getStatusCode(); |
|
74
|
9 |
|
} elseif ($this->exception instanceof AbstractValidationException) { |
|
75
|
4 |
|
return Response::HTTP_BAD_REQUEST; |
|
76
|
5 |
|
} elseif ($this->isValidHttpStatusCode($this->exception->getCode())) { |
|
77
|
2 |
|
return $this->exception->getCode(); |
|
78
|
|
|
} |
|
79
|
3 |
|
return Response::HTTP_INTERNAL_SERVER_ERROR; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $code |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
5 |
|
protected function isValidHttpStatusCode($code) |
|
87
|
|
|
{ |
|
88
|
5 |
|
return array_key_exists($code, Response::$statusTexts) && $code >= Response::HTTP_BAD_REQUEST; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $statusCode |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
9 |
|
public function setStatusCode($statusCode) |
|
96
|
|
|
{ |
|
97
|
9 |
|
$this->statusCode = $statusCode; |
|
98
|
|
|
|
|
99
|
9 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
26 |
|
public function getReturnStatusCode() |
|
106
|
|
|
{ |
|
107
|
26 |
|
return $this->returnStatusCode; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param bool $returnStatusCode |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
2 |
|
public function setReturnStatusCode($returnStatusCode) |
|
115
|
|
|
{ |
|
116
|
2 |
|
$this->returnStatusCode = $returnStatusCode; |
|
117
|
|
|
|
|
118
|
2 |
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
10 |
|
public function getData() |
|
125
|
|
|
{ |
|
126
|
10 |
|
return $this->data; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param mixed $data |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
24 |
|
public function setData($data) |
|
134
|
|
|
{ |
|
135
|
24 |
|
$this->data = $data; |
|
136
|
|
|
|
|
137
|
24 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return \Exception |
|
142
|
|
|
*/ |
|
143
|
11 |
|
public function getException() |
|
144
|
|
|
{ |
|
145
|
11 |
|
return $this->exception; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param \Exception $exception |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
15 |
|
public function setException(\Exception $exception) |
|
153
|
|
|
{ |
|
154
|
15 |
|
$this->exception = $exception; |
|
155
|
|
|
|
|
156
|
15 |
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
11 |
|
public function getPagination() |
|
163
|
|
|
{ |
|
164
|
11 |
|
return $this->pagination; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param PaginatedResponseInterface $pagination |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
4 |
|
public function setPagination(PaginatedResponseInterface $pagination) |
|
172
|
|
|
{ |
|
173
|
4 |
|
$this->pagination = $pagination; |
|
174
|
4 |
|
$this->setData($pagination->getData()); |
|
175
|
|
|
|
|
176
|
4 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return Response |
|
181
|
|
|
*/ |
|
182
|
11 |
|
public function getResponse() |
|
183
|
|
|
{ |
|
184
|
11 |
|
return $this->response; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param Response $response |
|
189
|
|
|
* @return ResponseModel |
|
190
|
|
|
*/ |
|
191
|
7 |
|
public function setResponse(Response $response) |
|
192
|
|
|
{ |
|
193
|
7 |
|
$this->response = $response; |
|
194
|
7 |
|
$this->setStatusCode($response->getStatusCode()); |
|
195
|
7 |
|
$this->setData($response->getContent()); |
|
196
|
|
|
|
|
197
|
7 |
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return array |
|
202
|
|
|
*/ |
|
203
|
25 |
|
public function toArray() |
|
204
|
|
|
{ |
|
205
|
25 |
|
$return = []; |
|
206
|
25 |
|
if ($this->getReturnStatusCode()) { |
|
207
|
1 |
|
$return['statusCode'] = $this->getStatusCode(); |
|
208
|
1 |
|
} |
|
209
|
25 |
|
if (isset($this->exception)) { |
|
210
|
12 |
|
$return['error'] = $this->exceptionToArray(); |
|
211
|
25 |
|
} elseif (isset($this->response) && $this->response instanceof RedirectResponse) { |
|
212
|
2 |
|
$return['location'] = $this->response->headers->get('Location'); |
|
213
|
2 |
|
} else { |
|
214
|
11 |
|
$return += $this->dataToArray(); |
|
215
|
|
|
} |
|
216
|
25 |
|
return $return; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
11 |
|
protected function dataToArray() |
|
223
|
|
|
{ |
|
224
|
11 |
|
$return = []; |
|
225
|
11 |
|
if (isset($this->data)) { |
|
226
|
10 |
|
$return['data'] = $this->data; |
|
227
|
10 |
|
if (isset($this->pagination)) { |
|
228
|
3 |
|
$return['pagination'] = $this->pagination->toArray(); |
|
229
|
3 |
|
} |
|
230
|
10 |
|
} |
|
231
|
11 |
|
return $return; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
12 |
|
protected function exceptionToArray() |
|
238
|
|
|
{ |
|
239
|
12 |
|
if ($this->exception instanceof ExceptionInterface) { |
|
240
|
4 |
|
return $this->exception->toArray(); |
|
241
|
8 |
|
} elseif ($this->exception instanceof HttpException) { |
|
242
|
4 |
|
return $this->httpExceptionToArray(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
4 |
|
return $this->generalExceptionToArray(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return array |
|
250
|
|
|
*/ |
|
251
|
4 |
|
protected function httpExceptionToArray() |
|
252
|
|
|
{ |
|
253
|
|
|
return [ |
|
254
|
4 |
|
'code' => $this->getExceptionErrorCode(Error::CODE_HTTP, self::EXCEPTION_HTTP), |
|
255
|
4 |
|
'message' => $this->exception->getMessage() |
|
256
|
4 |
|
]; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return array |
|
261
|
|
|
*/ |
|
262
|
4 |
|
protected function generalExceptionToArray() |
|
263
|
|
|
{ |
|
264
|
|
|
return [ |
|
265
|
4 |
|
'code' => trim($this->getExceptionErrorCode(Error::CODE_GENERAL, self::EXCEPTION_GENERAL), '.'), |
|
266
|
4 |
|
'message' => $this->exception->getMessage() |
|
267
|
4 |
|
]; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param string $errorCode |
|
272
|
|
|
* @param string $trim |
|
273
|
|
|
* @return string |
|
274
|
|
|
*/ |
|
275
|
8 |
|
protected function getExceptionErrorCode($errorCode, $trim = null) |
|
276
|
|
|
{ |
|
277
|
8 |
|
return sprintf($errorCode, StringUtil::classToSnakeCase($this->exception, $trim)); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
19 |
|
public function isEmpty() |
|
284
|
|
|
{ |
|
285
|
|
|
return ( |
|
286
|
19 |
|
!isset($this->exception) |
|
287
|
19 |
|
&& is_null($this->data) |
|
288
|
19 |
|
&& !isset($this->pagination) |
|
289
|
19 |
|
&& $this->isEmptyResponse() |
|
290
|
19 |
|
); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* @return bool |
|
295
|
|
|
*/ |
|
296
|
2 |
|
protected function isEmptyResponse() |
|
297
|
|
|
{ |
|
298
|
2 |
|
return !isset($this->response) || $this->response->isEmpty(); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
// @codeCoverageIgnoreStart |
|
302
|
|
|
/** |
|
303
|
|
|
* This is called when an exception is thrown during the response transformation |
|
304
|
|
|
* |
|
305
|
|
|
* @return string |
|
306
|
|
|
*/ |
|
307
|
|
|
public function __toString() |
|
308
|
|
|
{ |
|
309
|
|
|
$data = $this->toArray(); |
|
310
|
|
|
$data['error']['code'] = Error::CODE_REST_API_BUNDLE; |
|
311
|
|
|
|
|
312
|
|
|
return json_encode($data); |
|
313
|
|
|
} |
|
314
|
|
|
// @codeCoverageIgnoreEnd |
|
315
|
|
|
} |
|
316
|
|
|
|