Completed
Pull Request — master (#304)
by Jason
09:34
created

FoxyStripeClient::getCurrentStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 29
    public function __construct()
35
    {
36
        $config = array(
37
            'use_sandbox' => false
38 29
        );
39
40 29
        $site_config = \SiteConfig::current_site_config();
41 29
        if ($site_config) {
42 29
            $config['client_id'] = $site_config->client_id;
43 29
            $config['client_secret'] = $site_config->client_secret;
44 29
            $config['refresh_token'] = $site_config->refresh_token;
45 29
            $config['access_token'] = $site_config->access_token;
46 29
        }
47
48
        $guzzle_config = array(
49
            'defaults' => array(
50 29
                'debug' => false,
51
                'exceptions' => false
52 29
            )
53 29
        );
54
55
        /**
56
         * Set up our Guzzle Client
57
         */
58 29
        $guzzle = new Client($guzzle_config);
59 29
        CacheSubscriber::attach($guzzle);
60
61
        /**
62
         * Get our FoxyClient
63
         */
64 29
        $fc = new FoxyClient($guzzle, $config);
65
66 29
        $this->setClient($fc);
67 29
        $this->setCurrentStore();
68 29
        $this->setItemCategoriesURL();
69 29
        $this->setItemCategories();
70 29
    }
71
72
    /**
73
     * @return mixed
74
     */
75 29
    public function getClient()
76
    {
77 29
        return $this->client;
78 1
    }
79
80
    /**
81
     * @param $client
82
     * @return $this
83
     */
84 29
    public function setClient($client)
85
    {
86 29
        $this->client = $client;
87 29
        return $this;
88
    }
89
90
    /**
91
     * @return mixed
92
     */
93 29
    public function getCurrentStore()
94
    {
95 29
        return $this->current_store;
96
    }
97
98
    /**
99
     *
100
     */
101 29
    public function setCurrentStore()
102
    {
103 29
        $client = $this->getClient();
104 29
        $config = \SiteConfig::current_site_config();
105
106 29
        $errors = array();
107
        $data = array(
108 29
            'store_domain' => $config->StoreName,
109 29
        );
110
111 29
        if ($result = $client->get()) {
112 29
            $errors = array_merge($errors, $client->getErrors($result));
113 29
            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 29
            if (count($errors)) {
130
                \SS_Log::log('setCurrentStore errors - ' . json_encode($errors), \SS_Log::WARN);
131
            }
132 29
        }
133 29
    }
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 29
    public function getItemCategoriesURL()
153
    {
154 29
        return $this->item_categories_url;
155
    }
156
157
    /**
158
     *
159
     */
160 29
    public function setItemCategoriesURL()
161
    {
162 29
        $client = $this->getClient();
163 29
        $errors = [];
164 29
        $result = $client->get($this->getCurrentStore());
165
166 29
        if (isset($result['_links']['fx:item_categories']['href'])) {
167
            $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
168
        }
169
170 29
        $errors = array_merge($errors, $client->getErrors($result));
171 29
        if (count($errors)) {
172
            \SS_Log::log('setItemCategoriesURL errors - ' . json_encode($errors), \SS_Log::WARN);
173
        }
174 29
    }
175
176
    /**
177
     * @return mixed
178
     */
179
    public function getItemCategories()
180
    {
181
        return $this->item_categories;
182
    }
183
184
    /**
185
     *
186
     */
187 29 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 29
        $client = $this->getClient();
190 29
        $errors = [];
191 29
        $result = $client->get($this->getItemCategoriesURL());
192
193 29
        $this->item_categories = $result;
194
195 29
        $errors = array_merge($errors, $client->getErrors($result));
196 29
        if (count($errors)) {
197
            \SS_Log::log('setItemCategories errors - ' . json_encode($errors), \SS_Log::WARN);
198
        }
199 29
    }
200
201
    /**
202
     * @param $code
203
     * @return mixed
204
     */
205 29
    public function getCategory($code)
206
    {
207 29
        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 29
        return false;
225
    }
226
227
    /**
228
     * @param array $data
229
     */
230 29
    public function putCategory($data = [])
231
    {
232 29
        $client = $this->getClient();
233 29
        $errors = [];
234
235 29
        if ($category = $this->getCategory($data['code'])) {
236
            $result = $client->patch($category, $data);
237
        } else {
238 29
            $result = $client->post($this->getItemCategoriesURL(), $data);
239
        }
240 29
        $errors = array_merge($errors, $client->getErrors($result));
241 29
        if (count($errors)) {
242 29
            \SS_Log::log('putCategory errors - ' . json_encode($errors), \SS_Log::WARN);
243 29
        }
244 29
    }
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