Completed
Push — master ( 1d87d0...2a331f )
by Al3x
04:12
created

ObjectService   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.51%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 0
loc 246
ccs 66
cts 67
cp 0.9851
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createObject() 0 7 1
A getObjectById() 0 12 2
A findObjectBy() 0 21 4
A restoreObject() 0 4 1
A archiveObject() 0 4 1
A updateObject() 0 4 1
A update() 0 17 2
A deleteObject() 0 9 1
A getAllObjects() 0 14 2
A downloadFile() 0 8 1
A hydrateObject() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace InvoiceNinjaModule\Service;
5
6
use InvoiceNinjaModule\Exception\ApiException;
7
use InvoiceNinjaModule\Exception\EmptyResponseException;
8
use InvoiceNinjaModule\Exception\InvalidParameterException;
9
use InvoiceNinjaModule\Exception\InvalidResultException;
10
use InvoiceNinjaModule\Exception\NotFoundException;
11
use InvoiceNinjaModule\Model\Interfaces\BaseInterface;
12
use InvoiceNinjaModule\Model\RequestOptions;
13
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface;
14
use InvoiceNinjaModule\Service\Interfaces\RequestServiceInterface;
15
use Zend\Http\Request;
16
use Zend\Hydrator\HydratorInterface;
17
18
/**
19
 * Class ObjectService
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 17
    public function __construct(RequestServiceInterface $requestService, HydratorInterface $hydrator)
41
    {
42 17
        $this->requestService = $requestService;
43 17
        $this->hydrator       = $hydrator;
44 17
    }
45
46
    /**
47
     * @param BaseInterface $object
48
     * @param               $reqRoute
49
     *
50
     * @return BaseInterface
51
     * @throws ApiException
52
     * @throws EmptyResponseException
53
     * @throws InvalidResultException
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 int           $id
66
     * @param string        $reqRoute
67
     *
68
     * @return BaseInterface
69
     * @throws EmptyResponseException
70
     * @throws NotFoundException
71
     * @throws InvalidResultException
72
     */
73 2
    public function getObjectById(BaseInterface $object, int $id, string $reqRoute) :BaseInterface
74
    {
75 2
        $reop = new RequestOptions();
76
        //$reop->setInclude('invoices');
77
78
        try {
79 2
            $responseArr = $this->requestService->dispatchRequest(Request::METHOD_GET, $reqRoute.'/'.$id, $reop);
80 1
        } catch (ApiException $e) {
81 1
            throw new NotFoundException($id);
82
        }
83 1
        return $this->hydrateObject($responseArr, $object);
84
    }
85
86
    /**
87
     * @param BaseInterface $object
88
     * @param array         $searchTerm
89
     * @param string        $reqRoute
90
     *
91
     * @return BaseInterface[]
92
     * @throws ApiException
93
     * @throws InvalidParameterException
94
     * @throws InvalidResultException
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 2
                $resultArr[] = $this->hydrateObject($objectArr, clone $object);
111
            }
112 2
        } catch (EmptyResponseException $e) {
113 1
            return $resultArr;
114
        }
115 2
        return $resultArr;
116
    }
117
118
    /**
119
     * @param BaseInterface $object
120
     * @param string        $reqRoute
121
     *
122
     * @return BaseInterface
123
     * @throws ApiException
124
     * @throws EmptyResponseException
125
     * @throws InvalidResultException
126
     */
127 1
    public function restoreObject(BaseInterface $object, string $reqRoute) :BaseInterface
128
    {
129 1
        return $this->update($object, $reqRoute, self::ACTION_RESTORE);
130
    }
131
132
    /**
133
     * @param BaseInterface $object
134
     * @param string        $reqRoute
135
     *
136
     * @return BaseInterface
137
     * @throws ApiException
138
     * @throws EmptyResponseException
139
     * @throws InvalidResultException
140
     */
141 1
    public function archiveObject(BaseInterface $object, string $reqRoute) :BaseInterface
142
    {
143 1
        return $this->update($object, $reqRoute, self::ACTION_ARCHIVE);
144
    }
