Completed
Pull Request — master (#316)
by Matthew
12:03 queued 08:57
created

FoxyStripeClient::setItemCategoriesURL()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.3244

Importance

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