1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\Dandomain\Api\Endpoint; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
use Loevgaard\Dandomain\Api\KeyValue\KeyValueCollection; |
6
|
|
|
|
7
|
|
|
class ProductData extends Endpoint |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $productNumber |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
|
|
public function getDataProduct(string $productNumber) : array |
14
|
|
|
{ |
15
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
16
|
|
|
|
17
|
|
|
return (array)$this->master->doRequest( |
18
|
|
|
'GET', |
19
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s', rawurlencode($productNumber)) |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param int $categoryId |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getDataProductsInCategory(int $categoryId) : array |
28
|
|
|
{ |
29
|
|
|
Assert::that($categoryId)->greaterThan(0, 'The $categoryId has to be positive'); |
30
|
|
|
|
31
|
|
|
return (array)$this->master->doRequest( |
32
|
|
|
'GET', |
33
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/Products/{KEY}/%d', $categoryId) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $barCode |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function getDataProductsByBarcode(string $barCode) : array |
42
|
|
|
{ |
43
|
|
|
Assert::that($barCode)->minLength(1, 'The length of $barCode has to be > 0'); |
44
|
|
|
|
45
|
|
|
return (array)$this->master->doRequest( |
46
|
|
|
'GET', |
47
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/ByBarcode/%s', rawurlencode($barCode)) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param \DateTimeInterface $date |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function getDataProductsByModificationDate(\DateTimeInterface $date) : array |
56
|
|
|
{ |
57
|
|
|
return (array)$this->master->doRequest( |
58
|
|
|
'GET', |
59
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/ByModificationDate/%s', $date->format('Y-m-d')) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \DateTimeInterface $dateStart |
65
|
|
|
* @param \DateTimeInterface $dateEnd |
66
|
|
|
* @param int $page |
67
|
|
|
* @param int $pageSize |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getDataProductsInModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd, int $page = 1, int $pageSize = 100) : array |
71
|
|
|
{ |
72
|
|
|
Assert::that($dateStart)->lessThan($dateEnd, '$dateStart has to be before $dateEnd'); |
73
|
|
|
Assert::that($page)->greaterThan(0, 'The $page has to be positive'); |
74
|
|
|
Assert::that($pageSize)->greaterThan(0, 'The $pageSize has to be positive'); |
75
|
|
|
|
76
|
|
|
return (array)$this->master->doRequest( |
77
|
|
|
'GET', |
78
|
|
|
sprintf( |
79
|
|
|
'/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/GetByModifiedInterval?start=%s&end=%s&pageIndex=%d&pageSize=%d', |
80
|
|
|
$dateStart->format('Y-m-d\TH:i:s'), |
81
|
|
|
$dateEnd->format('Y-m-d\TH:i:s'), |
82
|
|
|
$page, |
83
|
|
|
$pageSize |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \DateTimeInterface $dateStart |
90
|
|
|
* @param \DateTimeInterface $dateEnd |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
|
|
public function countByModifiedInterval(\DateTimeInterface $dateStart, \DateTimeInterface $dateEnd) : int |
94
|
|
|
{ |
95
|
|
|
Assert::that($dateStart)->lessThan($dateEnd, '$dateStart has to be before $dateEnd'); |
96
|
|
|
|
97
|
|
|
return (int)$this->master->doRequest( |
98
|
|
|
'GET', |
99
|
|
|
sprintf( |
100
|
|
|
'/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/CountByModifiedInterval?start=%s&end=%s', |
101
|
|
|
$dateStart->format('Y-m-d\TH:i:s'), |
102
|
|
|
$dateEnd->format('Y-m-d\TH:i:s') |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array|\stdClass $product |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function createProduct($product) : array |
112
|
|
|
{ |
113
|
|
|
return (array)$this->master->doRequest('POST', '/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}', $product); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $productNumber |
118
|
|
|
* @param int $stockCount |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function setStockCount(string $productNumber, int $stockCount) : bool |
122
|
|
|
{ |
123
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
124
|
|
|
|
125
|
|
|
return (bool)$this->master->doRequest( |
126
|
|
|
'PUT', |
127
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/SetStockCount/%s/%d', rawurlencode($productNumber), $stockCount) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Will return the stock count for the specified product number |
133
|
|
|
* |
134
|
|
|
* @param string $productNumber |
135
|
|
|
* @return int |
136
|
|
|
*/ |
137
|
|
|
public function getStockCount(string $productNumber) : int |
138
|
|
|
{ |
139
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
140
|
|
|
$product = $this->getDataProduct($productNumber); |
141
|
|
|
|
142
|
|
|
return (int)$product['stockCount']; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Will increment or decrement the stock count for the given product number |
147
|
|
|
* |
148
|
|
|
* @param string $productNumber |
149
|
|
|
* @param int $amount |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function incrementOrDecrementStockCount(string $productNumber, int $amount) : array |
153
|
|
|
{ |
154
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
155
|
|
|
|
156
|
|
|
$oldStockCount = $this->getStockCount($productNumber); |
157
|
|
|
$newStockCount = $oldStockCount + $amount; |
158
|
|
|
|
159
|
|
|
$this->setStockCount($productNumber, $newStockCount); |
160
|
|
|
|
161
|
|
|
return [ |
162
|
|
|
'oldStockCount' => $oldStockCount, |
163
|
|
|
'newStockCount' => $newStockCount, |
164
|
|
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Will increment the stock count for the given product number |
169
|
|
|
* |
170
|
|
|
* @param string $productNumber |
171
|
|
|
* @param int $amount |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function incrementStockCount(string $productNumber, int $amount) : array |
175
|
|
|
{ |
176
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
177
|
|
|
|
178
|
|
|
return $this->incrementOrDecrementStockCount($productNumber, abs($amount)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Will decrement the stock count for the given product number |
183
|
|
|
* |
184
|
|
|
* @param string $productNumber |
185
|
|
|
* @param int $amount |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
public function decrementStockCount(string $productNumber, int $amount) : array |
189
|
|
|
{ |
190
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
191
|
|
|
|
192
|
|
|
return $this->incrementOrDecrementStockCount($productNumber, abs($amount)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function getDataCategories() : array |
199
|
|
|
{ |
200
|
|
|
return $this->master->doRequest('GET', '/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/Categories'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param int $categoryId |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function getDataSubCategories(int $categoryId) : array |
208
|
|
|
{ |
209
|
|
|
Assert::that($categoryId)->greaterThan(0, 'The $categoryId has to be positive'); |
210
|
|
|
|
211
|
|
|
return (array)$this->master->doRequest( |
212
|
|
|
'GET', |
213
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/Categories/%d', $categoryId) |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return int |
219
|
|
|
*/ |
220
|
|
|
public function getProductCount() : int |
221
|
|
|
{ |
222
|
|
|
return (int)$this->master->doRequest('GET', '/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/ProductCount'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param int $page |
227
|
|
|
* @param int $pageSize |
228
|
|
|
* @return array |
229
|
|
|
*/ |
230
|
|
View Code Duplication |
public function getProductPage(int $page = 1, int $pageSize = 100) : array |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
Assert::that($page)->greaterThan(0, 'The $page has to be positive'); |
233
|
|
|
Assert::that($pageSize)->greaterThan(0, 'The $pageSize has to be positive'); |
234
|
|
|
|
235
|
|
|
return (array)$this->master->doRequest( |
236
|
|
|
'GET', |
237
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/ProductPage/%d/%d', $page, $pageSize) |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* This method will return the number of pages you need to iterate to get the whole catalog using a page size of $pageSize |
243
|
|
|
* If a shop has 10,000 products, a call with $pageSize = 100 will return 10,000 / 100 = 100 |
244
|
|
|
* |
245
|
|
|
* @param int $pageSize |
246
|
|
|
* @return int |
247
|
|
|
*/ |
248
|
|
|
public function getProductPageCount(int $pageSize = 100) : int |
249
|
|
|
{ |
250
|
|
|
Assert::that($pageSize)->greaterThan(0, 'The $pageSize has to be positive'); |
251
|
|
|
|
252
|
|
|
return (int)$this->master->doRequest( |
253
|
|
|
'GET', |
254
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/ProductPageCount/%d', $pageSize) |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $productNumber |
260
|
|
|
* @return boolean |
261
|
|
|
*/ |
262
|
|
|
public function deleteProduct(string $productNumber) : bool |
263
|
|
|
{ |
264
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
265
|
|
|
|
266
|
|
|
return (bool)$this->master->doRequest( |
267
|
|
|
'DELETE', |
268
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s', rawurlencode($productNumber)) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array|\stdClass $category |
274
|
|
|
* @return array |
275
|
|
|
*/ |
276
|
|
|
public function createCategory($category) : array |
277
|
|
|
{ |
278
|
|
|
return (array)$this->master->doRequest('POST', '/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/Category', $category); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param int $categoryId |
283
|
|
|
* @return boolean |
284
|
|
|
*/ |
285
|
|
|
public function deleteCategory(int $categoryId) : bool |
286
|
|
|
{ |
287
|
|
|
Assert::that($categoryId)->greaterThan(0, 'The $categoryId has to be positive'); |
288
|
|
|
|
289
|
|
|
return (bool)$this->master->doRequest( |
290
|
|
|
'DELETE', |
291
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/Category/%d', $categoryId) |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param int $categoryId |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
public function getDataCategory(int $categoryId) : array |
300
|
|
|
{ |
301
|
|
|
Assert::that($categoryId)->greaterThan(0, 'The $categoryId has to be positive'); |
302
|
|
|
|
303
|
|
|
return (array)$this->master->doRequest( |
304
|
|
|
'GET', |
305
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/Category/%d', $categoryId) |
306
|
|
|
); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param string $productNumber |
311
|
|
|
* @param array|\stdClass $product |
312
|
|
|
* @return array |
313
|
|
|
*/ |
314
|
|
|
public function updateProduct(string $productNumber, $product) : array |
315
|
|
|
{ |
316
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
317
|
|
|
|
318
|
|
|
return (array)$this->master->doRequest( |
319
|
|
|
'PUT', |
320
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s', rawurlencode($productNumber)), |
321
|
|
|
$product |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param string $productNumber |
327
|
|
|
* @param KeyValueCollection $keyValueCollection |
328
|
|
|
* @return array |
329
|
|
|
*/ |
330
|
|
View Code Duplication |
public function patchProduct(string $productNumber, KeyValueCollection $keyValueCollection) : array |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
333
|
|
|
Assert::that($keyValueCollection->count())->greaterThan(0); |
334
|
|
|
|
335
|
|
|
return (array)$this->master->doRequest( |
336
|
|
|
'PATCH', |
337
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s', rawurlencode($productNumber)), |
338
|
|
|
$keyValueCollection->get() |
339
|
|
|
); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param string $productNumber |
344
|
|
|
* @param array|\stdClass $price |
345
|
|
|
* @return array |
346
|
|
|
*/ |
347
|
|
|
public function createPrice(string $productNumber, $price) : array |
348
|
|
|
{ |
349
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
350
|
|
|
|
351
|
|
|
return (array)$this->master->doRequest( |
352
|
|
|
'POST', |
353
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s/Prices', rawurlencode($productNumber)), |
354
|
|
|
$price |
355
|
|
|
); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param string $productNumber |
360
|
|
|
* @param array|\stdClass $price |
361
|
|
|
* @return bool |
362
|
|
|
*/ |
363
|
|
|
public function deletePrice(string $productNumber, $price) : bool |
364
|
|
|
{ |
365
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
366
|
|
|
|
367
|
|
|
return (bool)$this->master->doRequest( |
368
|
|
|
'DELETE', |
369
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s/Prices', rawurlencode($productNumber)), |
370
|
|
|
$price |
371
|
|
|
); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param string $productNumber |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
|
|
public function getPricesForProduct(string $productNumber) : array |
379
|
|
|
{ |
380
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
381
|
|
|
|
382
|
|
|
return (array)$this->master->doRequest( |
383
|
|
|
'GET', |
384
|
|
|
sprintf('/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%s/Prices/List', rawurlencode($productNumber)) |
385
|
|
|
); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param int $siteId |
390
|
|
|
* @param string $productNumber |
391
|
|
|
* @param KeyValueCollection $keyValueCollection |
392
|
|
|
* @return array |
393
|
|
|
*/ |
394
|
|
View Code Duplication |
public function patchProductSettings(int $siteId, string $productNumber, KeyValueCollection $keyValueCollection) : array |
|
|
|
|
395
|
|
|
{ |
396
|
|
|
Assert::that($siteId)->greaterThan(0, 'The $siteId has to be positive'); |
397
|
|
|
Assert::that($productNumber)->minLength(1, 'The length of $productNumber has to be > 0'); |
398
|
|
|
Assert::that($keyValueCollection->count())->greaterThan(0); |
399
|
|
|
|
400
|
|
|
return (array)$this->master->doRequest( |
401
|
|
|
'PATCH', |
402
|
|
|
sprintf( |
403
|
|
|
'/admin/WEBAPI/Endpoints/v1_0/ProductDataService/{KEY}/%d/Products/%s/Settings', |
404
|
|
|
$siteId, |
405
|
|
|
rawurlencode($productNumber) |
406
|
|
|
), |
407
|
|
|
$keyValueCollection->get() |
408
|
|
|
); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.