145
146
    /**
147
     * @param BaseInterface $object
148
     * @param string        $reqRoute
149
     *
150
     * @return BaseInterface
151
     * @throws ApiException
152
     * @throws EmptyResponseException
153
     * @throws InvalidResultException
154
     */
155 1
    public function updateObject(BaseInterface $object, string $reqRoute) :BaseInterface
156
    {
157 1
        return $this->update($object, $reqRoute);
158
    }
159
160
    /**
161
     * @param BaseInterface $object
162
     * @param string        $reqRoute
163
     * @param string|null   $action
164
     *
165
     * @return BaseInterface
166
     * @throws ApiException
167
     * @throws EmptyResponseException
168
     * @throws InvalidResultException
169
     */
170 3
    private function update(BaseInterface $object, $reqRoute, ?string $action = null) :BaseInterface
171
    {
172 3
        $reqOptions = new RequestOptions();
173
174 3
        if ($action !== null) {
175 2
            $reqOptions->addQueryParameters(['action' => $action]);
176
        } else {
177 1
            $reqOptions->addPostParameters($this->hydrator->extract($object));
178
        }
179
180 3
        $responseArr = $this->requestService->dispatchRequest(
181 3
            Request::METHOD_PUT,
182 3
            $reqRoute.'/'.$object->getId(),
183
            $reqOptions
184
        );
185 3
        return $this->hydrateObject($responseArr, $object);
186
    }
187
188
    /**
189
     * @param BaseInterface $object
190
     * @param string        $reqRoute
191
     *
192
     * @return BaseInterface
193
     * @throws ApiException
194
     * @throws EmptyResponseException
195
     * @throws InvalidResultException
196
     */
197 1
    public function deleteObject(BaseInterface $object, string $reqRoute) :BaseInterface
198
    {
199 1
        $responseArr = $this->requestService->dispatchRequest(
200 1
            Request::METHOD_DELETE,
201 1
            $reqRoute.'/'.$object->getId(),
202 1
            new RequestOptions()
203
        );
204 1
        return $this->hydrateObject($responseArr, $object);
205
    }
206
207
    /**
208
     * Retrieves all objects.
209
     * Default page size on server side is 15!
210
     * @param BaseInterface $object
211
     * @param string        $reqRoute
212
     * @param int           $page
213
     * @param int           $pageSize
214
     *
215
     * @return BaseInterface[]
216
     * @throws ApiException
217
     * @throws EmptyResponseException
218
     * @throws InvalidResultException
219
     */
220 2
    public function getAllObjects(BaseInterface $object, string $reqRoute, int $page = 1, int $pageSize = 0) :array
221
    {
222 2
        $reqOptions = new RequestOptions();
223 2
        $reqOptions->setPage($page);
224 2
        $reqOptions->setPageSize($pageSize);
225
226 2
        $responseArr = $this->requestService->dispatchRequest(Request::METHOD_GET, $reqRoute, $reqOptions);
227
228 2
        $result = [];
229 2
        foreach ($responseArr as $clientData) {
230 1
            $result[] = $this->hydrateObject($clientData, clone $object);
231
        }
232 2
        return $result;
233
    }
234
235
    /**
236
     * @param int $id
237
     *
238
     * @return array
239
     * @throws ApiException
240
     * @throws EmptyResponseException
241
     */
242 1
    public function downloadFile(int $id) :array
243
    {
244 1
        return $this->requestService->dispatchRequest(
245 1
            Request::METHOD_GET,
246 1
            self::ROUTE_DOWNLOAD.'/'.$id,
247 1
            new RequestOptions()
248
        );
249
    }
250
251
    /**
252
     * @param array         $data
253
     * @param BaseInterface $object
254
     *
255
     * @return BaseInterface
256
     * @throws InvalidResultException
257
     */
258 8
    private function hydrateObject(array $data, BaseInterface $object) :BaseInterface
259
    {
260 8
        $result = $this->hydrator->hydrate($data, $object);
261 8
        if ($result instanceof BaseInterface) {
262 8
            return $result;
263
        }
264
        throw new InvalidResultException();
265
    }
266
}
267