|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MediaMonks\RestApi\Model; |
|
4
|
|
|
|
|
5
|
|
|
use MediaMonks\RestApi\Exception\AbstractValidationException; |
|
6
|
|
|
use MediaMonks\RestApi\Response\ExtendedResponseInterface; |
|
7
|
|
|
use MediaMonks\RestApi\Response\PaginatedResponseInterface; |
|
8
|
|
|
use MediaMonks\RestApi\Util\StringUtil; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
10
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractResponseModel |
|
13
|
|
|
{ |
|
14
|
|
|
protected int $statusCode = Response::HTTP_OK; |
|
15
|
|
|
|
|
16
|
|
|
protected bool $returnStatusCode = false; |
|
17
|
|
|
|
|
18
|
|
|
protected bool $returnStackTrace = false; |
|
19
|
|
|
|
|
20
|
|
|
protected mixed $data = null; |
|
21
|
|
|
|
|
22
|
|
|
protected ?Response $response = null; |
|
23
|
|
|
|
|
24
|
|
|
protected ?ExtendedResponseInterface $extendedResponse = null; |
|
25
|
|
|
|
|
26
|
|
|
protected ?\Throwable $throwable = null; |
|
27
|
|
|
|
|
28
|
|
|
protected ?PaginatedResponseInterface $pagination = null; |
|
29
|
|
|
|
|
30
|
|
|
public function isReturnStackTrace(): bool |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->returnStackTrace; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setReturnStackTrace(bool $returnStackTrace): ResponseModelInterface |
|
36
|
|
|
{ |
|
37
|
|
|
$this->returnStackTrace = $returnStackTrace; |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getStatusCode(): int |
|
43
|
|
|
{ |
|
44
|
|
|
if (isset($this->throwable)) { |
|
45
|
|
|
return $this->getExceptionStatusCode(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if ($this->isEmpty()) { |
|
49
|
|
|
return Response::HTTP_NO_CONTENT; |
|
50
|
|
|
} |
|
51
|
5 |
|
|
|
52
|
|
|
return $this->statusCode; |
|
53
|
5 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function getExceptionStatusCode(): int |
|
56
|
|
|
{ |
|
57
|
|
|
if ($this->throwable instanceof HttpException) { |
|
58
|
|
|
return $this->throwable->getStatusCode(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
3 |
|
if ($this->throwable instanceof AbstractValidationException) { |
|
61
|
|
|
return Response::HTTP_BAD_REQUEST; |
|
62
|
3 |
|
} |
|
63
|
|
|
if ($this->isValidHttpStatusCode($this->throwable->getCode())) { |
|
64
|
3 |
|
return $this->throwable->getCode(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return Response::HTTP_INTERNAL_SERVER_ERROR; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
14 |
|
protected function isValidHttpStatusCode(int $code): bool |
|
71
|
|
|
{ |
|
72
|
14 |
|
return array_key_exists($code, Response::$statusTexts) && $code >= Response::HTTP_BAD_REQUEST; |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
protected function getThrowableErrorCode(string $errorCode, ?string $trim = null): string |
|
76
|
2 |
|
{ |
|
77
|
|
|
return sprintf($errorCode, StringUtil::classToSnakeCase($this->throwable, $trim)); |
|
78
|
|
|
} |
|
79
|
8 |
|
|
|
80
|
|
|
protected function getThrowableStackTrace(): array |
|
81
|
|
|
{ |
|
82
|
|
|
$traces = []; |
|
83
|
|
|
foreach ($this->throwable->getTrace() as $trace) { |
|
84
|
|
|
// Since PHP 7.4 the args key got disabled, to enable it again: |
|
85
|
4 |
|
// zend.exception_ignore_args = On |
|
86
|
|
|
if (array_key_exists('args', $trace)) { |
|
87
|
4 |
|
$trace['args'] = json_decode(json_encode($trace['args']), true); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
3 |
|
$traces[] = $trace; |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
return $traces; |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function setStatusCode(int $statusCode): ResponseModelInterface |
|
97
|
1 |
|
{ |
|
98
|
|
|
$this->statusCode = $statusCode; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getReturnStatusCode(): bool |
|
104
|
2 |
|
{ |
|
105
|
|
|
return $this->returnStatusCode; |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setReturnStatusCode(bool $returnStatusCode): ResponseModelInterface |
|
109
|
|
|
{ |
|
110
|
|
|
$this->returnStatusCode = $returnStatusCode; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
4 |
|
|
|
115
|
|
|
public function getData(): mixed |
|
116
|
4 |
|
{ |
|
117
|
|
|
return $this->data; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function setData(mixed $data): ResponseModelInterface |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->data = $data; |
|
123
|
|
|
|
|
124
|
1 |
|
return $this; |
|
|
|
|
|
|
125
|
1 |
|
} |
|
126
|
1 |
|
|
|
127
|
1 |
|
public function getThrowable(): ?\Throwable |
|
128
|
1 |
|
{ |
|
129
|
|
|
return $this->throwable; |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function setThrowable(\Throwable $throwable): ResponseModelInterface |
|
133
|
|
|
{ |
|
134
|
|
|
$this->throwable = $throwable; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
|
|
|
|
|
137
|
6 |
|
} |
|
138
|
|
|
|
|
139
|
6 |
|
public function getPagination(): ?PaginatedResponseInterface |
|
140
|
|
|
{ |
|
141
|
6 |
|
return $this->pagination; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function setPagination(PaginatedResponseInterface $pagination): ResponseModelInterface |
|
145
|
|
|
{ |
|
146
|
|
|
$this->pagination = $pagination; |
|
147
|
10 |
|
$this->setData($pagination->getData()); |
|
148
|
|
|
|
|
149
|
10 |
|
return $this; |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function getResponse(): ?Response |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->response; |
|
155
|
|
|
} |
|
156
|
3 |
|
|
|
157
|
|
|
public function setResponse(Response $response): ResponseModelInterface |
|
158
|
3 |
|
{ |
|
159
|
|
|
$this->response = $response; |
|
160
|
3 |
|
$this->setStatusCode($response->getStatusCode()); |
|
161
|
|
|
$this->setData($response->getContent()); |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
10 |
|
/** |
|
167
|
|
|
* @return ExtendedResponseInterface |
|
168
|
10 |
|
*/ |
|
169
|
|
|
public function getExtendedResponse() |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->extendedResponse; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
12 |
|
* @param ExtendedResponseInterface $response |
|
176
|
|
|
* @return $this |
|
177
|
12 |
|
*/ |
|
178
|
|
|
public function setExtendedResponse(ExtendedResponseInterface $response): ResponseModelInterface |
|
179
|
12 |
|
{ |
|
180
|
|
|
$this->extendedResponse = $response; |
|
181
|
|
|
$this->setStatusCode($response->getStatusCode()); |
|
|
|
|
|
|
182
|
|
|
$this->setData($response->getCustomContent()); |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
|
|
|
|
|
185
|
11 |
|
} |
|
186
|
|
|
|
|
187
|
11 |
|
public function isEmpty(): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return ( |
|
190
|
|
|
!isset($this->throwable) |
|
191
|
|
|
&& is_null($this->data) |
|
192
|
|
|
&& !isset($this->pagination) |
|
193
|
|
|
&& $this->isEmptyResponse() |
|
194
|
8 |
|
&& $this->isEmptyExtendedResponse() |
|
195
|
|
|
); |
|
196
|
8 |
|
} |
|
197
|
|
|
|
|
198
|
8 |
|
protected function isEmptyResponse(): bool |
|
199
|
|
|
{ |
|
200
|
|
|
return (!isset($this->response) || $this->response->isEmpty()); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
protected function isEmptyExtendedResponse(): bool |
|
204
|
11 |
|
{ |
|
205
|
|
|
return (!isset($this->extendedResponse) || $this->extendedResponse->isEmpty()); |
|
|
|
|
|
|
206
|
11 |
|
} |
|
207
|
|
|
|
|
208
|
|
|
// @codeCoverageIgnoreStart |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* This is called when an exception is thrown during the response transformation |
|
212
|
|
|
*/ |
|
213
|
2 |
|
public function __toString(): string |
|
214
|
|
|
{ |
|
215
|
2 |
|
return json_encode(get_object_vars($this)); |
|
216
|
2 |
|
} |
|
217
|
|
|
// @codeCoverageIgnoreEnd |
|
218
|
|
|
} |
|
219
|
|
|
|