1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Model; |
4
|
|
|
|
5
|
|
|
use Foxy\FoxyClient\FoxyClient; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
10
|
|
|
|
11
|
|
|
class FoxyStripeClient |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private static $table_name = 'FS_FoxyStripeClient'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var |
20
|
|
|
*/ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
private $current_store; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var |
30
|
|
|
*/ |
31
|
|
|
private $item_categories_url; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
private $item_categories; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* FoxyStripeClient constructor. |
40
|
|
|
* |
41
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
42
|
|
|
*/ |
43
|
49 |
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$config = array( |
46
|
49 |
|
'use_sandbox' => false, |
47
|
|
|
); |
48
|
|
|
|
49
|
49 |
|
if ($site_config = SiteConfig::current_site_config()) { |
50
|
49 |
|
$config['client_id'] = $site_config->client_id; |
51
|
49 |
|
$config['client_secret'] = $site_config->client_secret; |
52
|
49 |
|
$config['refresh_token'] = $site_config->refresh_token; |
53
|
49 |
|
$config['access_token'] = $site_config->access_token; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$guzzle_config = array( |
57
|
49 |
|
'defaults' => array( |
58
|
|
|
'debug' => false, |
59
|
|
|
'exceptions' => false, |
60
|
|
|
), |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
/* |
64
|
|
|
* Set up our Guzzle Client |
65
|
|
|
*/ |
66
|
49 |
|
$guzzle = new Client($guzzle_config); |
67
|
|
|
//CacheSubscriber::attach($guzzle); // todo add caching middleware guzzle-cache-middleware |
|
|
|
|
68
|
|
|
|
69
|
|
|
/* |
70
|
|
|
* Get our FoxyClient |
71
|
|
|
*/ |
72
|
49 |
|
$fc = new FoxyClient($guzzle, $config); |
73
|
|
|
|
74
|
49 |
|
$this->setClient($fc); |
75
|
49 |
|
$this->setCurrentStore(); |
76
|
49 |
|
$this->setItemCategoriesURL(); |
77
|
49 |
|
$this->setItemCategories(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
49 |
|
public function getClient() |
84
|
|
|
{ |
85
|
49 |
|
return $this->client; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $client |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
49 |
|
public function setClient($client) |
94
|
|
|
{ |
95
|
49 |
|
$this->client = $client; |
96
|
|
|
|
97
|
49 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
49 |
|
public function getCurrentStore() |
104
|
|
|
{ |
105
|
49 |
|
return $this->current_store; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
110
|
|
|
*/ |
111
|
49 |
|
public function setCurrentStore() |
112
|
|
|
{ |
113
|
49 |
|
$client = $this->getClient(); |
|
|
|
|
114
|
49 |
|
$config = SiteConfig::current_site_config(); |
115
|
|
|
|
116
|
49 |
|
$errors = array(); |
117
|
|
|
$data = array( |
118
|
49 |
|
'store_domain' => $config->StoreName, |
119
|
|
|
); |
120
|
|
|
|
121
|
49 |
|
if ($client && $result = $client->get()) { |
122
|
49 |
|
$errors = array_merge($errors, $client->getErrors($result)); |
123
|
49 |
|
if ($reporting_uri = $client->getLink('fx:reporting')) { |
124
|
|
|
$errors = array_merge($errors, $client->getErrors($reporting_uri)); |
125
|
|
|
if ($result = $client->get($reporting_uri)) { |
126
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
127
|
|
|
if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) { |
128
|
|
|
$errors = array_merge($errors, $client->getErrors($store_exists_uri)); |
129
|
|
|
if ($result = $client->get($store_exists_uri, $data)) { |
130
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
131
|
|
|
if ($store = $client->getLink('fx:store')) { |
132
|
|
|
$errors = array_merge($errors, $client->getErrors($store)); |
133
|
|
|
$this->current_store = $store; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
49 |
|
if (count($errors)) { |
140
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - '.json_encode($errors)); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $data |
147
|
|
|
* |
148
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
149
|
|
|
*/ |
150
|
|
|
public function updateStore($data = []) |
151
|
|
|
{ |
152
|
|
|
$client = $this->getClient(); |
|
|
|
|
153
|
|
|
$errors = []; |
154
|
|
|
|
155
|
|
|
$result = $client->patch($this->getCurrentStore(), $data); |
156
|
|
|
|
157
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
158
|
|
|
if (count($errors)) { |
159
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - '.json_encode($errors)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
49 |
|
public function getItemCategoriesURL() |
167
|
|
|
{ |
168
|
49 |
|
return $this->item_categories_url; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
173
|
|
|
*/ |
174
|
49 |
|
public function setItemCategoriesURL() |
175
|
|
|
{ |
176
|
49 |
|
$client = $this->getClient(); |
|
|
|
|
177
|
49 |
|
$errors = []; |
178
|
|
|
|
179
|
49 |
|
if ($client) { |
180
|
49 |
|
$result = $client->get($this->getCurrentStore()); |
|
|
|
|
181
|
|
|
|
182
|
49 |
|
if (isset($result['_links']['fx:item_categories']['href'])) { |
183
|
|
|
$this->item_categories_url = $result['_links']['fx:item_categories']['href']; |
184
|
|
|
} |
185
|
|
|
|
186
|
49 |
|
$errors = array_merge($errors, $client->getErrors($result)); |
187
|
49 |
|
if (count($errors)) { |
188
|
|
|
Injector::inst() |
189
|
|
|
->get(LoggerInterface::class)->error('setItemCategoriesURL errors - '.json_encode($errors)); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function getItemCategories() |
198
|
|
|
{ |
199
|
|
|
return $this->item_categories; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
204
|
|
|
*/ |
205
|
49 |
|
public function setItemCategories() |
206
|
|
|
{ |
207
|
49 |
|
$client = $this->getClient(); |
|
|
|
|
208
|
49 |
|
$errors = []; |
209
|
|
|
|
210
|
49 |
|
if ($client) { |
211
|
49 |
|
$result = $client->get($this->getItemCategoriesURL()); |
|
|
|
|
212
|
|
|
|
213
|
49 |
|
$this->item_categories = $result; |
214
|
|
|
|
215
|
49 |
|
$errors = array_merge($errors, $client->getErrors($result)); |
216
|
49 |
|
if (count($errors)) { |
217
|
|
|
Injector::inst() |
218
|
|
|
->get(LoggerInterface::class)->error('setItemCategories errors - '.json_encode($errors)); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $code |
225
|
|
|
* |
226
|
|
|
* @return bool |
227
|
|
|
* |
228
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
229
|
|
|
*/ |
230
|
49 |
|
public function getCategory($code) |
231
|
|
|
{ |
232
|
49 |
|
if ($categoriesURL = $this->getItemCategoriesURL()) { |
233
|
|
|
$client = $this->getClient(); |
|
|
|
|
234
|
|
|
$errors = []; |
235
|
|
|
$data = [ |
236
|
|
|
'code' => $code, |
237
|
|
|
]; |
238
|
|
|
if ($result = $client->get($categoriesURL, $data)) { |
239
|
|
|
if (count($result['_embedded']['fx:item_categories']) > 0) { |
240
|
|
|
$category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href']; |
241
|
|
|
|
242
|
|
|
return $category; |
243
|
|
|
} |
244
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
245
|
|
|
if (count($errors)) { |
246
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - '.json_encode($errors)); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
49 |
|
return false; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param array $data |
256
|
|
|
* |
257
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
258
|
|
|
*/ |
259
|
49 |
|
public function putCategory($data = []) |
260
|
|
|
{ |
261
|
49 |
|
$client = $this->getClient(); |
|
|
|
|
262
|
49 |
|
$errors = []; |
263
|
|
|
|
264
|
49 |
|
if ($client) { |
265
|
49 |
|
if ($category = $this->getCategory($data['code'])) { |
266
|
|
|
$result = $client->patch($category, $data); |
267
|
|
|
} else { |
268
|
49 |
|
$result = $client->post($this->getItemCategoriesURL(), $data); |
269
|
|
|
} |
270
|
49 |
|
$errors = array_merge($errors, $client->getErrors($result)); |
271
|
49 |
|
if (count($errors)) { |
272
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - '.json_encode($errors)); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param array $data |
279
|
|
|
* |
280
|
|
|
* @throws \Psr\Container\NotFoundExceptionInterface |
281
|
|
|
*/ |
282
|
1 |
|
public function deleteCategory($data = []) |
283
|
|
|
{ |
284
|
1 |
|
$client = $this->getClient(); |
|
|
|
|
285
|
1 |
|
$errors = []; |
286
|
|
|
|
287
|
1 |
|
if ($category = $this->getCategory($data['code'])) { |
288
|
|
|
$result = $client->delete($category); |
289
|
|
|
|
290
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
291
|
|
|
if (count($errors)) { |
292
|
|
|
Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - '.json_encode($errors)); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|