Passed
Push — master ( de12e2...4347a5 )
by Al3x
02:17 queued 13s
created

ObjectService::sendBulkCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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