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