TaxRateManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 20
dl 0
loc 147
ccs 25
cts 25
cp 1
rs 10
c 1
b 1
f 1
wmc 11

9 Methods

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