1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace InvoiceNinjaModule\Service; |
6
|
|
|
|
7
|
|
|
use InvoiceNinjaModule\Exception\ApiAuthException; |
8
|
|
|
use InvoiceNinjaModule\Exception\EmptyResponseException; |
9
|
|
|
use InvoiceNinjaModule\Exception\HttpClientAuthException; |
10
|
|
|
use InvoiceNinjaModule\Exception\HttpClientException; |
11
|
|
|
use InvoiceNinjaModule\Exception\InvalidParameterException; |
12
|
|
|
use InvoiceNinjaModule\Exception\InvalidResultException; |
13
|
|
|
use InvoiceNinjaModule\Exception\NotFoundException; |
14
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
15
|
|
|
use InvoiceNinjaModule\Options\RequestOptions; |
16
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
17
|
|
|
use InvoiceNinjaModule\Service\Interfaces\RequestServiceInterface; |
18
|
|
|
use JsonException; |
19
|
|
|
use Laminas\Http\Request; |
20
|
|
|
use Laminas\Hydrator\HydratorInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ObjectService |
24
|
|
|
*/ |
25
|
|
|
final class ObjectService implements ObjectServiceInterface |
26
|
|
|
{ |
27
|
|
|
protected RequestServiceInterface $requestService; |
28
|
|
|
protected HydratorInterface $hydrator; |
29
|
|
|
protected BaseInterface $objectType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* BaseManager constructor. |
33
|
|
|
* |
34
|
|
|
* @param RequestServiceInterface $requestService |
35
|
|
|
* @param HydratorInterface $hydrator |
36
|
|
|
*/ |
37
|
19 |
|
public function __construct(RequestServiceInterface $requestService, HydratorInterface $hydrator) |
38
|
|
|
{ |
39
|
19 |
|
$this->requestService = $requestService; |
40
|
19 |
|
$this->hydrator = $hydrator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param BaseInterface $object |
45
|
|
|
* @param string $reqRoute |
46
|
|
|
* |
47
|
|
|
* @return BaseInterface |
48
|
|
|
* @throws ApiAuthException |
49
|
|
|
* @throws EmptyResponseException |
50
|
|
|
* @throws HttpClientAuthException |
51
|
|
|
* @throws InvalidResultException |
52
|
|
|
* @throws HttpClientException |
53
|
|
|
* @throws JsonException |
54
|
|
|
*/ |
55
|
1 |
|
public function createObject(BaseInterface $object, string $reqRoute): BaseInterface |
56
|
|
|
{ |
57
|
1 |
|
$reqOptions = new RequestOptions(); |
58
|
1 |
|
$reqOptions->addPostParameters($this->hydrator->extract($object)); |
59
|
1 |
|
$responseArr = $this->requestService->dispatchRequest(Request::METHOD_POST, $reqRoute, $reqOptions); |
60
|
1 |
|
return $this->hydrateObject($responseArr, $object); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param BaseInterface $object |
65
|
|
|
* @param string $id |
66
|
|
|
* @param string $reqRoute |
67
|
|
|
* |
68
|
|
|
* @return BaseInterface |
69
|
|
|
* @throws ApiAuthException |
70
|
|
|
* @throws HttpClientAuthException |
71
|
|
|
* @throws HttpClientException |
72
|
|
|
* @throws InvalidResultException |
73
|
|
|
* @throws JsonException |
74
|
|
|
* @throws NotFoundException |
75
|
|
|
*/ |
76
|
2 |
|
public function getObjectById(BaseInterface $object, string $id, string $reqRoute): BaseInterface |
77
|
|
|
{ |
78
|
2 |
|
$requestOptions = new RequestOptions(); |
79
|
|
|
|
80
|
|
|
try { |
81
|
2 |
|
$responseArr = $this->requestService->dispatchRequest( |
82
|
|
|
Request::METHOD_GET, |
83
|
2 |
|
$reqRoute . '/' . $id, |
84
|
|
|
$requestOptions |
85
|
|
|
); |
86
|
1 |
|
} catch (EmptyResponseException) { |
87
|
1 |
|
throw new NotFoundException($id); |
88
|
|
|
} |
89
|
1 |
|
return $this->hydrateObject($responseArr, $object); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param BaseInterface $object |
94
|
|
|
* @param array $searchTerm |
95
|
|
|
* @param string $reqRoute |
96
|
|
|
* |
97
|
|
|
* @return BaseInterface[] |
98
|
|
|
* @throws ApiAuthException |
99
|
|
|
* @throws HttpClientAuthException |
100
|
|
|
* @throws HttpClientException |
101
|
|
|
* @throws InvalidParameterException |
102
|
|
|
* @throws InvalidResultException |
103
|
|
|
* @throws JsonException |
104
|
|
|
*/ |
105
|
5 |
|
public function findObjectBy(BaseInterface $object, array $searchTerm, string $reqRoute): array |
106
|
|
|
{ |
107
|
5 |
|
$resultArr = []; |
108
|
5 |
|
$reqOptions = new RequestOptions(); |
109
|
|
|
|
110
|
5 |
|
if (empty($searchTerm)) { |
111
|
1 |
|
throw new InvalidParameterException('searchTerm must not be empty'); |
112
|
|
|
} |
113
|
4 |
|
$reqOptions->addQueryParameters($searchTerm); |
114
|
|
|
|
115
|
|
|
try { |
116
|
4 |
|
$responseArr = $this->requestService->dispatchRequest(Request::METHOD_GET, $reqRoute, $reqOptions); |
117
|
|
|
|
118
|
2 |
|
foreach ($responseArr as $objectArr) { |
119
|
1 |
|
$resultArr[] = $this->hydrateObject($objectArr, clone $object); |
120
|
|
|
} |
121
|
2 |
|
} catch (EmptyResponseException) { |
122
|
2 |
|
return $resultArr; |
123
|
|
|
} |
124
|
2 |
|
return $resultArr; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param BaseInterface $object |
129
|
|
|
* @param string $reqRoute |
130
|
|
|
* |
131
|
|
|
* @return BaseInterface |
132
|
|
|
* @throws ApiAuthException |
133
|
|
|
* @throws EmptyResponseException |
134
|
|
|
* @throws HttpClientAuthException |
135
|
|
|
* @throws HttpClientException |
136
|
|
|
* @throws InvalidResultException |
137
|
|
|
* @throws JsonException |
138
|
|
|
*/ |
139
|
1 |
|
public function restoreObject(BaseInterface $object, string $reqRoute): BaseInterface |
140
|
|
|
{ |
141
|
1 |
|
return $this->update($object, $reqRoute, ObjectServiceInterface::ACTION_RESTORE); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param BaseInterface $object |
146
|
|
|
* @param string $reqRoute |
147
|
|
|
* |
148
|
|
|
* @return BaseInterface |
149
|
|
|
* @throws ApiAuthException |
150
|
|
|
* @throws EmptyResponseException |
151
|
|
|
* @throws HttpClientAuthException |
152
|
|
|
* @throws HttpClientException |
153
|
|
|
* @throws InvalidResultException |
154
|
|
|
* @throws JsonException |
155
|
|
|
*/ |
156
|
1 |
|
public function archiveObject(BaseInterface $object, string $reqRoute): BaseInterface |
157
|
|
|
{ |
158
|
1 |
|
return $this->update($object, $reqRoute, ObjectServiceInterface::ACTION_ARCHIVE); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param BaseInterface $object |
163
|
|
|
* @param string $reqRoute |
164
|
|
|
* |
165
|
|
|
* @return BaseInterface |
166
|
|
|
* @throws ApiAuthException |
167
|
|
|
* @throws EmptyResponseException |
168
|
|
|
* @throws HttpClientAuthException |
169
|
|
|
* @throws HttpClientException |
170
|
|
|
* @throws InvalidResultException |
171
|
|
|
* @throws JsonException |
172
|
|
|
*/ |
173
|
1 |
|
public function updateObject(BaseInterface $object, string $reqRoute): BaseInterface |
174
|
|
|
{ |
175
|
1 |
|
return $this->update($object, $reqRoute); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param BaseInterface $object |
180
|
|
|
* @param string $reqRoute |
181
|
|
|
* @param string|null $action |
182
|
|
|
* |
183
|
|
|
* @return BaseInterface |
184
|
|
|
* @throws ApiAuthException |
185
|
|
|
* @throws EmptyResponseException |
186
|
|
|
* @throws HttpClientAuthException |
187
|
|
|
* @throws HttpClientException |
188
|
|
|
* @throws InvalidResultException |
189
|
|
|
* @throws JsonException |
190
|
|
|
*/ |
191
|
3 |
|
private function update(BaseInterface $object, string $reqRoute, ?string $action = null): BaseInterface |
192
|
|
|
{ |
193
|
3 |
|
$reqOptions = new RequestOptions(); |
194
|
|
|
|
195
|
3 |
|
if ($action !== null) { |
196
|
2 |
|
$reqOptions->addQueryParameters(['action' => $action]); |
197
|
|
|
} else { |
198
|
1 |
|
$reqOptions->addPostParameters($this->hydrator->extract($object)); |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
$responseArr = $this->requestService->dispatchRequest( |
202
|
|
|
Request::METHOD_PUT, |
203
|
3 |
|
$reqRoute . '/' . $object->getId(), |
204
|
|
|
$reqOptions |
205
|
|
|
); |
206
|
3 |
|
return $this->hydrateObject($responseArr, $object); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param BaseInterface $object |
211
|
|
|
* @param string $reqRoute |
212
|
|
|
* |
213
|
|
|
* @return BaseInterface |
214
|
|
|
* @throws ApiAuthException |
215
|
|
|
* @throws EmptyResponseException |
216
|
|
|
* @throws HttpClientAuthException |
217
|
|
|
* @throws HttpClientException |
218
|
|
|
* @throws InvalidResultException |
219
|
|
|
* @throws JsonException |
220
|
|
|
*/ |
221
|
1 |
|
public function deleteObject(BaseInterface $object, string $reqRoute): BaseInterface |
222
|
|
|
{ |
223
|
1 |
|
$responseArr = $this->requestService->dispatchRequest( |
224
|
|
|
Request::METHOD_DELETE, |
225
|
1 |
|
$reqRoute . '/' . $object->getId(), |
226
|
1 |
|
new RequestOptions() |
227
|
|
|
); |
228
|
1 |
|
return $this->hydrateObject($responseArr, $object); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Retrieves all objects. |
233
|
|
|
* Default page size on server side is 15! |
234
|
|
|
* |
235
|
|
|
* @param BaseInterface $object |
236
|
|
|
* @param string $reqRoute |
237
|
|
|
* @param int $page |
238
|
|
|
* @param int $pageSize |
239
|
|
|
* |
240
|
|
|
* @return BaseInterface[] |
241
|
|
|
* @throws ApiAuthException |
242
|
|
|
* @throws EmptyResponseException |
243
|
|
|
* @throws HttpClientAuthException |
244
|
|
|
* @throws HttpClientException |
245
|
|
|
* @throws InvalidResultException |
246
|
|
|
* @throws JsonException |
247
|
|
|
*/ |
248
|
3 |
|
public function getAllObjects(BaseInterface $object, string $reqRoute, int $page = 1, int $pageSize = 0): array |
249
|
|
|
{ |
250
|
3 |
|
$reqOptions = new RequestOptions(); |
251
|
3 |
|
$reqOptions->setPage($page); |
252
|
3 |
|
$reqOptions->setPageSize($pageSize); |
253
|
|
|
|
254
|
3 |
|
$responseArr = $this->requestService->dispatchRequest(Request::METHOD_GET, $reqRoute, $reqOptions); |
255
|
|
|
|
256
|
3 |
|
$result = []; |
257
|
3 |
|
foreach ($responseArr as $clientData) { |
258
|
2 |
|
$result[] = $this->hydrateObject($clientData, clone $object); |
259
|
|
|
} |
260
|
2 |
|
return $result; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $invitationKey |
265
|
|
|
* @param string $topic |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
* @throws ApiAuthException |
269
|
|
|
* @throws EmptyResponseException |
270
|
|
|
* @throws HttpClientAuthException |
271
|
|
|
* @throws HttpClientException |
272
|
|
|
* @throws JsonException |
273
|
|
|
*/ |
274
|
1 |
|
public function downloadFile(string $invitationKey, string $topic): array |
275
|
|
|
{ |
276
|
1 |
|
return $this->requestService->dispatchRequest( |
277
|
|
|
Request::METHOD_GET, |
278
|
1 |
|
'/' . $topic . '/' . $invitationKey . '/download', |
279
|
1 |
|
new RequestOptions() |
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $command |
285
|
|
|
* @param string $id |
286
|
|
|
* @param string $reqRoute |
287
|
|
|
* |
288
|
|
|
* @throws ApiAuthException |
289
|
|
|
* @throws EmptyResponseException |
290
|
|
|
* @throws HttpClientAuthException |
291
|
|
|
* @throws HttpClientException |
292
|
|
|
* @throws JsonException |
293
|
|
|
*/ |
294
|
1 |
|
public function sendCommand(string $command, string $id, string $reqRoute): void |
295
|
|
|
{ |
296
|
1 |
|
$reqOptions = new RequestOptions(); |
297
|
1 |
|
$this->requestService->dispatchRequest( |
298
|
|
|
Request::METHOD_GET, |
299
|
1 |
|
$reqRoute . '/' . $id . '/' . $command, |
300
|
|
|
$reqOptions |
301
|
|
|
); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param string $command |
306
|
|
|
* @param array $ids |
307
|
|
|
* @param string $reqRoute |
308
|
|
|
* |
309
|
|
|
* @throws ApiAuthException |
310
|
|
|
* @throws EmptyResponseException |
311
|
|
|
* @throws HttpClientAuthException |
312
|
|
|
* @throws HttpClientException |
313
|
|
|
* @throws JsonException |
314
|
|
|
*/ |
315
|
|
|
public function sendBulkCommand(string $command, array $ids, string $reqRoute): void |
316
|
|
|
{ |
317
|
|
|
$reqOptions = new RequestOptions(); |
318
|
|
|
$reqOptions->addPostParameters([ |
319
|
|
|
'ids' => $ids, |
320
|
|
|
'action' => $command |
321
|
|
|
]); |
322
|
|
|
|
323
|
|
|
$this->requestService->dispatchRequest( |
324
|
|
|
Request::METHOD_POST, |
325
|
|
|
$reqRoute . '/' . 'bulk', |
326
|
|
|
$reqOptions |
327
|
|
|
); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @param array $data |
332
|
|
|
* @param BaseInterface $object |
333
|
|
|
* |
334
|
|
|
* @return BaseInterface |
335
|
|
|
* @throws InvalidResultException |
336
|
|
|
*/ |
337
|
9 |
|
private function hydrateObject(array $data, BaseInterface $object): BaseInterface |
338
|
|
|
{ |
339
|
9 |
|
$result = $this->hydrator->hydrate($data, $object); |
340
|
9 |
|
if ($result instanceof BaseInterface) { |
341
|
8 |
|
return $result; |
342
|
|
|
} |
343
|
1 |
|
throw new InvalidResultException(); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|