Completed
Pull Request — master (#304)
by Jason
11:27
created

FoxyStripeClient::getCategory()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 18.9614

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
ccs 3
cts 17
cp 0.1765
rs 8.7624
c 1
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
crap 18.9614
1
<?php
2
3
namespace Dynamic\FoxyStripe\Model;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;
7
use Foxy\FoxyClient\FoxyClient;
8
9
class FoxyStripeClient extends \Object
10
{
11
    /**
12
     * @var
13
     */
14
    private $client;
15
16
    /**
17
     * @var
18
     */
19
    private $current_store;
20
21
    /**
22
     * @var
23
     */
24
    private $item_categories_url;
25
26
    /**
27
     * @var
28
     */
29
    private $item_categories;
30
31
    /**
32
     * FoxyStripeClient constructor.
33
     */
34 41
    public function __construct()
35
    {
36
        $config = array(
37
            'use_sandbox' => false
38 41
        );
39
40 41
        $site_config = \SiteConfig::current_site_config();
41 41
        if ($site_config) {
42 41
            $config['client_id'] = $site_config->client_id;
43 41
            $config['client_secret'] = $site_config->client_secret;
44 41
            $config['refresh_token'] = $site_config->refresh_token;
45 41
            $config['access_token'] = $site_config->access_token;
46 41
        }
47
48
        $guzzle_config = array(
49
            'defaults' => array(
50 41
                'debug' => false,
51
                'exceptions' => false
52 41
            )
53 41
        );
54
55
        /**
56
         * Set up our Guzzle Client
57
         */
58 41
        $guzzle = new Client($guzzle_config);
59 41
        CacheSubscriber::attach($guzzle);
60
61
        /**
62
         * Get our FoxyClient
63
         */
64 41
        $fc = new FoxyClient($guzzle, $config);
65
66 41
        $this->setClient($fc);
67 41
        $this->setCurrentStore();
68 41
        $this->setItemCategoriesURL();
69 41
        $this->setItemCategories();
70 41
    }
71
72
    /**
73
     * @return mixed
74
     */
75 41
    public function getClient()
76
    {
77 41
        return $this->client;
78 1
    }
79
80
    /**
81
     * @param $client
82
     * @return $this
83
     */
84 41
    public function setClient($client)
85
    {
86 41
        $this->client = $client;
87 41
        return $this;
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93 41
    public function getCurrentStore()
94
    {
95 41
        return $this->current_store;
96
    }
97
98
    /**
99
     *
100
     */
101 41
    public function setCurrentStore()
102
    {
103 41
        $client = $this->getClient();
104 41
        $config = \SiteConfig::current_site_config();
105
106 41
        $errors = array();
107
        $data = array(
108 41
            'store_domain' => $config->StoreName,
109 41
        );
110
111 41
        if ($result = $client->get()) {
112 41
            $errors = array_merge($errors, $client->getErrors($result));
113 41
            if ($reporting_uri = $client->getLink('fx:reporting')) {
114
                $errors = array_merge($errors, $client->getErrors($reporting_uri));
115
                if ($result = $client->get($reporting_uri)) {
116
                    $errors = array_merge($errors, $client->getErrors($result));
117
                    if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) {
118
                        $errors = array_merge($errors, $client->getErrors($store_exists_uri));
119
                        if ($result = $client->get($store_exists_uri, $data)) {
120
                            $errors = array_merge($errors, $client->getErrors($result));
121
                            if ($store = $client->getLink('fx:store')) {
122
                                $errors = array_merge($errors, $client->getErrors($store));
123
                                $this->current_store = $store;
124
                            }
125
                        }
126
                    }
127
                }
128
            }
129 41
            if (count($errors)) {
130
                \SS_Log::log('setCurrentStore errors - ' . json_encode($errors), \SS_Log::WARN);
131
            }
132 41
        }
133 41
    }
134
135
    /**
136
     * @param array $data
137
     */
138 View Code Duplication
    public function updateStore($data = []) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
        $client = $this->getClient();
140
        $errors = [];
141
        $result = $client->patch($this->getCurrentStore(), $data);
142
143
        $errors = array_merge($errors, $client->getErrors($result));
144
        if (count($errors)) {
145
            \SS_Log::log('updateStore errors - ' . json_encode($errors), \SS_Log::WARN);
146
        }
147
    }
148
149
    /**
150
     * @return mixed
151
     */
152 41
    public function getItemCategoriesURL()
153
    {
154 41
        return $this->item_categories_url;
155
    }
156
157
    /**
158
     *
159
     */
160 41
    public function setItemCategoriesURL()
161
    {
162 41
        $client = $this->getClient();
163 41
        $errors = [];
164 41
        $result = $client->get($this->getCurrentStore());
165
166 41
        if (isset($result['_links']['fx:item_categories']['href'])) {
167
            $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
168
        }
169
170 41
        $errors = array_merge($errors, $client->getErrors($result));
171 41
        if (count($errors)) {
172
            \SS_Log::log('setItemCategoriesURL errors - ' . json_encode($errors), \SS_Log::WARN);
173
        }
174 41
    }
175
176
    /**
177
     * @return mixed
178
     */
179
    public function getItemCategories()
180
    {
181
        return $this->item_categories;
182
    }
183
184
    /**
185
     *
186
     */
187 41 View Code Duplication
    public function setItemCategories()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189 41
        $client = $this->getClient();
190 41
        $errors = [];
191 41
        $result = $client->get($this->getItemCategoriesURL());
192
193 41
        $this->item_categories = $result;
194
195 41
        $errors = array_merge($errors, $client->getErrors($result));
196 41
        if (count($errors)) {
197
            \SS_Log::log('setItemCategories errors - ' . json_encode($errors), \SS_Log::WARN);
198
        }
199 41
    }
