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