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