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
![]() |
|||
25 | |||
26 | /** |
||
27 | * @var |
||
28 | */ |
||
29 | private static $client_id; |
||
0 ignored issues
–
show
|
|||
30 | |||
31 | /** |
||
32 | * @var |
||
33 | */ |
||
34 | private static $client_secret; |
||
0 ignored issues
–
show
|
|||
35 | |||
36 | /** |
||
37 | * @var |
||
38 | */ |
||
39 | private static $access_token; |
||
0 ignored issues
–
show
|
|||
40 | |||
41 | /** |
||
42 | * @var |
||
43 | */ |
||
44 | private static $refresh_token; |
||
0 ignored issues
–
show
|
|||
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
|
|||
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 |