|
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\ProductInterface; |
|
10
|
|
|
use InvoiceNinjaModule\Model\Product; |
|
11
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ObjectServiceInterface; |
|
12
|
|
|
use InvoiceNinjaModule\Service\Interfaces\ProductManagerInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class ProductManager |
|
16
|
|
|
*/ |
|
17
|
|
|
final class ProductManager implements ProductManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ObjectServiceInterface */ |
|
20
|
|
|
private $objectManager; |
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $reqRoute; |
|
23
|
|
|
/** @var ProductInterface */ |
|
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 = '/products'; |
|
35
|
12 |
|
$this->objectType = new Product(); |
|
36
|
12 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ProductInterface $product |
|
40
|
|
|
* |
|
41
|
|
|
* @return ProductInterface |
|
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 createProduct(ProductInterface $product) :ProductInterface |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->checkResult($this->objectManager->createObject($product, $this->reqRoute)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ProductInterface $product |
|
54
|
|
|
* |
|
55
|
|
|
* @return ProductInterface |
|
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(ProductInterface $product) :ProductInterface |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->checkResult($this->objectManager->deleteObject($product, $this->reqRoute)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param ProductInterface $product |
|
68
|
|
|
* |
|
69
|
|
|
* @return ProductInterface |
|
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(ProductInterface $product) :ProductInterface |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->checkResult($this->objectManager->updateObject($product, $this->reqRoute)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param ProductInterface $product |
|
82
|
|
|
* |
|
83
|
|
|
* @return ProductInterface |
|
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(ProductInterface $product) :ProductInterface |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->checkResult($this->objectManager->restoreObject($product, $this->reqRoute)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param ProductInterface $product |
|
96
|
|
|
* |
|
97
|
|
|
* @return ProductInterface |
|
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(ProductInterface $product) :ProductInterface |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->checkResult($this->objectManager->archiveObject($product, $this->reqRoute)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $id |
|
110
|
|
|
* |
|
111
|
|
|
* @return ProductInterface |
|
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 getProductById(int $id) :ProductInterface |
|
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 getAllProducts(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 $product) { |
|
137
|
2 |
|
$this->checkResult($product); |
|
138
|
|
|
} |
|
139
|
2 |
|
return $result; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param BaseInterface $product |
|
144
|
|
|
* |
|
145
|
|
|
* @return ProductInterface |
|
146
|
|
|
* @throws \InvoiceNinjaModule\Exception\InvalidResultException |
|
147
|
|
|
*/ |
|
148
|
8 |
|
private function checkResult(BaseInterface $product) :ProductInterface |
|
149
|
|
|
{ |
|
150
|
8 |
|
if (!$product instanceof ProductInterface) { |
|
151
|
1 |
|
throw new InvalidResultException(); |
|
152
|
|
|
} |
|
153
|
7 |
|
return $product; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|