Completed
Pull Request — master (#373)
by Nic
12:09
created

FoxyStripeClient::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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
The private property $table_name is not used, and could be removed.
Loading history...
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 = [
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
            $config['access_token_expires'] = 7200;
53
        }
54
55 10
        $guzzle_config = [
56
            'defaults' => [
57
                'debug' => false,
58
                'exceptions' => false,
59
            ],
60
        ];
61
62
        /*
63
         * Set up our Guzzle Client
64 10
         */
65
        $guzzle = new Client($guzzle_config);
66
        //CacheSubscriber::attach($guzzle); // todo add caching middleware guzzle-cache-middleware
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
68
        /*
69
         * Get our FoxyClient
70 10
         */
71
        $fc = new FoxyClient($guzzle, $config);
72 10
73 10
        $this->setClient($fc);
74 10
        $this->setCurrentStore();
75 10
        $this->setItemCategoriesURL();
76
        $this->setItemCategories();
77
    }
78
79
    /**
80
     * @return mixed
81 10
     */
82
    public function getClient()
83 10
    {
84
        return $this->client;
85
    }
86
87
    /**
88
     * @param $client
89
     *
90
     * @return $this
91 10
     */
92
    public function setClient($client)
93 10
    {
94
        $this->client = $client;
95 10
96
        return $this;
97
    }
98
99
    /**
100
     * @return bool
101
     * @throws \SilverStripe\ORM\ValidationException
102 11
     */
103
    public static function is_valid()
104 11
    {
105 11
        $config = FoxyStripeSetting::current_foxystripe_setting();
106 11
107 11
        return $config->EnableAPI &&
0 ignored issues
show
Bug Best Practice introduced by
The property EnableAPI does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
108 11
            $config->client_id &&
109 11
            $config->client_secret &&
110
            $config->refresh_token &&
111
            $config->access_token;
112
    }
113
114
    /**
115 10
     * @return mixed
116
     */
117 10
    public function getCurrentStore()
118
    {
119
        return $this->current_store;
120
    }
121
122
    /**
123 10
     * @throws \SilverStripe\ORM\ValidationException
124
     */
125 10
    public function setCurrentStore()
126 10
    {
127
        $client = $this->getClient();
128 10
        $config = FoxyStripeSetting::current_foxystripe_setting();
129
130 10
        $errors = [];
131
        $data = [
132
            'store_domain' => $config->StoreName,
133 10
        ];
134 10
135 10
        if ($client && $result = $client->get()) {
136
            $errors = array_merge($errors, $client->getErrors($result));
137
            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 10
                }
152
            }
153
            if (count($errors)) {
154
                Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - ' . json_encode($errors));
155
            }
156
        }
157
    }
158
159
    /**
160
     * @param $uri
161
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
162
     */
163
    public function post($uri, $data = null)
164
    {
165
        $this->getClient()->post($uri, $data);
166
    }
167
168
    /**
169
     * @param $uri
170
     * @param null $data
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $data is correct as it would always require null to be passed?
Loading history...
171
     */
172
    public function patch($uri, $data = null)
173
    {
174
        $this->getClient()->patch($uri, $data);
175
    }
176
177
    /**
178 10
     * @param array $data
179
     *
180 10
     * @throws \Psr\Container\NotFoundExceptionInterface
181
     */
182
    public function updateStore($data = [])
183
    {
184
        $client = $this->getClient();
185
        $errors = [];
186 10
187
        $result = $client->patch($this->getCurrentStore(), $data);
188 10
189 10
        $errors = array_merge($errors, $client->getErrors($result));
190
        if (count($errors)) {
191 10
            Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - ' . json_encode($errors));
192 10
        }
193
    }
194 10
195
    /**
196
     * @return mixed
197
     */
198 10
    public function getItemCategoriesURL()
199 10
    {
200
        return $this->item_categories_url;
201
    }
202
203
    /**
204
     * @throws \Psr\Container\NotFoundExceptionInterface
205
     */
206
    public function setItemCategoriesURL()
207
    {
208
        $client = $this->getClient();
209
        $errors = [];
210
211
        if ($client) {
212
            $result = $client->get($this->getCurrentStore());
213
214
            if (isset($result['_links']['fx:item_categories']['href'])) {
215
                $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
216
            }
217 10
218
            $errors = array_merge($errors, $client->getErrors($result));
219 10
            if (count($errors)) {
220 10
                Injector::inst()
221
                    ->get(LoggerInterface::class)->error('setItemCategoriesURL errors - ' . json_encode($errors));
222 10
            }
223 10
        }
224
    }
225 10
226
    /**
227 10
     * @return mixed
228 10
     */
229
    public function getItemCategories()
230
    {
231
        return $this->item_categories;
232
    }
233
234
    /**
235
     * @throws \Psr\Container\NotFoundExceptionInterface
236
     */
237
    public function setItemCategories()
238
    {
239
        $client = $this->getClient();
240
        $errors = [];
241
242 10
        if ($client) {
243
            $result = $client->get($this->getItemCategoriesURL());
244 10
245
            $this->item_categories = $result;
246
247
            $errors = array_merge($errors, $client->getErrors($result));
248
            if (count($errors)) {
249
                Injector::inst()
250
                    ->get(LoggerInterface::class)->error('setItemCategories errors - ' . json_encode($errors));
251
            }
252
        }
253
    }
254
255
    /**
256
     * @param $code
257
     *
258
     * @return bool
259
     *
260
     * @throws \Psr\Container\NotFoundExceptionInterface
261
     */
262
    public function getCategory($code)
263 10
    {
264
        if ($categoriesURL = $this->getItemCategoriesURL()) {
265
            $client = $this->getClient();
266
            $errors = [];
267
            $data = [
268
                'code' => $code,
269
            ];
270
            if ($result = $client->get($categoriesURL, $data)) {
271 9
                if (count($result['_embedded']['fx:item_categories']) > 0) {
272
                    $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href'];
273 9
274 9
                    return $category;
275
                }
276 9
                $errors = array_merge($errors, $client->getErrors($result));
277 9
                if (count($errors)) {
278
                    Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - ' . json_encode($errors));
279
                }
280 9
            }
281
        }
282 9
283 9
        return false;
284
    }
285
286
    /**
287
     * @param array $data
288
     *
289
     * @throws \Psr\Container\NotFoundExceptionInterface
290
     */
291
    public function putCategory($data = [])
292
    {
293
        $client = $this->getClient();
294 1
        $errors = [];
295
296 1
        if ($client) {
297 1
            if ($category = $this->getCategory($data['code'])) {
298
                $result = $client->patch($category, $data);
299 1
            } else {
300
                $result = $client->post($this->getItemCategoriesURL(), $data);
301
            }
302
            $errors = array_merge($errors, $client->getErrors($result));
303
            if (count($errors)) {
304
                Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - ' . json_encode($errors));
305
            }
306
        }
307
    }
308
309
    /**
310
     * @param array $data
311
     *
312
     * @throws \Psr\Container\NotFoundExceptionInterface
313
     */
314
    public function deleteCategory($data = [])
315
    {
316
        $client = $this->getClient();
317
        $errors = [];
318
319
        if ($category = $this->getCategory($data['code'])) {
320
            $result = $client->delete($category);
321
322
            $errors = array_merge($errors, $client->getErrors($result));
323
            if (count($errors)) {
324
                Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - ' . json_encode($errors));
325
            }
326
        }
327
    }
328
}
329