Passed
Push — master ( d80cfb...de12e2 )
by Al3x
01:55
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 17
    #[Pure] public function __construct(ObjectServiceInterface $objectManager)
35
    {
36 17
        $this->objectManager = $objectManager;
37 17
        $this->reqRoute = '/invoices';
38 17
        $this->objectType  = new Invoice();
39 17
    }
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 string $invoiceId
189
     *
190
     * @throws ApiAuthException
191
     * @throws EmptyResponseException
192
     * @throws HttpClientAuthException
193
     */
194
    public function sendEmailInvoice(string $invoiceId) :void
195
    {
196
        $this->objectManager->sendCommand(ObjectServiceInterface::ACTION_EMAIL, ['id'=> $invoiceId]);
197
    }
198
199
    /**
200
     * @param string $invoiceId
201
     *
202
     * @throws ApiAuthException
203
     * @throws EmptyResponseException
204
     * @throws HttpClientAuthException
205
     */
206
    public function markInvoiceSent(string $invoiceId) :void
207
    {
208
        $this->objectManager->sendCommand(ObjectServiceInterface::ACTION_MARK_SENT, ['id'=> $invoiceId]);
209
    }
210
211
    /**
212
     * @param string $invoiceId
213
     *
214
     * @throws ApiAuthException
215
     * @throws EmptyResponseException
216
     * @throws HttpClientAuthException
217
     */
218
    public function markInvoicePaid(string $invoiceId) :void
219
    {
220
        $this->objectManager->sendCommand(ObjectServiceInterface::ACTION_MARK_PAID, ['id'=> $invoiceId]);
221
    }
222
223
    /**
224
     * @param BaseInterface $invoice
225
     *
226
     * @return InvoiceInterface
227
     * @throws InvalidResultException
228
     */
229 8
    private function checkResult(BaseInterface $invoice) :InvoiceInterface
230
    {
231 8
        if (!$invoice instanceof InvoiceInterface) {
232 1
            throw new InvalidResultException();
233
        }
234 7
        return $invoice;
235
    }
236
}
237