Passed
Push — master ( c2faf8...afa411 )
by Al3x
11:51
created

TaxRateManager::checkResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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