Passed
Pull Request — master (#309)
by Jason
13:47
created

FoxyStripeClient::getItemCategoriesURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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\ORM\DataObject;
10
use SilverStripe\SiteConfig\SiteConfig;
11
12
class FoxyStripeClient
13
{
14
    /**
15
     * @var string
16
     */
17
    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...
18
19
    /**
20
     * @var
21
     */
22
    private $client;
23
24
    /**
25
     * @var
26
     */
27
    private $current_store;
28
29
    /**
30
     * @var
31
     */
32
    private $item_categories_url;
33
34
    /**
35
     * @var
36
     */
37
    private $item_categories;
38
39
    /**
40
     * FoxyStripeClient constructor.
41
     * @throws \Psr\Container\NotFoundExceptionInterface
42
     */
43
    public function __construct()
44
    {
45
        $config = array(
46
            'use_sandbox' => false
47
        );
48
49
        if ($site_config = SiteConfig::current_site_config()) {
50
            $config['client_id'] = $site_config->client_id;
51
            $config['client_secret'] = $site_config->client_secret;
52
            $config['refresh_token'] = $site_config->refresh_token;
53
            $config['access_token'] = $site_config->access_token;
54
        }
55
56
        $guzzle_config = array(
57
            'defaults' => array(
58
                'debug' => false,
59
                'exceptions' => false
60
            )
61
        );
62
63
        /**
64
         * Set up our Guzzle Client
65
         */
66
        $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
        $fc = new FoxyClient($guzzle, $config);
73
74
        $this->setClient($fc);
75
        $this->setCurrentStore();
76
        $this->setItemCategoriesURL();
77
        $this->setItemCategories();
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function getClient()
84
    {
85
        return $this->client;
86
    }
87
88
    /**
89
     * @param $client
90
     * @return $this
91
     */
92
    public function setClient($client)
93
    {
94
        $this->client = $client;
95
        return $this;
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getCurrentStore()
102
    {
103
        return $this->current_store;
104
    }
105
106
    /**
107
     * @throws \Psr\Container\NotFoundExceptionInterface
108
     */
109
    public function setCurrentStore()
110
    {
111
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
112
        $config = SiteConfig::current_site_config();
113
114
        $errors = array();
115
        $data = array(
116
            'store_domain' => $config->StoreName,
117
        );
118
119
        if ($client && $result = $client->get()) {
120
            $errors = array_merge($errors, $client->getErrors($result));
121
            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
            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
     * @throws \Psr\Container\NotFoundExceptionInterface
146
     */
147
    public function updateStore($data = []) {
148
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
149
        $errors = [];
150
151
        $result = $client->patch($this->getCurrentStore(), $data);
152
153
        $errors = array_merge($errors, $client->getErrors($result));
154
        if (count($errors)) {
155
            Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - ' . json_encode($errors));
156
        }
157
    }
158
159
    /**
160
     * @return mixed
161
     */
162
    public function getItemCategoriesURL()
163
    {
164
        return $this->item_categories_url;
165
    }
166
167
    /**
168
     * @throws \Psr\Container\NotFoundExceptionInterface
169
     */
170
    public function setItemCategoriesURL()
171
    {
172
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
173
        $errors = [];
174
175
        if ($client) {
176
            $result = $client->get($this->getCurrentStore());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCurrentStore() targeting Dynamic\FoxyStripe\Model...ient::getCurrentStore() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
177
178
            if (isset($result['_links']['fx:item_categories']['href'])) {
179
                $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
180
            }
181
182
            $errors = array_merge($errors, $client->getErrors($result));
183
            if (count($errors)) {
184
                Injector::inst()->get(LoggerInterface::class)->error('setItemCategoriesURL errors - ' . json_encode($errors));
185
            }
186
        }
187
    }
188
189
    /**
190
     * @return mixed
191
     */
192
    public function getItemCategories()
193
    {
194
        return $this->item_categories;
195
    }
196
197
    /**
198
     * @throws \Psr\Container\NotFoundExceptionInterface
199
     */
200
    public function setItemCategories()
201
    {
202
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
203
        $errors = [];
204
205
        if ($client) {
206
            $result = $client->get($this->getItemCategoriesURL());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getItemCategoriesURL() targeting Dynamic\FoxyStripe\Model...:getItemCategoriesURL() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
207
208
            $this->item_categories = $result;
209
210
            $errors = array_merge($errors, $client->getErrors($result));
211
            if (count($errors)) {
212
                Injector::inst()->get(LoggerInterface::class)->error('setItemCategories errors - ' . json_encode($errors));
213
            }
214
        }
215
    }
216
217
    /**
218
     * @param $code
219
     * @return bool
220
     * @throws \Psr\Container\NotFoundExceptionInterface
221
     */
222
    public function getCategory($code)
223
    {
224
        if ($categoriesURL = $this->getItemCategoriesURL()) {
225
            $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
226
            $errors = [];
227
            $data = [
228
                'code' => $code,
229
            ];
230
            if ($result = $client->get($categoriesURL, $data)) {
231
                if (count($result['_embedded']['fx:item_categories']) > 0) {
232
                    $category = $result['_embedded']['fx:item_categories'][0]['_links']['self']['href'];
233
                    return $category;
234
                }
235
                $errors = array_merge($errors, $client->getErrors($result));
236
                if (count($errors)) {
237
                    Injector::inst()->get(LoggerInterface::class)->error('getCategory errors - ' . json_encode($errors));
238
                }
239
            }
240
        }
241
        return false;
242
    }
243
244
    /**
245
     * @param array $data
246
     * @throws \Psr\Container\NotFoundExceptionInterface
247
     */
248
    public function putCategory($data = [])
249
    {
250
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
251
        $errors = [];
252
253
        if ($client) {
254
            if ($category = $this->getCategory($data['code'])) {
255
                $result = $client->patch($category, $data);
256
            } else {
257
                $result = $client->post($this->getItemCategoriesURL(), $data);
258
            }
259
            $errors = array_merge($errors, $client->getErrors($result));
260
            if (count($errors)) {
261
                Injector::inst()->get(LoggerInterface::class)->error('putCategory errors - ' . json_encode($errors));
262
            }
263
        }
264
    }
265
266
    /**
267
     * @param array $data
268
     * @throws \Psr\Container\NotFoundExceptionInterface
269
     */
270
    public function deleteCategory($data = [])
271
    {
272
        $client = $this->getClient();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $client is correct as $this->getClient() targeting Dynamic\FoxyStripe\Model...ripeClient::getClient() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
273
        $errors = [];
274
275
        if ($category = $this->getCategory($data['code'])) {
276
            $result = $client->delete($category);
277
278
            $errors = array_merge($errors, $client->getErrors($result));
279
            if (count($errors)) {
280
                Injector::inst()->get(LoggerInterface::class)->error('deleteCategory errors - ' . json_encode($errors));
281
            }
282
        }
283
    }
284
}
285