Passed
Push — master ( b704df...28e450 )
by Al3x
10:52
created

ClientManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 201
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A checkResult() 0 6 2
A getAllClients() 0 7 2
A getActiveClients() 0 3 1
A delete() 0 3 1
A __construct() 0 5 1
A restore() 0 3 1
A getClientById() 0 3 1
A findClientsByEmail() 0 3 1
A archive() 0 3 1
A update() 0 3 1
A findClientsByIdNumber() 0 3 1
A createClient() 0 11 1
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\Client;
15
use InvoiceNinjaModule\Model\Interfaces\BaseInterface;
16
use InvoiceNinjaModule\Model\Interfaces\ClientInterface;
17
use InvoiceNinjaModule\Service\Interfaces\ClientManagerInterface;
18
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface;
19
use JetBrains\PhpStorm\Pure;
20
use JsonException;
21
22
/**
23
 * Class ClientManager
24
 */
25
final class ClientManager implements ClientManagerInterface
26
{
27
    private ObjectServiceInterface $objectManager;
28
    private string $reqRoute;
29
    private Client $objectType;
30
31
    /**
32
     * ClientManager constructor.
33
     *
34
     * @param ObjectServiceInterface $objectManager
35
     */
36 14
    #[Pure] public function __construct(ObjectServiceInterface $objectManager)
37
    {
38 14
        $this->reqRoute = '/clients';
39 14
        $this->objectManager = $objectManager;
40 14
        $this->objectType  = new Client();
41
    }
42
43
    /**
44
     * @param ClientInterface $client
45
     *
46
     * @return ClientInterface
47
     * @throws ApiAuthException
48
     * @throws EmptyResponseException
49
     * @throws HttpClientAuthException
50
     * @throws HttpClientException
51
     * @throws InvalidResultException
52
     * @throws JsonException
53
     */
54 1
    public function createClient(ClientInterface $client): ClientInterface
55
    {
56
        //@TODO: Client needs at least one contact!!
57
        /*
58
                    $contact = new Contact();
59
                    $contact->setEmail('[email protected]');
60
                    $contact->setFirstName('dddd');
61
                    $contact->setLastName('XXXXXX');
62
                    $contact->setPrimary(true);
63
                    $contact->setSendInvoice(true); */
64 1
        return $this->checkResult($this->objectManager->createObject($client, $this->reqRoute));
65
    }
66
67
    /**
68
     * @param ClientInterface $client
69
     *
70
     * @return ClientInterface
71
     * @throws ApiAuthException
72
     * @throws EmptyResponseException
73
     * @throws HttpClientAuthException
74
     * @throws InvalidResultException
75
     * @throws HttpClientException
76
     * @throws JsonException
77
     */
78 1
    public function delete(ClientInterface $client): ClientInterface
79
    {
80 1
        return $this->checkResult($this->objectManager->deleteObject($client, $this->reqRoute));
81
    }
82
83
    /**
84
     * @param ClientInterface $client
85
     *
86
     * @return ClientInterface
87
     * @throws ApiAuthException
88
     * @throws EmptyResponseException
89
     * @throws HttpClientAuthException
90
     * @throws HttpClientException
91
     * @throws InvalidResultException
92
     * @throws JsonException
93
     */
94 1
    public function update(ClientInterface $client): ClientInterface
95
    {
96 1
        return $this->checkResult($this->objectManager->updateObject($client, $this->reqRoute));
97
    }
98
99
    /**
100
     * @param ClientInterface $client
101
     *
102
     * @return ClientInterface
103
     * @throws ApiAuthException
104
     * @throws EmptyResponseException
105
     * @throws HttpClientAuthException
106
     * @throws HttpClientException
107
     * @throws InvalidResultException
108
     * @throws JsonException
109
     */
110 1
    public function restore(ClientInterface $client): ClientInterface
111
    {
112 1
        return $this->checkResult($this->objectManager->restoreObject($client, $this->reqRoute));
113
    }
114
115
    /**
116
     * @param ClientInterface $client
117
     *
118
     * @return ClientInterface
119
     * @throws ApiAuthException
120
     * @throws EmptyResponseException
121
     * @throws HttpClientAuthException
122
     * @throws HttpClientException
123
     * @throws InvalidResultException
124
     * @throws JsonException
125
     */
126 1
    public function archive(ClientInterface $client): ClientInterface
127
    {
128 1
        return $this->checkResult($this->objectManager->archiveObject($client, $this->reqRoute));
129
    }
130
131
    /**
132
     * @param string $id
133
     *
134
     * @return ClientInterface
135
     * @throws ApiAuthException
136
     * @throws HttpClientAuthException
137
     * @throws HttpClientException
138
     * @throws InvalidResultException
139
     * @throws JsonException
140
     * @throws NotFoundException
141
     */
142 2
    public function getClientById(string $id): ClientInterface
143
    {
144 2
        return $this->checkResult($this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute));
145
    }
146
147
    /**
148
     * @param string $email
149
     *
150
     * @return ClientInterface[]
151
     * @throws ApiAuthException
152
     * @throws HttpClientAuthException
153
     * @throws HttpClientException
154
     * @throws InvalidParameterException
155
     * @throws InvalidResultException
156
     * @throws JsonException
157
     */
158 1
    public function findClientsByEmail(string $email): array
159
    {
160 1
        return $this->objectManager->findObjectBy($this->objectType, ['email' => $email], $this->reqRoute);
161
    }
162
163
    /**
164
     * @param string $idNumber
165
     *
166
     * @return ClientInterface[]
167
     * @throws ApiAuthException
168
     * @throws HttpClientAuthException
169
     * @throws HttpClientException
170
     * @throws InvalidParameterException
171
     * @throws InvalidResultException
172
     * @throws JsonException
173
     */
174 1
    public function findClientsByIdNumber(string $idNumber): array
175
    {
176 1
        return $this->objectManager->findObjectBy($this->objectType, ['id_number' => $idNumber], $this->reqRoute);
177
    }
178
179
    /**
180
     * @param int $page
181
     * @param int $pageSize
182
     *
183
     * @return ClientInterface[]
184
     * @throws ApiAuthException
185
     * @throws EmptyResponseException
186
     * @throws HttpClientAuthException
187
     * @throws HttpClientException
188
     * @throws InvalidResultException
189
     * @throws JsonException
190
     */
191 3
    public function getAllClients(int $page = 1, int $pageSize = 0): array
192
    {
193 3
        $result = $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize);
194 3
        foreach ($result as $client) {
195 2
            $this->checkResult($client);
196
        }
197 2
        return $result;
198
    }
199
200
    /**
201
     * @return ClientInterface[]
202
     * @throws ApiAuthException
203
     * @throws HttpClientAuthException
204
     * @throws HttpClientException
205
     * @throws InvalidParameterException
206
     * @throws InvalidResultException
207
     * @throws JsonException
208
     */
209
    public function getActiveClients(): array
210
    {
211
        return $this->objectManager->findObjectBy($this->objectType, ['is_deleted' => false], $this->reqRoute);
212
    }
213
214
    /**
215
     * @param BaseInterface $client
216
     *
217
     * @return ClientInterface
218
     * @throws InvalidResultException
219
     */
220 8
    private function checkResult(BaseInterface $client): ClientInterface
221
    {
222 8
        if (!$client instanceof ClientInterface) {
223 1
            throw new InvalidResultException();
224
        }
225 7
        return $client;
226
    }
227
}
228