Completed
Push — master ( d19556...dcc177 )
by Al3x
08:24
created

TaxRateManager::getAllTaxRates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
ccs 5
cts 5
cp 1
cc 2
eloc 5
nc 2
nop 2
crap 2
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\TaxRateInterface;
10
use InvoiceNinjaModule\Model\TaxRate;
11
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface;
12
use InvoiceNinjaModule\Service\Interfaces\TaxRateManagerInterface;
13
14
/**
15
 * Class TaxRateManager
16
 */
17
final class TaxRateManager implements TaxRateManagerInterface
18
{
19
    /** @var ObjectServiceInterface  */
20
    private $objectManager;
21
    /** @var  string */
22
    private $reqRoute;
23
    /** @var TaxRateInterface  */
24
    private $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 \InvoiceNinjaModule\Exception\EmptyResponseException
43
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
44
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
45
     * @throws \InvoiceNinjaModule\Exception\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 \InvoiceNinjaModule\Exception\EmptyResponseException
57
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
58
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
59
     * @throws \InvoiceNinjaModule\Exception\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 \InvoiceNinjaModule\Exception\EmptyResponseException
71
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
72
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
73
     * @throws \InvoiceNinjaModule\Exception\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 \InvoiceNinjaModule\Exception\EmptyResponseException
85
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
86
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
87
     * @throws \InvoiceNinjaModule\Exception\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 \InvoiceNinjaModule\Exception\EmptyResponseException
99
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
100
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
101
     * @throws \InvoiceNinjaModule\Exception\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 int $id
110
     *
111
     * @return TaxRateInterface
112
     * @throws NotFoundException
113
     * @throws \InvoiceNinjaModule\Exception\EmptyResponseException
114
     * @throws \InvoiceNinjaModule\Exception\InvalidResultException
115
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
116
     * @throws \InvoiceNinjaModule\Exception\ApiAuthException
117
     */
118 2
    public function getTaxRateById(int $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 \InvoiceNinjaModule\Exception\InvalidResultException
129
     * @throws \InvoiceNinjaModule\Exception\EmptyResponseException
130
     * @throws \InvoiceNinjaModule\Exception\HttpClientAuthException
131
     * @throws \InvoiceNinjaModule\Exception\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 \InvoiceNinjaModule\Exception\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