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