|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lifeboat\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Lifeboat\Exceptions\ApiException; |
|
6
|
|
|
use Lifeboat\Exceptions\OAuthException; |
|
7
|
|
|
use Lifeboat\Models\Location; |
|
8
|
|
|
use Lifeboat\Models\Product; |
|
9
|
|
|
use Lifeboat\Resource\ListResource; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Product |
|
13
|
|
|
* @package Lifeboat\Services |
|
14
|
|
|
*/ |
|
15
|
|
|
class Products extends ApiService { |
|
16
|
|
|
|
|
17
|
|
|
const LIST_DETAILED = 'detailed'; |
|
18
|
|
|
const LIST_SIMPLE = 'simple'; |
|
19
|
|
|
const LIST_BARE = 'bare'; |
|
20
|
|
|
const SORT_TITLE_ASC = 'title_az'; |
|
21
|
|
|
const SORT_TITLE_DESC = 'title_za'; |
|
22
|
|
|
const SORT_SKU_ASC = 'title_az'; |
|
23
|
|
|
const SORT_SKU_DESC = 'title_za'; |
|
24
|
|
|
const SORT_PRICE_ASC = 'title_az'; |
|
25
|
|
|
const SORT_PRICE_DESC = 'title_za'; |
|
26
|
|
|
const SORT_CREATED_ASC = 'title_az'; |
|
27
|
|
|
const SORT_CREATED_DESC = 'title_za'; |
|
28
|
|
|
const SORT_EDITED_ASC = 'title_az'; |
|
29
|
|
|
const SORT_EDITED_DESC = 'title_za'; |
|
30
|
|
|
|
|
31
|
|
|
private static $_cache_lists = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $id |
|
35
|
|
|
* @return Product|null |
|
36
|
|
|
* @throws ApiException |
|
37
|
|
|
* @throws OAuthException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function fetch(int $id): ?Product |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var Product|null $fetch */ |
|
42
|
|
|
$fetch = $this->_get('api/products/product/' . $id); |
|
43
|
|
|
return $fetch; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
* @return Product|null |
|
49
|
|
|
* @throws ApiException |
|
50
|
|
|
* @throws OAuthException |
|
51
|
|
|
*/ |
|
52
|
|
|
public function create(array $data): ?Product |
|
53
|
|
|
{ |
|
54
|
|
|
/** @var Product|null $create */ |
|
55
|
|
|
$create = $this->_post('api/products/product/', $data); |
|
56
|
|
|
return $create; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param int $id |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* @return Product|null |
|
63
|
|
|
* @throws ApiException |
|
64
|
|
|
* @throws OAuthException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function update(int $id, array $data): ?Product |
|
67
|
|
|
{ |
|
68
|
|
|
/** @var Product|null $post */ |
|
69
|
|
|
$post = $this->_post('api/products/product/' . $id, $data); |
|
70
|
|
|
return $post; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param int $id |
|
75
|
|
|
* @return bool |
|
76
|
|
|
* @throws ApiException |
|
77
|
|
|
* @throws OAuthException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function delete(int $id): bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->_delete('api/products/product/' . $id); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param int $id |
|
86
|
|
|
* @param int $quantity |
|
87
|
|
|
* @param Location $location |
|
88
|
|
|
* @return bool |
|
89
|
|
|
* @throws ApiException |
|
90
|
|
|
* @throws OAuthException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function setStockLevel(int $id, int $quantity, Location $location): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return (bool) $this->_post('api/products/inventory/'. $id . '/quantity', [ |
|
95
|
|
|
'location' => $location->ID, |
|
96
|
|
|
'quantity' => $quantity |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $search |
|
102
|
|
|
* @param string $sort |
|
103
|
|
|
* @param string $type |
|
104
|
|
|
* @param bool $clear_cache |
|
105
|
|
|
* @return ListResource |
|
106
|
|
|
*/ |
|
107
|
|
|
public function all( |
|
108
|
|
|
string $search = '', |
|
109
|
|
|
string $sort = self::SORT_CREATED_DESC, |
|
110
|
|
|
string $type = self::LIST_BARE, |
|
111
|
|
|
bool $clear_cache = false |
|
112
|
|
|
): ListResource { |
|
113
|
|
|
$key = md5($search.$sort.$type); |
|
114
|
|
|
|
|
115
|
|
|
if (!array_key_exists($key, self::$_cache_lists) || $clear_cache) { |
|
116
|
|
|
self::$_cache_lists[$key] = new ListResource($this->getClient(), 'api/products/all', [ |
|
117
|
|
|
'search' => $search, |
|
118
|
|
|
'sort' => $sort, |
|
119
|
|
|
'data' => $type |
|
120
|
|
|
], 20); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return self::$_cache_lists[$key]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|