1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShopifyClient\Resource; |
4
|
|
|
|
5
|
|
|
use ShopifyClient\Action\Action; |
6
|
|
|
use ShopifyClient\Action\ActionCollection; |
7
|
|
|
use ShopifyClient\Action\ActionInterface; |
8
|
|
|
use ShopifyClient\Exception\ClientException; |
9
|
|
|
use ShopifyClient\Request; |
10
|
|
|
|
11
|
|
|
abstract class AbstractResource |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Request |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ActionCollection |
20
|
|
|
*/ |
21
|
|
|
protected $actions = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $childResources = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AbstractResource constructor. |
30
|
|
|
* @param Request $request |
31
|
|
|
*/ |
32
|
5 |
|
public function __construct(Request $request) |
33
|
|
|
{ |
34
|
5 |
|
$this->request = $request; |
35
|
5 |
|
$this->actions = new ActionCollection(); |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $action |
40
|
|
|
* @return ActionInterface |
41
|
|
|
* @throws ClientException |
42
|
|
|
*/ |
43
|
133 |
|
public function getAction(string $action): ActionInterface |
44
|
|
|
{ |
45
|
133 |
|
return $this->actions->get($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
|
17 |
|
public function hasAction(string $action): bool |
61
|
|
|
{ |
62
|
17 |
|
if (!$this->actions->has($action)) { |
63
|
1 |
|
return false; |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
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 |
|
$action = $this->getAction($action); |
93
|
|
|
|
94
|
130 |
|
$this->request->setResponseKey($action->getResponseKey()); |
95
|
|
|
|
96
|
130 |
|
return $this->request->request( |
97
|
130 |
|
$action->getMethod(), |
98
|
130 |
|
$this->getEndpoint($action, $parentId, $childId, $childChildId), |
|
|
|
|
99
|
130 |
|
$this->getRequestOptions($action, $parameters) |
|
|
|
|
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param Action $action |
105
|
|
|
* @param float|null $parentId |
106
|
|
|
* @param float|null $childId |
107
|
|
|
* @param float|null $childChildId |
108
|
|
|
* @return string |
109
|
|
|
* @throws ClientException |
110
|
|
|
*/ |
111
|
130 |
|
protected function getEndpoint( |
112
|
|
|
Action $action, |
113
|
|
|
float $parentId = null, |
114
|
|
|
float $childId = null, |
115
|
|
|
float $childChildId = null |
116
|
|
|
): string { |
117
|
130 |
|
return sprintf($action->getEndpoint(), $parentId, $childId, $childChildId); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Action $action |
122
|
|
|
* @param array $parameters |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
130 |
|
private function getRequestOptions(Action $action, array $parameters): array |
126
|
|
|
{ |
127
|
130 |
|
$method = $action->getMethod(); |
128
|
130 |
|
$resourceKey = $action->getResourceKey(); |
129
|
|
|
|
130
|
130 |
|
if ($method === 'POST' || $method === 'PUT') { |
131
|
52 |
|
return strlen($resourceKey) < 1 ? [] : ['body' => json_encode([$resourceKey => $parameters])]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return [ |
135
|
82 |
|
'query' => $parameters |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $method |
141
|
|
|
* @param array|null $arguments |
142
|
|
|
* @return array|bool |
143
|
|
|
*/ |
144
|
133 |
|
public function __call(string $method, array $arguments = []) |
145
|
|
|
{ |
146
|
133 |
|
return $this->request( |
147
|
133 |
|
$method, |
148
|
133 |
|
$this->getParentId($arguments), |
149
|
133 |
|
$this->getChildId($arguments), |
150
|
133 |
|
$this->getChildChildId($arguments), |
151
|
133 |
|
$this->getParameters($arguments) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param array $arguments |
157
|
|
|
* @return float|null |
158
|
|
|
*/ |
159
|
133 |
|
private function getParentId(array $arguments) |
160
|
|
|
{ |
161
|
133 |
|
$parentId = null; |
162
|
|
|
|
163
|
133 |
|
if (!empty($arguments[0])) { |
164
|
127 |
|
if (is_numeric($arguments[0])) { |
165
|
109 |
|
$parentId = $arguments[0]; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
133 |
|
return $parentId; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $arguments |
174
|
|
|
* @return float|null |
175
|
|
|
*/ |
176
|
133 |
|
private function getChildId(array $arguments) |
177
|
|
|
{ |
178
|
133 |
|
$childId = null; |
179
|
|
|
|
180
|
133 |
|
if (!empty($arguments[1])) { |
181
|
75 |
|
if (is_numeric($arguments[1])) { |
182
|
46 |
|
$childId = $arguments[1]; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
133 |
|
return $childId; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $arguments |
191
|
|
|
* @return float|null |
192
|
|
|
*/ |
193
|
133 |
|
private function getChildChildId(array $arguments) |
194
|
|
|
{ |
195
|
133 |
|
$childChildId = null; |
196
|
|
|
|
197
|
133 |
|
if (!empty($arguments[2])) { |
198
|
24 |
|
if (is_numeric($arguments[2])) { |
199
|
8 |
|
$childChildId = $arguments[2]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
133 |
|
return $childChildId; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param array $arguments |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
133 |
|
private function getParameters(array $arguments): array |
211
|
|
|
{ |
212
|
133 |
|
foreach ($arguments as $value) { |
213
|
128 |
|
if (is_array($value)) { |
214
|
128 |
|
return $value; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
83 |
|
return []; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.