1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModule\Service; |
5
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Exception\InvalidResultException; |
7
|
|
|
use InvoiceNinjaModule\Exception\NotFoundException; |
8
|
|
|
use InvoiceNinjaModule\Model\Interfaces\BaseInterface; |
9
|
|
|
use InvoiceNinjaModule\Model\Interfaces\InvoiceInterface; |
10
|
|
|
use InvoiceNinjaModule\Model\Invoice; |
11
|
|
|
use InvoiceNinjaModule\Service\Interfaces\InvoiceManagerInterface; |
12
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class InvoiceManager |
16
|
|
|
*/ |
17
|
|
|
class InvoiceManager implements InvoiceManagerInterface |
18
|
|
|
{ |
19
|
|
|
/** @var ObjectServiceInterface */ |
20
|
|
|
private $objectManager; |
21
|
|
|
/** @var string */ |
22
|
|
|
private $reqRoute; |
23
|
|
|
/** @var Invoice */ |
24
|
|
|
private $objectType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* InvoiceManager constructor. |
28
|
|
|
* |
29
|
|
|
* @param ObjectServiceInterface $objectManager |
30
|
|
|
*/ |
31
|
16 |
|
public function __construct(ObjectServiceInterface $objectManager) |
32
|
|
|
{ |
33
|
16 |
|
$this->objectManager = $objectManager; |
34
|
16 |
|
$this->reqRoute = '/invoices'; |
35
|
16 |
|
$this->objectType = new Invoice(); |
36
|
16 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param InvoiceInterface $invoice |
40
|
|
|
* |
41
|
|
|
* @return InvoiceInterface |
42
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
43
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
44
|
|
|
* @throws InvalidResultException |
45
|
|
|
*/ |
46
|
1 |
|
public function createInvoice(InvoiceInterface $invoice) :InvoiceInterface |
47
|
|
|
{ |
48
|
1 |
|
return $this->checkResult($this->objectManager->createObject($invoice, $this->reqRoute)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InvoiceInterface $invoice |
53
|
|
|
* |
54
|
|
|
* @return InvoiceInterface |
55
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
56
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
57
|
|
|
* @throws InvalidResultException |
58
|
|
|
*/ |
59
|
1 |
|
public function delete(InvoiceInterface $invoice) :InvoiceInterface |
60
|
|
|
{ |
61
|
1 |
|
return $this->checkResult($this->objectManager->deleteObject($invoice, $this->reqRoute)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param InvoiceInterface $invoice |
66
|
|
|
* |
67
|
|
|
* @return InvoiceInterface |
68
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
69
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
70
|
|
|
* @throws InvalidResultException |
71
|
|
|
*/ |
72
|
1 |
|
public function update(InvoiceInterface $invoice) :InvoiceInterface |
73
|
|
|
{ |
74
|
1 |
|
return $this->checkResult($this->objectManager->updateObject($invoice, $this->reqRoute)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param InvoiceInterface $invoice |
79
|
|
|
* |
80
|
|
|
* @return InvoiceInterface |
81
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
82
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
83
|
|
|
* @throws InvalidResultException |
84
|
|
|
*/ |
85
|
1 |
|
public function restore(InvoiceInterface $invoice) :InvoiceInterface |
86
|
|
|
{ |
87
|
1 |
|
return $this->checkResult($this->objectManager->restoreObject($invoice, $this->reqRoute)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param InvoiceInterface $invoice |
92
|
|
|
* |
93
|
|
|
* @return InvoiceInterface |
94
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
95
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
96
|
|
|
* @throws InvalidResultException |
97
|
|
|
*/ |
98
|
1 |
|
public function archive(InvoiceInterface $invoice) :InvoiceInterface |
99
|
|
|
{ |
100
|
1 |
|
return $this->checkResult($this->objectManager->archiveObject($invoice, $this->reqRoute)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $id |
105
|
|
|
* |
106
|
|
|
* @return InvoiceInterface |
107
|
|
|
* @throws NotFoundException |
108
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
109
|
|
|
* @throws InvalidResultException |
110
|
|
|
*/ |
111
|
2 |
|
public function getInvoiceById(int $id) :InvoiceInterface |
112
|
|
|
{ |
113
|
2 |
|
return $this->checkResult($this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $invoiceNumber |
118
|
|
|
* |
119
|
|
|
* @return InvoiceInterface |
120
|
|
|
* @throws InvalidResultException |
121
|
|
|
* @throws NotFoundException |
122
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
123
|
|
|
* @throws \InvoiceNinjaModule\Exception\InvalidParameterException |
124
|
|
|
*/ |
125
|
3 |
|
public function getInvoiceByNumber(string $invoiceNumber) :InvoiceInterface |
126
|
|
|
{ |
127
|
3 |
|
$result = $this->objectManager->findObjectBy( |
128
|
3 |
|
$this->objectType, |
129
|
3 |
|
[Invoice::INVOICE_NR => $invoiceNumber], |
130
|
3 |
|
$this->reqRoute |
131
|
|
|
); |
132
|
|
|
|
133
|
3 |
|
if (\count($result) === 1) { |
134
|
1 |
|
return $result[0]; |
135
|
|
|
} |
136
|
|
|
|
137
|
2 |
|
if (empty($result)) { |
138
|
1 |
|
throw new NotFoundException(Invoice::INVOICE_NR.' '.$invoiceNumber); |
139
|
|
|
} |
140
|
1 |
|
throw new InvalidResultException(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param int $page |
145
|
|
|
* @param int $pageSize |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
* @throws InvalidResultException |
149
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
150
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
151
|
|
|
*/ |
152
|
3 |
|
public function getAllInvoices(int $page = 1, int $pageSize = 0) :array |
153
|
|
|
{ |
154
|
3 |
|
$result = $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize); |
155
|
3 |
|
foreach ($result as $invoice) { |
156
|
2 |
|
$this->checkResult($invoice); |
157
|
|
|
} |
158
|
2 |
|
return $result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param int $invoiceId |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
* @throws \InvoiceNinjaModule\Exception\ApiException |
166
|
|
|
* @throws \InvoiceNinjaModule\Exception\EmptyResponseException |
167
|
|
|
*/ |
168
|
1 |
|
public function downloadInvoice(int $invoiceId) :array |
169
|
|
|
{ |
170
|
1 |
|
return $this->objectManager->downloadFile($invoiceId); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param BaseInterface $invoice |
175
|
|
|
* |
176
|
|
|
* @return InvoiceInterface |
177
|
|
|
* @throws InvalidResultException |
178
|
|
|
*/ |
179
|
8 |
|
private function checkResult(BaseInterface $invoice) :InvoiceInterface |
180
|
|
|
{ |
181
|
8 |
|
if (!$invoice instanceof InvoiceInterface) { |
182
|
1 |
|
throw new InvalidResultException(); |
183
|
|
|
} |
184
|
7 |
|
return $invoice; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|