|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace InvoiceNinjaModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use InvoiceNinjaModule\Exception\InvalidResultException; |
|
6
|
|
|
use InvoiceNinjaModule\Model\Client; |
|
7
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
|
8
|
|
|
use InvoiceNinjaModule\Model\Interfaces\ClientInterface; |
|
9
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ClientManagerInterface; |
|
10
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class ClientManager |
|
14
|
|
|
* |
|
15
|
|
|
* @package InvoiceNinjaModule\Service |
|
16
|
|
|
*/ |
|
17
|
|
|
class ClientManager implements ClientManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ObjectServiceInterface */ |
|
20
|
|
|
private $objectManager; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $reqRoute; |
|
23
|
|
|
/** @var Client */ |
|
24
|
|
|
private $objectType; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(ObjectServiceInterface $objectManager) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->reqRoute = '/clients'; |
|
29
|
|
|
$this->objectManager = $objectManager; |
|
30
|
|
|
$this->objectType = new Client(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function createClient(ClientInterface $client) :ClientInterface |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->checkResult($this->objectManager->createObject($client, $this->reqRoute)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function delete(ClientInterface $client) :ClientInterface |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->checkResult($this->objectManager->deleteObject($client, $this->reqRoute)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function update(ClientInterface $client) :ClientInterface |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->checkResult($this->objectManager->updateObject($client, $this->reqRoute)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function restore(ClientInterface $client) :ClientInterface |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->checkResult($this->objectManager->restoreObject($client, $this->reqRoute)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function archive(ClientInterface $client) :ClientInterface |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->checkResult($this->objectManager->archiveObject($client, $this->reqRoute)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getClientById($id) :ClientInterface |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->checkResult($this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function findClientsByEmail($email) :array |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->objectManager->findObjectBy($this->objectType, ['email' => $email], $this->reqRoute); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function findClientsByIdNumber($idNumber) :array |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->objectManager->findObjectBy($this->objectType, ['id_number' => $idNumber], $this->reqRoute); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getAllClients($page = 1, $pageSize = 0) :array |
|
74
|
|
|
{ |
|
75
|
|
|
$result = $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize); |
|
76
|
|
|
foreach ($result as $client) { |
|
77
|
|
|
$this->checkResult($client); |
|
78
|
|
|
} |
|
79
|
|
|
return $result; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param BaseInterface $client |
|
84
|
|
|
* |
|
85
|
|
|
* @return ClientInterface |
|
86
|
|
|
* @throws InvalidResultException |
|
87
|
|
|
*/ |
|
88
|
|
|
private function checkResult(BaseInterface $client) :ClientInterface |
|
89
|
|
|
{ |
|
90
|
|
|
if (!$client instanceof ClientInterface) { |
|
91
|
|
|
throw new InvalidResultException(); |
|
92
|
|
|
} |
|
93
|
|
|
return $client; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|