Completed
Push — master ( 10c38a...3a9e00 )
by Jason
03:24
created

FoxyStripeClient::getItemCategoriesURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
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\SiteConfig\SiteConfig;
10
11
class FoxyStripeClient
12
{
13
    /**
14
     * @var string
15
     */
16
    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...
17
18
    /**
19
     * @var
20
     */
21
    private $client;
22
23
    /**
24
     * @var
25
     */
26
    private $current_store;
27
28
    /**
29
     * @var
30
     */
31
    private $item_categories_url;
32
33
    /**
34
     * @var
35
     */
36
    private $item_categories;
37
38
    /**
39
     * FoxyStripeClient constructor.
40
     * @throws \Psr\Container\NotFoundExceptionInterface
41
     * @throws \SilverStripe\ORM\ValidationException
42
     */
43 10
    public function __construct()
44
    {
45
        $config = array(
46 10
            'use_sandbox' => false,
47
        );
48
49 10
        if ($setting = FoxyStripeSetting::current_foxystripe_setting()) {
50 10
            $config['client_id'] = $setting->client_id;
0 ignored issues
show
Bug Best Practice introduced by
The property client_id does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
51 10
            $config['client_secret'] = $setting->client_secret;
0 ignored issues
show
Bug Best Practice introduced by
The property client_secret does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
52 10
            $config['refresh_token'] = $setting->refresh_token;
0 ignored issues
show
Bug Best Practice introduced by
The property refresh_token does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
53 10
            $config['access_token'] = $setting->access_token;
0 ignored issues
show
Bug Best Practice introduced by
The property access_token does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
        }
55
56
        $guzzle_config = array(
57 10
            'defaults' => array(
58
                'debug' => false,
59
                'exceptions' => false,
60
            ),
61
        );
62
63
        /*
64
         * Set up our Guzzle Client
65
         */
66 10
        $guzzle = new Client($guzzle_config);
67
        //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...
68
69
        /*
70
         * Get our FoxyClient
71
         */
72 10
        $fc = new FoxyClient($guzzle, $config);
73
74 10
        $this->setClient($fc);
75 10
        $this->setCurrentStore();
76 10
        $this->setItemCategoriesURL();
77 10
        $this->setItemCategories();
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83 10
    public function getClient()
84
    {
85 10
        return $this->client;
86
    }
87
88
    /**
89
     * @param $client
90
     *
91
     * @return $this
92
     */
93 10
    public function setClient($client)
94
    {
95 10
        $this->client = $client;
96
97 10
        return $this;
98
    }
99
100
    /**
101
     * @return bool
102
     * @throws \SilverStripe\ORM\ValidationException
103
     */
104 11
    public static function is_valid()
105
    {
106 11
        $config = FoxyStripeSetting::current_foxystripe_setting();
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 &&
0 ignored issues
show
Bug Best Practice introduced by
The property client_id does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
109 11
            $config->client_secret &&
0 ignored issues
show
Bug Best Practice introduced by
The property client_secret does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
110 11
            $config->refresh_token &&
0 ignored issues
show
Bug Best Practice introduced by
The property refresh_token does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
111 11
            $config->access_token;
0 ignored issues
show
Bug Best Practice introduced by
The property access_token does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
112
    }
113
114
    /**
115
     * @return mixed
116
     */
117 10
    public function getCurrentStore()
118
    {
119 10
        return $this->current_store;
120
    }
121
122
    /**
123
     * @throws \SilverStripe\ORM\ValidationException
124
     */
125 10
    public function setCurrentStore()
126
    {
127 10
        $client = $this->getClient();
128 10
        $config = FoxyStripeSetting::current_foxystripe_setting();
129
130 10
        $errors = array();
131
        $data = array(
132 10
            'store_domain' => $config->StoreName,
0 ignored issues
show
Bug Best Practice introduced by
The property StoreName does not exist on Dynamic\FoxyStripe\Model\FoxyStripeSetting. Since you implemented __get, consider adding a @property annotation.
Loading history...
133
        );
134
135 10
        if ($client && $result = $client->get()) {
136 10
            $errors = array_merge($errors, $client->getErrors($result));
137 10
            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
                }
152
            }
153 10
            if (count($errors)) {
154
                Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - '.json_encode($errors));
155
            }
156
        }
157
    }
158
159
    /**
160
     * @param array $data
161
     *
162
     * @throws \Psr\Container\NotFoundExceptionInterface
163
     */
164
    public function updateStore($data = [])
