1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShopifyClient\Resource; |
4
|
|
|
|
5
|
|
|
use ShopifyClient\Exception\ClientException; |
6
|
|
|
use ShopifyClient\Request; |
7
|
|
|
|
8
|
|
|
abstract class AbstractResource |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Request |
12
|
|
|
*/ |
13
|
|
|
protected $request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $actions = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $childResources = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* AbstractResource constructor. |
27
|
|
|
* @param Request $request |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct(Request $request) |
30
|
|
|
{ |
31
|
5 |
|
$this->request = $request; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $action |
36
|
|
|
* @return array |
37
|
|
|
* @throws ClientException |
38
|
|
|
*/ |
39
|
133 |
|
public function getAction(string $action): array |
40
|
|
|
{ |
41
|
133 |
|
if (!$this->hasAction($action)) { |
42
|
1 |
|
throw new ClientException(sprintf('Action: %s not found on resource. ', $action, get_called_class())); |
43
|
|
|
} |
44
|
|
|
|
45
|
132 |
|
return $this->actions[$action]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Request |
50
|
|
|
*/ |
51
|
1 |
|
public function getRequest() |
52
|
|
|
{ |
53
|
1 |
|
return $this->request; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $action |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
133 |
|
public function hasAction(string $action): bool |
61
|
|
|
{ |
62
|
133 |
|
if (!isset($this->actions[$action])) { |
63
|
3 |
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
132 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
5 |
|
public function getChildResources(): array |
73
|
|
|
{ |
74
|
5 |
|
return $this->childResources; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $action |
79
|
|
|
* @param float|null $parentId |
80
|
|
|
* @param float|null $childId |
81
|
|
|
* @param float|null $childChildId |
82
|
|
|
* @param array|null $parameters |
83
|
|
|
* @return array|bool |
84
|
|
|
*/ |
85
|
133 |
|
protected function request( |
86
|
|
|
string $action, |
87
|
|
|
float $parentId = null, |
88
|
|
|
float $childId = null, |
89
|
|
|
float $childChildId = null, |
90
|
|
|
array $parameters = [] |
91
|
|
|
) { |
92
|
133 |
|
$this->request->setResponseKey($this->getResponseKey($action)); |
93
|
|
|
|
94
|
132 |
|
return $this->request->request( |
95
|
132 |
|
$this->getMethod($action), |
96
|
131 |
|
$this->getEndpoint($action, $parentId, $childId, $childChildId), |
97
|
130 |
|
$this->getRequestOptions($action, $parameters) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $action |
103
|
|
|
* @param float|null $parentId |
104
|
|
|
* @param float|null $childId |
105
|
|
|
* @param float|null $childChildId |
106
|
|
|
* @return string |
107
|
|
|
* @throws ClientException |
108
|
|
|
*/ |
109
|
131 |
|
protected function getEndpoint( |
110
|
|
|
string $action, |
111
|
|
|
float $parentId = null, |
112
|
|
|
float $childId = null, |
113
|
|
|
float $childChildId = null |
114
|
|
|
): string { |
115
|
131 |
|
$actionData = $this->getAction($action); |
116
|
|
|
|
117
|
131 |
|
if (!isset($actionData['endpoint'])) { |
118
|
1 |
|
throw new ClientException(sprintf('Endpoint key not set for action: %s.', $action)); |
119
|
|
|
} |
120
|
|
|
|
121
|
130 |
|
return sprintf($actionData['endpoint'], $parentId, $childId, $childChildId); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $action |
126
|
|
|
* @return string |
127
|
|
|
* @throws ClientException |
128
|
|
|
*/ |
129
|
132 |
|
protected function getMethod(string $action): string |
130
|
|
|
{ |
131
|
132 |
|
$actionData = $this->getAction($action); |
132
|
|
|
|
133
|
132 |
|
if (!isset($actionData['method'])) { |
134
|
1 |
|
throw new ClientException(sprintf('Method key not set for action: %s.', $action)); |
135
|
|
|
} |
136
|
|
|
|
137
|
131 |
|
return $actionData['method']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $action |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
52 |
|
protected function getResourceKey(string $action): string |
145
|
|
|
{ |
146
|
52 |
|
$actionData = $this->getAction($action); |
147
|
|
|
|
148
|
52 |
|
if (!isset($actionData['resourceKey'])) { |
149
|
3 |
|
return ''; |
150
|
|
|
} |
151
|
|
|
|
152
|
49 |
|
return $actionData['resourceKey']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $action |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
133 |
|
protected function getResponseKey(string $action): string |
160
|
|
|
{ |
161
|
133 |
|
$actionData = $this->getAction($action); |
162
|
|
|
|
163
|
132 |
|
if (!isset($actionData['responseKey'])) { |
164
|
25 |
|
return ''; |
165
|
|
|
} |
166
|
|
|
|
167
|
108 |
|
return $actionData['responseKey']; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $action |
172
|
|
|
* @param array $parameters |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
130 |
|
private function getRequestOptions(string $action, array $parameters): array |
176
|
|
|
{ |
177
|
130 |
|
switch ($this->getMethod($action)) { |
178
|
130 |
|
case 'GET': |
179
|
|
|
return [ |
180
|
59 |
|
'query' => $parameters |
181
|
|
|
]; |
182
|
74 |
|
case 'POST': |
183
|
46 |
|
case 'PUT': |
184
|
52 |
|
if (strlen($this->getResourceKey($action)) < 1) { |
185
|
3 |
|
return []; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return [ |
189
|
49 |
|
'body' => json_encode([ |
190
|
49 |
|
$this->getResourceKey($action) => $parameters |
191
|
|
|
]) |
192
|
|
|
]; |
193
|
|
|
default: |
194
|
|
|
return [ |
195
|
23 |
|
'query' => $parameters |
196
|
|
|
]; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $method |
202
|
|
|
* @param array|null $arguments |
203
|
|
|
* @return array|bool |
204
|
|
|
*/ |
205
|
133 |
|
public function __call(string $method, array $arguments = []) |
206
|
|
|
{ |
207
|
133 |
|
return $this->request( |
208
|
133 |
|
$method, |
209
|
133 |
|
$this->getParentId($arguments), |
210
|
133 |
|
$this->getChildId($arguments), |
211
|
133 |
|
$this->getChildChildId($arguments), |
212
|
133 |
|
$this->getParameters($arguments) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $arguments |
218
|
|
|
* @return float|null |
219
|
|
|
*/ |
220
|
133 |
|
private function getParentId(array $arguments) |
221
|
|
|
{ |
222
|
133 |
|
$parentId = null; |
223
|
|
|
|
224
|
133 |
|
if (!empty($arguments[0])) { |
225
|
126 |
|
if (is_numeric($arguments[0])) { |
226
|
109 |
|
$parentId = $arguments[0]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
133 |
|
return $parentId; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $arguments |
235
|
|
|
* @return float|null |
236
|
|
|
*/ |
237
|
133 |
|
private function getChildId(array $arguments) |
238
|
|
|
{ |
239
|
133 |
|
$childId = null; |
240
|
|
|
|
241
|
133 |
|
if (!empty($arguments[1])) { |
242
|
75 |
|
if (is_numeric($arguments[1])) { |
243
|
46 |
|
$childId = $arguments[1]; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
133 |
|
return $childId; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param array $arguments |
252
|
|
|
* @return float|null |
253
|
|
|
*/ |
254
|
133 |
|
private function getChildChildId(array $arguments) |
255
|
|
|
{ |
256
|
133 |
|
$childChildId = null; |
257
|
|
|
|
258
|
133 |
|
if (!empty($arguments[2])) { |
259
|
24 |
|
if (is_numeric($arguments[2])) { |
260
|
8 |
|
$childChildId = $arguments[2]; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
133 |
|
return $childChildId; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param array $arguments |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
133 |
|
private function getParameters(array $arguments): array |
272
|
|
|
{ |
273
|
133 |
|
foreach ($arguments as $value) { |
274
|
127 |
|
if (is_array($value)) { |
275
|
127 |
|
return $value; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
82 |
|
return []; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|