Completed
Pull Request — master (#304)
by Jason
12:24
created

FoxyStripeClient::getItemCategories()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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