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