Passed
Push — master ( 1b8ed1...1f91fc )
by Jason
02:02
created

APIClient   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 57
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 1
B setCurrentStore() 0 31 9
A getClient() 0 3 1
A setClient() 0 5 1
A is_valid() 0 9 5
A getCurrentStore() 0 3 1
1
<?php
2
3
namespace Dynamic\Foxy\API\Client;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use Dynamic\Foxy\Model\Setting;
7
use Foxy\FoxyClient\FoxyClient;
8
use GuzzleHttp\Client;
9
use Psr\Log\LoggerInterface;
10
use SilverStripe\Core\Config\Configurable;
11
use SilverStripe\Core\Extensible;
12
use SilverStripe\Core\Injector\Injectable;
13
use SilverStripe\Core\Injector\Injector;
14
15
class APIClient
16
{
17
    use Configurable;
18
    use Injectable;
19
    use Extensible;
20
21
    /**
22
     * @var
23
     */
24
    private static $enable_api;
0 ignored issues
show
introduced by
The private property $enable_api is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var
28
     */
29
    private static $client_id;
0 ignored issues
show
introduced by
The private property $client_id is not used, and could be removed.
Loading history...
30
31
    /**
32
     * @var
33
     */
34
    private static $client_secret;
0 ignored issues
show
introduced by
The private property $client_secret is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var
38
     */
39
    private static $access_token;
0 ignored issues
show
introduced by
The private property $access_token is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var
43
     */
44
    private static $refresh_token;
0 ignored issues
show
introduced by
The private property $refresh_token is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var
48
     */
49
    private $client;
50
51
    /**
52
     * @var
53
     */
54
    private $current_store;
55
56
    /**
57
     * APIClient constructor.
58
     */
59
    public function __construct()
60
    {
61
        $config = [
62
            'use_sandbox' => false,
63
        ];
64
65
        $config['client_id'] = $this->config()->get('client_id');
66
        $config['client_secret'] = $this->config()->get('client_secret');
67
        $config['refresh_token'] = $this->config()->get('refresh_token');
68
        $config['access_token'] = $this->config()->get('access_token');
69
        $config['access_token_expires'] = 7200;
70
71
        $guzzle_config = [
72
            'defaults' => [
73
                'debug' => false,
74
                'exceptions' => false,
75
            ],
76
        ];
77
78
        $guzzle = new Client($guzzle_config);
79
80
        $fc = new FoxyClient($guzzle, $config);
81
82
        $this->setClient($fc);
83
        $this->setCurrentStore();
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89
    public function getClient()
90
    {
91
        return $this->client;
92
    }
93
94
    /**
95
     * @param $client
96
     *
97
     * @return $this
98
     */
99
    public function setClient($client)
100
    {
101
        $this->client = $client;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return bool
108
     * @throws \SilverStripe\ORM\ValidationException
109
     */
110
    public static function is_valid()
111
    {
112
        $helper = FoxyHelper::create();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
113
114
        return self::config()->get('enable_api') &&
115
            self::config()->get('client_id') &&
116
            self::config()->get('client_secret') &&
117
            self::config()->get('refresh_token') &&
118
            self::config()->get('access_token');
119
    }
120
121
    /**
122
     * @return mixed
123
     */
124
    public function getCurrentStore()
125
    {
126
        return $this->current_store;
127
    }
128
129
    /**
130
     * @throws \SilverStripe\ORM\ValidationException
131
     */
132
    public function setCurrentStore()
133
    {
134
        $client = $this->getClient();
135
        $helper = FoxyHelper::create();
136
137
        $errors = [];
138
        $data = [
139
            'store_domain' => $helper->getStoreCartURL(),
140
        ];
141
142
        if ($client && $result = $client->get()) {
143
            $errors = array_merge($errors, $client->getErrors($result));
144
            if ($reporting_uri = $client->getLink('fx:reporting')) {
145
                $errors = array_merge($errors, $client->getErrors($reporting_uri));
146
                if ($result = $client->get($reporting_uri)) {
147
                    $errors = array_merge($errors, $client->getErrors($result));
148
                    if ($store_exists_uri = $client->getLink('fx:reporting_store_domain_exists')) {
149
                        $errors = array_merge($errors, $client->getErrors($store_exists_uri));
150
                        if ($result = $client->get($store_exists_uri, $data)) {
151
                            $errors = array_merge($errors, $client->getErrors($result));
152
                            if ($store = $client->getLink('fx:store')) {
153
                                $errors = array_merge($errors, $client->getErrors($store));
154
                                $this->current_store = $store;
155
                            }
156
                        }
157
                    }
158
                }
159
            }
160
            if (count($errors)) {
161
                Injector::inst()
162
                    ->get(LoggerInterface::class)->error('setCurrentStore errors - ' . json_encode($errors));
163
            }
164
        }
165
    }
166
}
167