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