1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neta\Shopware\SDK\Query; |
4
|
|
|
|
5
|
|
|
use Neta\Shopware\SDK\ShopwareClient; |
6
|
|
|
use Neta\Shopware\SDK\Util\ArrayUtil; |
7
|
|
|
use Neta\Shopware\SDK\Util\Constants; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Neta\Shopware\SDK\Exception\MethodNotAllowedException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Base. |
13
|
|
|
* |
14
|
|
|
* @author Alexander Mahrt <[email protected]> |
15
|
|
|
* @copyright 2016 LeadCommerce <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class Base |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ShopwareClient |
21
|
|
|
*/ |
22
|
|
|
protected $client; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $queryPath; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $methodsAllowed = [ |
33
|
|
|
Constants::METHOD_CREATE, |
34
|
|
|
Constants::METHOD_GET, |
35
|
|
|
Constants::METHOD_GET_BATCH, |
36
|
|
|
Constants::METHOD_UPDATE, |
37
|
|
|
Constants::METHOD_UPDATE_BATCH, |
38
|
|
|
Constants::METHOD_DELETE, |
39
|
|
|
Constants::METHOD_DELETE_BATCH, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $data; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $total; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Base constructor. |
54
|
|
|
* |
55
|
|
|
* @param $client |
56
|
|
|
*/ |
57
|
14 |
|
public function __construct($client) |
58
|
|
|
{ |
59
|
14 |
|
$this->client = $client; |
60
|
14 |
|
$this->queryPath = $this->getQueryPath(); |
61
|
14 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the query path to look for entities. |
65
|
|
|
* E.G: 'variants' or 'articles'. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
abstract protected function getQueryPath(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Finds all entities. |
73
|
|
|
* |
74
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base[] |
75
|
|
|
*/ |
76
|
1 |
|
public function findAll() |
77
|
|
|
{ |
78
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_GET_BATCH); |
79
|
|
|
|
80
|
1 |
|
return $this->fetch($this->queryPath); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Finds entities by params. |
85
|
|
|
* |
86
|
|
|
* e.g.: |
87
|
|
|
* |
88
|
|
|
* $params = [ |
89
|
|
|
* 'limit' => 10, |
90
|
|
|
* 'start' => 20, |
91
|
|
|
* 'sort' => [ |
92
|
|
|
* [ |
93
|
|
|
* 'property' => 'name', |
94
|
|
|
* 'direction' => 'ASC' |
95
|
|
|
* ] |
96
|
|
|
* ], |
97
|
|
|
* 'filter' => [ |
98
|
|
|
* [ |
99
|
|
|
* 'property' => 'name', |
100
|
|
|
* 'expression' => 'LIKE', |
101
|
|
|
* 'value' => '%foo' |
102
|
|
|
* ], |
103
|
|
|
* [ |
104
|
|
|
* 'operator' => 'AND', |
105
|
|
|
* 'property' => 'number', |
106
|
|
|
* 'expression' => '>', |
107
|
|
|
* 'value' => '500' |
108
|
|
|
* ] |
109
|
|
|
* ] |
110
|
|
|
* ] |
111
|
|
|
* |
112
|
|
|
* @param array $params |
113
|
|
|
* |
114
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base[] |
115
|
|
|
*/ |
116
|
1 |
|
public function findByParams($params) |
117
|
|
|
{ |
118
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_GET_BATCH); |
119
|
|
|
|
120
|
1 |
|
return $this->fetch($this->queryPath, 'GET', null, [], $params); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Validates if the requested method is allowed. |
125
|
|
|
* |
126
|
|
|
* @param $method |
127
|
|
|
* |
128
|
|
|
* @throws MethodNotAllowedException |
129
|
|
|
*/ |
130
|
13 |
|
private function validateMethodAllowed($method) |
131
|
|
|
{ |
132
|
13 |
|
if (! in_array($method, $this->methodsAllowed)) { |
133
|
|
|
throw new MethodNotAllowedException('Method ' . $method . ' is not allowed for ' . get_class($this)); |
134
|
|
|
} |
135
|
13 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Fetch and build entity. |
139
|
|
|
* |
140
|
|
|
* @param $uri |
141
|
|
|
* @param string $method |
142
|
|
|
* @param null $body |
143
|
|
|
* @param array $headers |
144
|
|
|
* @param array $uriParams |
145
|
|
|
* |
146
|
|
|
* @return array|mixed |
147
|
|
|
*/ |
148
|
13 |
|
protected function fetch($uri, $method = 'GET', $body = null, $headers = [], $uriParams = []) |
149
|
|
|
{ |
150
|
13 |
|
if (is_array($uriParams) && count($uriParams) > 0) { |
151
|
1 |
|
if (strpos($uri, '?') !== false) { |
152
|
|
|
parse_str(substr($uri, strpos($uri, '?') + 1), $existingUriParams); |
153
|
|
|
ArrayUtil::mergeRecursiveWithOverrule($existingUriParams, $uriParams); |
|
|
|
|
154
|
|
|
$uri = substr($uri, 0, strpos($uri, '?')) . '?' . http_build_query($existingUriParams); |
155
|
|
|
} else { |
156
|
1 |
|
$uri = $uri . '?' . http_build_query($uriParams); |
157
|
|
|
} |
158
|
|
|
} |
159
|
13 |
|
$response = $this->client->request($uri, $method, $body, $headers); |
160
|
|
|
|
161
|
13 |
|
return $this->createEntityFromResponse($response); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Creates an entity. |
166
|
|
|
* |
167
|
|
|
* @param ResponseInterface $response |
168
|
|
|
* |
169
|
|
|
* @return array|\Neta\Shopware\SDK\Entity\Base |
170
|
|
|
*/ |
171
|
13 |
|
protected function createEntityFromResponse(ResponseInterface $response) |
172
|
|
|
{ |
173
|
13 |
|
$result = []; |
174
|
13 |
|
$content = $response->getBody()->getContents(); |
175
|
13 |
|
$content = json_decode($content); |
176
|
13 |
|
$this->data = isset($content->data) ? $content->data : null; |
177
|
13 |
|
$this->total = isset($content->total) ? $content->total : null; |
178
|
|
|
|
179
|
13 |
|
if (is_array($this->data)) { |
180
|
|
|
$result = array_map(function ($item) { |
181
|
4 |
|
if (isset($item->id)) { |
182
|
3 |
|
return $this->createEntity($item); |
183
|
|
|
} |
184
|
1 |
|
if (isset($item->data) && isset($item->data->id)) { |
185
|
1 |
|
return $this->createEntity($item->data); |
186
|
|
|
} |
187
|
4 |
|
}, $this->data); |
188
|
|
|
} |
189
|
|
|
|
190
|
13 |
|
if (is_object($this->data)) { |
191
|
8 |
|
$result = $this->createEntity($this->data); |
192
|
|
|
} |
193
|
|
|
|
194
|
13 |
|
return $result; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Creates an entity based on the getClass method. |
199
|
|
|
* |
200
|
|
|
* @param $content |
201
|
|
|
* |
202
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base |
203
|
|
|
*/ |
204
|
12 |
|
protected function createEntity($content) |
205
|
|
|
{ |
206
|
12 |
|
$class = $this->getClass(); |
207
|
12 |
|
$entity = new $class(); |
208
|
|
|
|
209
|
12 |
|
if ($entity instanceof \Neta\Shopware\SDK\Entity\Base) { |
210
|
12 |
|
$content = json_decode(json_encode($content), true); |
211
|
12 |
|
$entity->setEntityAttributes($content); |
212
|
|
|
} |
213
|
|
|
|
214
|
12 |
|
return $entity; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Gets the class for the entities. |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
abstract protected function getClass(); |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Finds an entity by its id. |
226
|
|
|
* |
227
|
|
|
* @param mixed $id |
228
|
|
|
* @param array $params |
229
|
|
|
* |
230
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base |
231
|
|
|
*/ |
232
|
6 |
|
public function findOne($id, $params = []) |
233
|
|
|
{ |
234
|
6 |
|
$this->validateMethodAllowed(Constants::METHOD_GET); |
235
|
|
|
|
236
|
6 |
|
return $this->fetch($this->queryPath . '/' . $id, 'GET', null, [], $params); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Creates an entity. |
241
|
|
|
* |
242
|
|
|
* @param \Neta\Shopware\SDK\Entity\Base $entity |
243
|
|
|
* @param array $params |
244
|
|
|
* |
245
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base |
246
|
|
|
* @throws MethodNotAllowedException |
247
|
|
|
*/ |
248
|
1 |
|
public function create(\Neta\Shopware\SDK\Entity\Base $entity, $params = []) |
249
|
|
|
{ |
250
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_CREATE); |
251
|
|
|
|
252
|
1 |
|
return $this->fetch($this->queryPath, 'POST', $entity->getArrayCopy(), [], $params); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Updates an entity. |
257
|
|
|
* |
258
|
|
|
* @param \Neta\Shopware\SDK\Entity\Base $entity |
259
|
|
|
* @param array $params |
260
|
|
|
* |
261
|
|
|
* @return array|mixed |
262
|
|
|
* @throws MethodNotAllowedException |
263
|
|
|
*/ |
264
|
1 |
|
public function update(\Neta\Shopware\SDK\Entity\Base $entity, $params = []) |
265
|
|
|
{ |
266
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_UPDATE); |
267
|
|
|
|
268
|
1 |
|
return $this->fetch($this->queryPath . '/' . $entity->getId(), 'PUT', $entity->getArrayCopy(), [], $params); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Updates a batch of this entity. |
273
|
|
|
* |
274
|
|
|
* @param \Neta\Shopware\SDK\Entity\Base[] $entities |
275
|
|
|
* @param array $params |
276
|
|
|
* |
277
|
|
|
* @return \Neta\Shopware\SDK\Entity\Base[] |
278
|
|
|
*/ |
279
|
1 |
|
public function updateBatch($entities, $params = []) |
280
|
|
|
{ |
281
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_UPDATE_BATCH); |
282
|
1 |
|
$body = []; |
283
|
1 |
|
foreach ($entities as $entity) { |
284
|
1 |
|
$body[] = $entity->getArrayCopy(); |
285
|
|
|
} |
286
|
|
|
|
287
|
1 |
|
return $this->fetch($this->queryPath . '/', 'PUT', $body, [], $params); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Deletes an entity by its id.. |
292
|
|
|
* |
293
|
|
|
* @param mixed $id |
294
|
|
|
* @param array $params |
295
|
|
|
* |
296
|
|
|
* @return array|mixed |
297
|
|
|
* @throws MethodNotAllowedException |
298
|
|
|
*/ |
299
|
1 |
|
public function delete($id, $params = []) |
|
|
|
|
300
|
|
|
{ |
301
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_DELETE); |
302
|
|
|
|
303
|
1 |
|
return $this->fetch($this->queryPath . '/' . $id, 'DELETE'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Deletes a batch of this entity given by ids. |
308
|
|
|
* |
309
|
|
|
* @param array $ids |
310
|
|
|
* @param array $params |
311
|
|
|
* |
312
|
|
|
* @return array|mixed |
313
|
|
|
* @throws MethodNotAllowedException |
314
|
|
|
*/ |
315
|
1 |
|
public function deleteBatch(array $ids, $params = []) |
316
|
|
|
{ |
317
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_DELETE_BATCH); |
318
|
|
|
|
319
|
1 |
|
return $this->fetch($this->queryPath . '/', 'DELETE', $ids, [], $params); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return array |
324
|
|
|
*/ |
325
|
|
|
public function getData() |
326
|
|
|
{ |
327
|
|
|
return $this->data; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return int |
332
|
|
|
*/ |
333
|
|
|
public function getTotal() |
334
|
|
|
{ |
335
|
|
|
return $this->total; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param $uri |
340
|
|
|
* @param string $method |
341
|
|
|
* @param null $body |
342
|
|
|
* @param array $headers |
343
|
|
|
* |
344
|
|
|
* @return mixed|ResponseInterface |
345
|
|
|
*/ |
346
|
|
|
protected function fetchSimple($uri, $method = 'GET', $body = null, $headers = []) |
347
|
|
|
{ |
348
|
|
|
return $this->client->request($uri, $method, $body, $headers); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Fetch as json object. |
353
|
|
|
* |
354
|
|
|
* @param $uri |
355
|
|
|
* @param string $method |
356
|
|
|
* @param null $body |
357
|
|
|
* @param array $headers |
358
|
|
|
* |
359
|
|
|
* @return false|\stdClass |
360
|
|
|
*/ |
361
|
|
|
protected function fetchJson($uri, $method = 'GET', $body = null, $headers = []) |
362
|
|
|
{ |
363
|
|
|
$response = $this->client->request($uri, $method, $body, $headers); |
364
|
|
|
$response = json_decode($response->getBody()->getContents()); |
365
|
|
|
|
366
|
|
|
return $response ? $response : null; |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.