165
    {
166
        $client = $this->getClient();
167
        $errors = [];
168
169
        $result = $client->patch($this->getCurrentStore(), $data);
170
171
        $errors = array_merge($errors, $client->getErrors($result));
172
        if (count($errors)) {
173
            Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - '.json_encode($errors));
174
        }
175
    }
176
177
    /**
178
     * @return mixed
179
     */
180 10
    public function getItemCategoriesURL()
181
    {
182 10
        return $this->item_categories_url;
183
    }
184
185
    /**
186
     * @throws \Psr\Container\NotFoundExceptionInterface
187
     */
188 10
    public function setItemCategoriesURL()
189
    {
190 10
        $client = $this->getClient();
191 10
        $errors = [];
192
193 10
        if ($client) {
0 ignored issues
show
introduced by
$client is of type Foxy\FoxyClient\FoxyClient, thus it always evaluated to true.
Loading history...
194 10
            $result = $client->get($this->getCurrentStore());
195
196 10
            if (isset($result['_links']['fx:item_categories']['href'])) {
197
                $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
198
            }
199
200 10
            $errors = array_merge($errors, $client->getErrors($result));
201 10
            if (count($errors)) {
202
                Injector::inst()
203
                    ->get(LoggerInterface::class)->error('setItemCategoriesURL errors - '.json_encode($errors));
204
            }
205
        }
206
    }
207
208
    /**
209
     * @return mixed
210
     */
211
    public function getItemCategories()
212
    {
213
        return $this->item_categories;
214
    }
215
216
    /**
217
     * @throws \Psr\Container\NotFoundExceptionInterface
218
     */
219 10
    public function setItemCategories()
220
    {
221 10
        $client = $this->getClient();
222 10
        $errors = [];
223
224 10
        if ($client) {
0 ignored issues
show
introduced by
$client is of type Foxy\FoxyClient\FoxyClient, thus it always evaluated to true.
Loading history...
225 10
            $result = $client->get($this->getItemCategoriesURL());
226
227 10
            $this->item_categories = $result;
228
229 10
            $errors = array_merge($errors, $client->getErrors($result));
230 10
            if (count($errors)) {
231
                Injector::inst()
232
                    ->get(LoggerInterface::class)->error('setItemCategories errors - '.json_encode($errors));
233
            }
234
        }
235
    }
236
237
    /**
238
     * @param $code
239
     *
240
     * @return bool
241
     *
242
     * @throws \Psr\Container\NotFoundExceptionInterface
243
     */
244 10
    public function getCategory($code)
245
    {
246 10
        if ($categoriesURL = $this->getItemCategoriesURL()) {
247
            $client = $this->getClient();
248
            $errors = [];
249
            $data = [
250
                'code' => $code,
251
            ];
252
            if ($result = $client->get($categoriesURL, $data)) {
253
                if (count($result['_embedded']['fx:item_categories']) > 0) {
254
                    $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href'];
255
256
                    return $category;
257
                }
258
                $errors = array_merge($errors, $client->getErrors($result));
259
                if (count($errors)) {
260
                    Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - '.json_encode($errors));
261
                }
262
            }
263
        }
264
265 10
        return false;
266
    }
267
268
    /**
269
     * @param array $data
270
     *
271
     * @throws \Psr\Container\NotFoundExceptionInterface
272
     */
273 9
    public function putCategory($data = [])
274
    {
275 9
        $client = $this->getClient();
276 9
        $errors = [];
277
278 9
        if ($client) {
0 ignored issues
show
introduced by
$client is of type Foxy\FoxyClient\FoxyClient, thus it always evaluated to true.
Loading history...
279 9
            if ($category = $this->getCategory($data['code'])) {
280
                $result = $client->patch($category, $data);
281
            } else {
282 9
                $result = $client->post($this->getItemCategoriesURL(), $data);
283
            }
284 9
            $errors = array_merge($errors, $client->getErrors($result));
285 9
            if (count($errors)) {
286
                Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - '.json_encode($errors));
287
            }
288
        }
289
    }
290
291
    /**
292
     * @param array $data
293
     *
294
     * @throws \Psr\Container\NotFoundExceptionInterface
295
     */
296 1
    public function deleteCategory($data = [])
297
    {
298 1
        $client = $this->getClient();
299 1
        $errors = [];
300
301 1
        if ($category = $this->getCategory($data['code'])) {
302
            $result = $client->delete($category);
303
304
            $errors = array_merge($errors, $client->getErrors($result));
305
            if (count($errors)) {
306
                Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - '.json_encode($errors));
307
            }
308
        }
309
    }
310
}
311