200
201
    /**
202
     * @param $code
203
     * @return mixed
204
     */
205 41
    public function getCategory($code)
206
    {
207 41
        if ($categoriesURL = $this->getItemCategoriesURL()) {
208
            $client = $this->getClient();
209
            $errors = [];
210
            $data = [
211
                'code' => $code,
212
            ];
213
            if ($result = $client->get($categoriesURL, $data)) {
214
                if (count($result['_embedded']['fx:item_categories']) > 0) {
215
                    $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href'];
216
                    return $category;
217
                }
218
                $errors = array_merge($errors, $client->getErrors($result));
219
                if (count($errors)) {
220
                    \SS_Log::log('getCategory errors - ' . json_encode($errors), \SS_Log::WARN);
221
                }
222
            }
223
        }
224 41
        return false;
225
    }
226
227
    /**
228
     * @param array $data
229
     */
230 41
    public function putCategory($data = [])
231
    {
232 41
        $client = $this->getClient();
233 41
        $errors = [];
234
235 41
        if ($category = $this->getCategory($data['code'])) {
236
            $result = $client->patch($category, $data);
237
        } else {
238 41
            $result = $client->post($this->getItemCategoriesURL(), $data);
239
        }
240 41
        $errors = array_merge($errors, $client->getErrors($result));
241 41
        if (count($errors)) {
242 41
            \SS_Log::log('putCategory errors - ' . json_encode($errors), \SS_Log::WARN);
243 41
        }
244 41
    }
245
246
    /**
247
     * @param array $data
248
     */
249 1 View Code Duplication
    public function deleteCategory($data = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
    {
251 1
        $client = $this->getClient();
252 1
        $errors = [];
253
254 1
        if ($category = $this->getCategory($data['code'])) {
255
            $result = $client->delete($category);
256
257
            $errors = array_merge($errors, $client->getErrors($result));
258
            if (count($errors)) {
259
                \SS_Log::log('deleteCategory errors - ' . json_encode($errors), \SS_Log::WARN);
260
            }
261
        }
262 1
    }
263
}
264