Passed
Pull Request — master (#309)
by Jason
06:15 queued 01:51
created

FoxyStripeClient::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 2
nc 1
nop 1
crap 1
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\SiteConfig\SiteConfig;
10
11
class FoxyStripeClient
12
{
13
    /**
14
     * @var string
15
     */
16
    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...
17
18
    /**
19
     * @var
20
     */
21
    private $client;
22
23
    /**
24
     * @var
25
     */
26
    private $current_store;
27
28
    /**
29
     * @var
30
     */
31
    private $item_categories_url;
32
33
    /**
34
     * @var
35
     */
36
    private $item_categories;
37
38
    /**
39
     * FoxyStripeClient constructor.
40
     *
41
     * @throws \Psr\Container\NotFoundExceptionInterface
42
     */
43 49
    public function __construct()
44
    {
45
        $config = array(
46 49
            'use_sandbox' => false,
47
        );
48
49 49
        if ($site_config = SiteConfig::current_site_config()) {
50 49
            $config['client_id'] = $site_config->client_id;
51 49
            $config['client_secret'] = $site_config->client_secret;
52 49
            $config['refresh_token'] = $site_config->refresh_token;
53 49
            $config['access_token'] = $site_config->access_token;
54
        }
55
56
        $guzzle_config = array(
57 49
            'defaults' => array(
58
                'debug' => false,
59
                'exceptions' => false,
60
            ),
61
        );
62
63
        /*
64
         * Set up our Guzzle Client
65
         */
66 49
        $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 49
        $fc = new FoxyClient($guzzle, $config);
73
74 49
        $this->setClient($fc);
75 49
        $this->setCurrentStore();
76 49
        $this->setItemCategoriesURL();
77 49
        $this->setItemCategories();
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83 49
    public function getClient()
84
    {
85 49
        return $this->client;
86
    }
87
88
    /**
89
     * @param $client
90
     *
91
     * @return $this
92
     */
93 49
    public function setClient($client)
94
    {
95 49
        $this->client = $client;
96
97 49
        return $this;
98
    }
99
100
    /**
101
     * @return mixed
102
     */
103 49
    public function getCurrentStore()
104
    {
105 49
        return $this->current_store;
106
    }
107
108
    /**
109
     * @throws \Psr\Container\NotFoundExceptionInterface
110
     */
111 49
    public function setCurrentStore()
112
    {
113 49
        $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...
114 49
        $config = SiteConfig::current_site_config();
115
116 49
        $errors = array();
117
        $data = array(
118 49
            'store_domain' => $config->StoreName,
119
        );
120
121 49
        if ($client && $result = $client->get()) {
122 49
            $errors = array_merge($errors, $client->getErrors($result));
123 49
            if ($reporting_uri = $client->getLink('fx:reporting')) {
124
                $errors = array_merge($errors, $client->getErrors($reporting_uri));
125
                if ($result = $client->get($reporting_uri)) {
126
                    $errors = array_merge($errors, $client->getErrors($result));
127
                    if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) {
128
                        $errors = array_merge($errors, $client->getErrors($store_exists_uri));
129
                        if ($result = $client->get($store_exists_uri, $data)) {
130
                            $errors = array_merge($errors, $client->getErrors($result));
131
                            if ($store = $client->getLink('fx:store')) {
132
                                $errors = array_merge($errors, $client->getErrors($store));
133
                                $this->current_store = $store;
134
                            }
135
                        }
136
                    }
137
                }
138
            }
139 49
            if (count($errors)) {
140
                Injector::inst()->get(LoggerInterface::class)->error('setCurrentStore errors - '.json_encode($errors));
141
            }
142
        }
143
    }
144
145
    /**
146
     * @param array $data
147
     *
148
     * @throws \Psr\Container\NotFoundExceptionInterface
149
     */
150
    public function updateStore($data = [])
151
    {
152
        $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...
153
        $errors = [];
154
155
        $result = $client->patch($this->getCurrentStore(), $data);
156
157
        $errors = array_merge($errors, $client->getErrors($result));
158
        if (count($errors)) {
159
            Injector::inst()->get(LoggerInterface::class)->error('updateStore errors - '.json_encode($errors));
160
        }
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166 49
    public function getItemCategoriesURL()
167
    {
168 49
        return $this->item_categories_url;
169
    }
170
171
    /**
172
     * @throws \Psr\Container\NotFoundExceptionInterface
173
     */
174 49
    public function setItemCategoriesURL()
175
    {
176 49
        $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...
177 49
        $errors = [];
178
179 49
        if ($client) {
180 49
            $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...
181
182 49
            if (isset($result['_links']['fx:item_categories']['href'])) {
183
                $this->item_categories_url = $result['_links']['fx:item_categories']['href'];
184
            }
185
186 49
            $errors = array_merge($errors, $client->getErrors($result));
187 49
            if (count($errors)) {
188
                Injector::inst()->get(LoggerInterface::class)->error('setItemCategoriesURL errors - '.json_encode($errors));
189
            }
190
        }
191
    }
192
193
    /**
194
     * @return mixed
195
     */
196
    public function getItemCategories()
197
    {
198
        return $this->item_categories;
199
    }
200
201
    /**
202
     * @throws \Psr\Container\NotFoundExceptionInterface
203
     */
204 49
    public function setItemCategories()
205
    {
206 49
        $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...
207 49
        $errors = [];
208
209 49
        if ($client) {
210 49
            $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...
211
212 49
            $this->item_categories = $result;
213
214 49
            $errors = array_merge($errors, $client->getErrors($result));
215 49
            if (count($errors)) {
216
                Injector::inst()->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();
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...
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();
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...
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();
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...
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