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