1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Model; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Subscriber\Cache\CacheSubscriber; |
7
|
|
|
use Foxy\FoxyClient\FoxyClient; |
8
|
|
|
|
9
|
|
|
class FoxyStripeClient extends \Object |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var |
13
|
|
|
*/ |
14
|
|
|
private $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var |
18
|
|
|
*/ |
19
|
|
|
private $current_store; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
private $item_categories_url; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
private $item_categories; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* FoxyStripeClient constructor. |
33
|
|
|
*/ |
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$config = array( |
37
|
|
|
'use_sandbox' => false |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$site_config = \SiteConfig::current_site_config(); |
41
|
|
|
if ($site_config) { |
42
|
|
|
$config['client_id'] = $site_config->client_id; |
43
|
|
|
$config['client_secret'] = $site_config->client_secret; |
44
|
|
|
$config['refresh_token'] = $site_config->refresh_token; |
45
|
|
|
$config['access_token'] = $site_config->access_token; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$guzzle_config = array( |
49
|
|
|
'defaults' => array( |
50
|
|
|
'debug' => false, |
51
|
|
|
'exceptions' => false |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set up our Guzzle Client |
57
|
|
|
*/ |
58
|
|
|
$guzzle = new Client($guzzle_config); |
59
|
|
|
CacheSubscriber::attach($guzzle); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get our FoxyClient |
63
|
|
|
*/ |
64
|
|
|
$fc = new FoxyClient($guzzle, $config); |
65
|
|
|
|
66
|
|
|
$this->setClient($fc); |
67
|
|
|
$this->setCurrentStore(); |
68
|
|
|
$this->setItemCategoriesURL(); |
69
|
|
|
$this->setItemCategories(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
public function getClient() |
76
|
|
|
{ |
77
|
|
|
return $this->client; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $client |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function setClient($client) |
85
|
|
|
{ |
86
|
|
|
$this->client = $client; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getCurrentStore() |
94
|
|
|
{ |
95
|
|
|
return $this->current_store; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
public function setCurrentStore() |
102
|
|
|
{ |
103
|
|
|
$client = $this->getClient(); |
104
|
|
|
$config = \SiteConfig::current_site_config(); |
105
|
|
|
|
106
|
|
|
$errors = array(); |
107
|
|
|
$data = array( |
108
|
|
|
'store_domain' => $config->StoreName, |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if ($result = $client->get()) { |
112
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
113
|
|
|
if ($reporting_uri = $client->getLink('fx:reporting')) { |
114
|
|
|
$errors = array_merge($errors, $client->getErrors($reporting_uri)); |
115
|
|
|
if ($result = $client->get($reporting_uri)) { |
116
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
117
|
|
|
if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) { |
118
|
|
|
$errors = array_merge($errors, $client->getErrors($store_exists_uri)); |
119
|
|
|
if ($result = $client->get($store_exists_uri, $data)) { |
120
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
121
|
|
|
if ($store = $client->getLink('fx:store')) { |
122
|
|
|
$errors = array_merge($errors, $client->getErrors($store)); |
123
|
|
|
$this->current_store = $store; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
if (count($errors)) { |
130
|
|
|
\SS_Log::log('setCurrentStore errors - ' . json_encode($errors), \SS_Log::WARN); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array $data |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function updateStore($data = []) { |
|
|
|
|
139
|
|
|
$client = $this->getClient(); |
140
|
|
|
$errors = []; |
141
|
|
|
$result = $client->patch($this->getCurrentStore(), $data); |
142
|
|
|
|
143
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
144
|
|
|
if (count($errors)) { |
145
|
|
|
\SS_Log::log('updateStore errors - ' . json_encode($errors), \SS_Log::WARN); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function getItemCategoriesURL() |
153
|
|
|
{ |
154
|
|
|
return $this->item_categories_url; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* |
159
|
|
|
*/ |
160
|
|
|
public function setItemCategoriesURL() |
161
|
|
|
{ |
162
|
|
|
$client = $this->getClient(); |
163
|
|
|
$errors = []; |
164
|
|
|
$result = $client->get($this->getCurrentStore()); |
165
|
|
|
|
166
|
|
|
if (isset($result['_links']['fx:item_categories']['href'])) { |
167
|
|
|
$this->item_categories_url = $result['_links']['fx:item_categories']['href']; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
171
|
|
|
if (count($errors)) { |
172
|
|
|
\SS_Log::log('setItemCategoriesURL errors - ' . json_encode($errors), \SS_Log::WARN); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
public function getItemCategories() |
180
|
|
|
{ |
181
|
|
|
return $this->item_categories; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
public function setItemCategories() |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
$client = $this->getClient(); |
190
|
|
|
$errors = []; |
191
|
|
|
$result = $client->get($this->getItemCategoriesURL()); |
192
|
|
|
|
193
|
|
|
$this->item_categories = $result; |
194
|
|
|
|
195
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
196
|
|
|
if (count($errors)) { |
197
|
|
|
\SS_Log::log('setItemCategories errors - ' . json_encode($errors), \SS_Log::WARN); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param $code |
203
|
|
|
* @return mixed |
204
|
|
|
*/ |
205
|
|
|
public function getCategory($code) |
206
|
|
|
{ |
207
|
|
|
if ($categoriesURL = $this->getItemCategoriesURL()) { |
208
|
|
|
$client = $this->getClient(); |
209
|
|
|
$errors = []; |
210
|
|
|
$data = [ |
211
|
|
|
'code' => $code, |
212
|
|
|
]; |
213
|
|
|
if ($result = $client->get($categoriesURL, $data)) { |
214
|
|
|
if (count($result['_embedded']['fx:item_categories']) > 0) { |
215
|
|
|
$category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href']; |
216
|
|
|
return $category; |
217
|
|
|
} |
218
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
219
|
|
|
if (count($errors)) { |
220
|
|
|
\SS_Log::log('getCategory errors - ' . json_encode($errors), \SS_Log::WARN); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param array $data |
229
|
|
|
*/ |
230
|
|
|
public function putCategory($data = []) |
231
|
|
|
{ |
232
|
|
|
$client = $this->getClient(); |
233
|
|
|
$errors = []; |
234
|
|
|
|
235
|
|
|
if ($category = $this->getCategory($data['code'])) { |
236
|
|
|
$result = $client->patch($category, $data); |
237
|
|
|
} else { |
238
|
|
|
$result = $client->post($this->getItemCategoriesURL(), $data); |
239
|
|
|
} |
240
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
241
|
|
|
if (count($errors)) { |
242
|
|
|
\SS_Log::log('putCategory errors - ' . json_encode($errors), \SS_Log::WARN); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param array $data |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function deleteCategory($data = []) |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$client = $this->getClient(); |
252
|
|
|
$errors = []; |
253
|
|
|
|
254
|
|
|
if ($category = $this->getCategory($data['code'])) { |
255
|
|
|
$result = $client->delete($category); |
256
|
|
|
|
257
|
|
|
$errors = array_merge($errors, $client->getErrors($result)); |
258
|
|
|
if (count($errors)) { |
259
|
|
|
\SS_Log::log('deleteCategory errors - ' . json_encode($errors), \SS_Log::WARN); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.