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