1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Indigerd\Repository\Rest; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\BadResponseException; |
7
|
|
|
use Indigerd\Hydrator\Hydrator; |
8
|
|
|
use Indigerd\Repository\RepositoryInterface; |
9
|
|
|
use Indigerd\Repository\Rest\Exception\ClientException; |
10
|
|
|
use Indigerd\Repository\Rest\Exception\ServerException; |
11
|
|
|
|
12
|
|
|
class RestRepository implements RepositoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Hydrator |
16
|
|
|
*/ |
17
|
|
|
protected $hydrator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $modelClass; |
28
|
|
|
|
29
|
|
|
protected $endpoint; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $headers; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* RestRepository constructor. |
38
|
|
|
* @param Hydrator $hydrator |
39
|
|
|
* @param Client $client |
40
|
|
|
* @param string $modelClass |
41
|
|
|
* @param string $collectionClass |
|
|
|
|
42
|
|
|
* @param string $endpoint |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
Hydrator $hydrator, |
46
|
|
|
Client $client, |
47
|
|
|
string $modelClass, |
48
|
|
|
string $endpoint |
49
|
|
|
) { |
50
|
|
|
$this->hydrator = $hydrator; |
51
|
|
|
$this->client = $client; |
52
|
|
|
$this->modelClass = $modelClass; |
53
|
|
|
$this->endpoint = $endpoint; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function findOne(array $conditions = [], array $with = []): object |
57
|
|
|
{ |
58
|
|
|
if (!isset($conditions['id'])) { |
59
|
|
|
throw new \InvalidArgumentException('No id provided'); |
60
|
|
|
} |
61
|
|
|
$url = \rtrim($this->endpoint, '/') . '/' . $conditions['id']; |
62
|
|
|
$response = $this->request('get', $url); |
63
|
|
|
return $this->hydrator->hydrate($this->modelClass, $response['body']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function findAll( |
67
|
|
|
array $conditions = [], |
68
|
|
|
array $order = [], |
69
|
|
|
int $limit = 0, |
70
|
|
|
int $offset = 0, |
71
|
|
|
array $with = [] |
72
|
|
|
): array { |
73
|
|
|
$url = \rtrim($this->endpoint, '/'); |
74
|
|
|
$response = $this->request('get', $url, $conditions); |
75
|
|
|
$result = []; |
76
|
|
|
foreach ($response['body'] as $item) { |
77
|
|
|
$result[] = $this->hydrator->hydrate($this->modelClass, $item); |
78
|
|
|
} |
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $expression |
84
|
|
|
* @param array $conditions |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function aggregate(string $expression, array $conditions): string |
88
|
|
|
{ |
89
|
|
|
throw new \RuntimeException('Not implemented'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $field |
94
|
|
|
* @param array $conditions |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function aggregateCount(string $field = '', array $conditions = []): string |
98
|
|
|
{ |
99
|
|
|
throw new \RuntimeException('Not implemented'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $field |
104
|
|
|
* @param array $conditions |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function aggregateSum(string $field, array $conditions): string |
108
|
|
|
{ |
109
|
|
|
throw new \RuntimeException('Not implemented'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $field |
114
|
|
|
* @param array $conditions |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function aggregateAverage(string $field, array $conditions): string |
118
|
|
|
{ |
119
|
|
|
throw new \RuntimeException('Not implemented'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $field |
124
|
|
|
* @param array $conditions |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function aggregateMin(string $field, array $conditions): string |
128
|
|
|
{ |
129
|
|
|
throw new \RuntimeException('Not implemented'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $field |
134
|
|
|
* @param array $conditions |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function aggregateMax(string $field, array $conditions): string |
138
|
|
|
{ |
139
|
|
|
throw new \RuntimeException('Not implemented'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $data |
144
|
|
|
* @return object |
145
|
|
|
*/ |
146
|
|
|
public function create(array $data= []): object |
147
|
|
|
{ |
148
|
|
|
throw new \RuntimeException('Not implemented'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param object $entity |
153
|
|
|
* @param array $data |
154
|
|
|
*/ |
155
|
|
|
public function populate(object $entity, array $data): void |
156
|
|
|
{ |
157
|
|
|
throw new \RuntimeException('Not implemented'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param object $entity |
162
|
|
|
*/ |
163
|
|
|
public function insert(object $entity): void |
164
|
|
|
{ |
165
|
|
|
throw new \RuntimeException('Not implemented'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param object $entity |
170
|
|
|
*/ |
171
|
|
|
public function update(object $entity): void |
172
|
|
|
{ |
173
|
|
|
throw new \RuntimeException('Not implemented'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param object $entity |
178
|
|
|
*/ |
179
|
|
|
public function delete(object $entity): void |
180
|
|
|
{ |
181
|
|
|
throw new \RuntimeException('Not implemented'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $data |
186
|
|
|
* @param array $conditions |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
|
|
public function updateAll(array $data, array $conditions): int |
190
|
|
|
{ |
191
|
|
|
throw new \RuntimeException('Not implemented'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $conditions |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
public function deleteAll(array $conditions): int |
199
|
|
|
{ |
200
|
|
|
throw new \RuntimeException('Not implemented'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $header |
205
|
|
|
* @param string $value |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function addHeader(string $header, string $value) |
209
|
|
|
{ |
210
|
|
|
$this->headers[$header] = $value; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $token |
216
|
|
|
*/ |
217
|
|
|
public function addToken(string $token) |
218
|
|
|
{ |
219
|
|
|
if (!empty($token)) { |
220
|
|
|
$this->addHeader('Authorization', $token); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $method |
226
|
|
|
* @param string $url |
227
|
|
|
* @param array $params |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
|
protected function request(string $method, string $url, array $params = []): array |
231
|
|
|
{ |
232
|
|
|
$this->addHeader('Accept', 'application/json'); |
233
|
|
|
$params['headers'] = $this->headers; |
234
|
|
|
|
235
|
|
|
try { |
236
|
|
|
/** @var \Psr\Http\Message\ResponseInterface $userRequest */ |
237
|
|
|
$userRequest = $this->client->{$method}($url, $params); |
238
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
239
|
|
|
$message = $this->formatResponseExceptionMessage($e); |
240
|
|
|
throw new ClientException($e->getResponse()->getStatusCode(), $message); |
241
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) { |
242
|
|
|
$message = $this->formatResponseExceptionMessage($e); |
243
|
|
|
throw new ServerException($message, $e->getResponse()->getStatusCode()); |
244
|
|
|
} catch (\Exception $e) { |
245
|
|
|
$message = \sprintf('Failed to to perform request to service (%s).', $e->getMessage()); |
246
|
|
|
throw new ServerException($message, $e->getCode()); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$data = \json_decode($userRequest->getBody()->getContents(), true); |
250
|
|
|
|
251
|
|
|
return [ |
252
|
|
|
'headers' => $userRequest->getHeaders(), |
253
|
|
|
'body' => $data |
254
|
|
|
]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param BadResponseException $e |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
private function formatResponseExceptionMessage(BadResponseException $e): string |
262
|
|
|
{ |
263
|
|
|
$message = \sprintf( |
264
|
|
|
'Service responded with error (%s - %s).', |
265
|
|
|
$e->getResponse()->getStatusCode(), |
266
|
|
|
$e->getResponse()->getReasonPhrase() |
267
|
|
|
); |
268
|
|
|
$message .= "\n" . $e->getResponse()->getBody()->getContents(); |
269
|
|
|
|
270
|
|
|
return $message; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param array $response |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
public function generateHeaders(array $response): array |
278
|
|
|
{ |
279
|
|
|
$headers = []; |
280
|
|
|
foreach ($this->headers as $header => $value) { |
281
|
|
|
if (isset($response[$header][0])) { |
282
|
|
|
$headers[$value] = $response[$header]; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
return $headers; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.