Passed
Push — master ( de12e2...4347a5 )
by Al3x
02:17 queued 13s
created

InvoiceManager::sendEmailInvoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\Interfaces\BaseInterface;
13
use InvoiceNinjaModule\Model\Interfaces\InvoiceInterface;
14
use InvoiceNinjaModule\Model\Invoice;
15
use InvoiceNinjaModule\Service\Interfaces\InvoiceManagerInterface;
16
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface;
17
use JetBrains\PhpStorm\Pure;
18
use function count;
19
20
/**
21
 * Class InvoiceManager
22
 */
23
final class InvoiceManager implements InvoiceManagerInterface
24
{
25
    private ObjectServiceInterface $objectManager;
26
    private string $reqRoute;
27
    private InvoiceInterface $objectType;
28
29
    /**
30
     * InvoiceManager constructor.
31
     *
32
     * @param ObjectServiceInterface $objectManager
33
     */
34 18
    #[Pure] public function __construct(ObjectServiceInterface $objectManager)
35
    {
36 18
        $this->objectManager = $objectManager;
37 18
        $this->reqRoute = '/invoices';
38 18
        $this->objectType  = new Invoice();
39 18
    }
40
41
    /**
42
     * @param InvoiceInterface $invoice
43
     *
44
     * @return InvoiceInterface
45
     * @throws EmptyResponseException
46
     * @throws InvalidResultException
47
     * @throws HttpClientAuthException
48
     * @throws ApiAuthException
49
     */
50 1
    public function createInvoice(InvoiceInterface $invoice) :InvoiceInterface
51
    {
52
        //@TODO: Invoice needs at least a clientId, a Date and status!
53 1
        return $this->checkResult($this->objectManager->createObject($invoice, $this->reqRoute));
54
    }
55
56
    /**
57
     * @param InvoiceInterface $invoice
58
     *
59
     * @return InvoiceInterface
60
     * @throws EmptyResponseException
61
     * @throws InvalidResultException
62
     * @throws HttpClientAuthException
63
     * @throws ApiAuthException
64
     */
65 1
    public function delete(InvoiceInterface $invoice) :InvoiceInterface
66
    {
67 1
        return $this->checkResult($this->objectManager->deleteObject($invoice, $this->reqRoute));
68
    }
69
70
    /**
71
     * @param InvoiceInterface $invoice
72
     *
73
     * @return InvoiceInterface
74
     * @throws EmptyResponseException
75
     * @throws InvalidResultException
76
     * @throws HttpClientAuthException
77
     * @throws ApiAuthException
78
     */
79 1
    public function update(InvoiceInterface $invoice) :InvoiceInterface
80
    {
81 1
        return $this->checkResult($this->objectManager->updateObject($invoice, $this->reqRoute));
82
    }
83
84
    /**
85
     * @param InvoiceInterface $invoice
86
     *
87
     * @return InvoiceInterface
88
     * @throws EmptyResponseException
89
     * @throws InvalidResultException
90
     * @throws HttpClientAuthException
91
     * @throws ApiAuthException
92
     */
93 1
    public function restore(InvoiceInterface $invoice) :InvoiceInterface
94
    {
95 1
        return $this->checkResult($this->objectManager->restoreObject($invoice, $this->reqRoute));
96
    }
97
98
    /**
99
     * @param InvoiceInterface $invoice
100
     *
101
     * @return InvoiceInterface
102
     * @throws EmptyResponseException
103
     * @throws InvalidResultException
104
     * @throws HttpClientAuthException
105
     * @throws ApiAuthException
106
     */
107 1
    public function archive(InvoiceInterface $invoice) :InvoiceInterface
108
    {
109 1
        return $this->checkResult($this->objectManager->archiveObject($invoice, $this->reqRoute));
110
    }
111
112
    /**
113
     * @param string $id
114
     *
115
     * @return InvoiceInterface
116
     * @throws NotFoundException
117
     * @throws EmptyResponseException
118
     * @throws InvalidResultException
119
     * @throws HttpClientAuthException
120
     * @throws ApiAuthException
121
     */
122 2
    public function getInvoiceById(string $id) :InvoiceInterface
123
    {
124 2
        return $this->checkResult($this->objectManager->getObjectById($this->objectType, $id, $this->reqRoute));
125
    }
126
127
    /**
128
     * @param string $invoiceNumber
129
     *
130
     * @return InvoiceInterface
131
     * @throws InvalidResultException
132
     * @throws NotFoundException
133
     * @throws InvalidParameterException
134
     * @throws HttpClientAuthException
135
     * @throws ApiAuthException
136
     */
137 3
    public function getInvoiceByNumber(string $invoiceNumber) :InvoiceInterface
138
    {
139 3
        $result = $this->objectManager->findObjectBy(
140 3
            $this->objectType,
141 3
            [InvoiceInterface::INVOICE_NR => $invoiceNumber],
142 3
            $this->reqRoute
143
        );
144
145 3
        if (count($result) === 1 && $result[0] instanceof InvoiceInterface) {
146 1
            return $result[0];
147
        }
148
149 2
        if (empty($result)) {
150 1
            throw new NotFoundException(InvoiceInterface::INVOICE_NR.' '.$invoiceNumber);
151
        }
152 1
        throw new InvalidResultException();
153
    }
154
155
    /**
156
     * @param int $page
157
     * @param int $pageSize
158
     *
159
     * @return array
160
     * @throws InvalidResultException
161
     * @throws EmptyResponseException
162
     * @throws HttpClientAuthException
163
     * @throws ApiAuthException
164
     */
165 3
    public function getAllInvoices(int $page = 1, int $pageSize = 0) :array
166
    {
167 3
        $result = $this->objectManager->getAllObjects($this->objectType, $this->reqRoute, $page, $pageSize);
168 3
        foreach ($result as $invoice) {
169 2
            $this->checkResult($invoice);
170
        }
171 2
        return $result;
172
    }
173
174
    /**
175
     * @param string $invitationKey
176
     *
177
     * @return array
178
     * @throws EmptyResponseException
179
     * @throws HttpClientAuthException
180
     * @throws ApiAuthException
181
     */
182 1
    public function downloadInvoice(string $invitationKey) :array
183
    {
184 1
        return $this->objectManager->downloadFile($invitationKey, 'invoice');
185
    }
186
187
    /**
188
     * @param array $invoiceIds
189
     *
190
     * @throws ApiAuthException
191
     * @throws EmptyResponseException
192
     * @throws HttpClientAuthException
193
     */
194 1
    public function sendInvoicesEmail(array $invoiceIds) :void
195
    {
196 1
        $this->objectManager->sendBulkCommand(
197 1
            ObjectServiceInterface::ACTION_EMAIL,
198
            $invoiceIds,
199 1
            $this->reqRoute
200
        );
201 1
    }
202
203
    /**
204
     * @param array $invoiceIds
205
     *
206
     * @throws ApiAuthException
207
     * @throws EmptyResponseException
208
     * @throws HttpClientAuthException
209
     */
210
    public function markInvoicesSent(array $invoiceIds) :void
211
    {
212
        $this->objectManager->sendBulkCommand(
213
            ObjectServiceInterface::ACTION_MARK_SENT,
214
            $invoiceIds,
215
            $this->reqRoute
216
        );
217
    }
218
219
    /**
220
     * @param array $invoiceIds
221
     *
222
     * @throws ApiAuthException
223
     * @throws EmptyResponseException
224
     * @throws HttpClientAuthException
225
     */
226
    public function markInvoicesPaid(array $invoiceIds) :void
227
    {
228
        $this->objectManager->sendBulkCommand(
229
            ObjectServiceInterface::ACTION_MARK_PAID,
230
            $invoiceIds,
231
            $this->reqRoute
232
        );
233
    }
234
235
    /**
236
     * @param BaseInterface $invoice
237
     *
238
     * @return InvoiceInterface
239
     * @throws InvalidResultException
240
     */
241 8
    private function checkResult(BaseInterface $invoice) :InvoiceInterface
242
    {
243 8
        if (!$invoice instanceof InvoiceInterface) {
244 1
            throw new InvalidResultException();
245
        }
246 7
        return $invoice;
247
    }
248
}
249