Passed
Pull Request — master (#373)
by Nic
13:50
created

FoxyStripeClient::is_valid()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 5
nop 0
dl 0
loc 9
rs 9.6111
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 5
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()
155
                    ->get(LoggerInterface::class)->error('setCurrentStore errors - ' . json_encode($errors));
156
            }
157
        }
158
    }
159
160
    /**
161
     * @param $uri
162
     * @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...
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
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...
172
     */
173
    public function patch($uri, $data = null)
174
    {
175
        $this->getClient()->patch($uri, $data);
176
    }
177
178 10
    /**
179
     * @param array $data
180 10
     *
181
     * @throws \Psr\Container\NotFoundExceptionInterface
182
     */
183
    public function updateStore($data = [])
184
    {
185
        $client = $this->getClient();
186 10
        $errors = [];
187
188 10
        $result = $client->patch($this->getCurrentStore(), $data);
189 10
190
        $errors = array_merge($errors, $client->getErrors($result));
191 10
        if (count($errors)) {
192 10
            Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - ' . json_encode($errors));
193
        }
194 10
    }
195
196
    /**
197
     * @return mixed
198 10
     */
199 10
    public function getItemCategoriesURL()
200
    {
201
        return $this->item_categories_url;
202
    }
203
204
    /**
205
     * @throws \Psr\Container\NotFoundExceptionInterface
206
     */
207
    public function setItemCategoriesURL()
208
    {
209
        $client = $this->getClient();
210
        $errors = [];
211
212
        if ($client) {
213
            $result = $client->get($this->getCurrentStore());
214
215
            if (isset($result['_links']['fx:item_categories']['href'])) {
216
                $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
217 10
            }
218
219 10
            $errors = array_merge($errors, $client->getErrors($result));
220 10
            if (count($errors)) {
221
                Injector::inst()
222 10
                    ->get(LoggerInterface::class)->error('setItemCategoriesURL errors - ' . json_encode($errors));
223 10
            }
224
        }
225 10
    }
226
227 10
    /**
228 10
     * @return mixed
229
     */
230
    public function getItemCategories()
231
    {
232
        return $this->item_categories;
233
    }
234
235
    /**
236
     * @throws \Psr\Container\NotFoundExceptionInterface
237
     */
238
    public function setItemCategories()
239
    {
240
        $client = $this->getClient();
241
        $errors = [];
242 10
243
        if ($client) {
244 10
            $result = $client->get($this->getItemCategoriesURL());
245
246
            $this->item_categories = $result;
247
248
            $errors = array_merge($errors, $client->getErrors($result));
249
            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
        if ($categoriesURL = $this->getItemCategoriesURL()) {
266
            $client = $this->getClient();
267
            $errors = [];
268
            $data = [
269
                'code' => $code,
270
            ];
271 9
            if ($result = $client->get($categoriesURL, $data)) {
272
                if (count($result['_embedded']['fx:item_categories']) > 0) {
273 9
                    $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href'];
274 9
275
                    return $category;
276 9
                }
277 9
                $errors = array_merge($errors, $client->getErrors($result));
278
                if (count($errors)) {
279
                    Injector::inst()
280 9
                        ->get(LoggerInterface::class)->error('getCategory errors - ' . json_encode($errors));
281
                }
282 9
            }
283 9
        }
284
285
        return false;
286
    }
287
288
    /**
289
     * @param array $data
290
     *
291
     * @throws \Psr\Container\NotFoundExceptionInterface
292
     */
293
    public function putCategory($data = [])
294 1
    {
295
        $client = $this->getClient();
296 1
        $errors = [];
297 1
298
        if ($client) {
299 1
            if ($category = $this->getCategory($data['code'])) {
300
                $result = $client->patch($category, $data);
301
            } else {
302
                $result = $client->post($this->getItemCategoriesURL(), $data);
303
            }
304
            $errors = array_merge($errors, $client->getErrors($result));
305
            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
    public function deleteCategory($data = [])
317
    {
318
        $client = $this->getClient();
319
        $errors = [];
320
321
        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