1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeadCommerce\Shopware\SDK\Query; |
4
|
|
|
|
5
|
|
|
use LeadCommerce\Shopware\SDK\Exception\MethodNotAllowedException; |
6
|
|
|
use LeadCommerce\Shopware\SDK\ShopwareClient; |
7
|
|
|
use LeadCommerce\Shopware\SDK\Util\Constants; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Base |
12
|
|
|
* |
13
|
|
|
* @author Alexander Mahrt <[email protected]> |
14
|
|
|
* @copyright 2016 LeadCommerce <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class Base |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ShopwareClient |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $queryPath; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $methodsAllowed = [ |
32
|
|
|
Constants::METHOD_CREATE, |
33
|
|
|
Constants::METHOD_GET, |
34
|
|
|
Constants::METHOD_GET_BATCH, |
35
|
|
|
Constants::METHOD_UPDATE, |
36
|
|
|
Constants::METHOD_UPDATE_BATCH, |
37
|
|
|
Constants::METHOD_DELETE, |
38
|
|
|
Constants::METHOD_DELETE_BATCH, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Base constructor. |
43
|
|
|
* |
44
|
|
|
* @param $client |
45
|
|
|
*/ |
46
|
8 |
|
public function __construct($client) |
47
|
|
|
{ |
48
|
8 |
|
$this->client = $client; |
49
|
8 |
|
$this->queryPath = $this->getQueryPath(); |
50
|
8 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the query path to look for entities. |
54
|
|
|
* E.G: 'variants' or 'articles' |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
abstract protected function getQueryPath(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Finds all entities. |
62
|
|
|
* |
63
|
|
|
* @return \LeadCommerce\Shopware\SDK\Entity\Base[] |
64
|
|
|
*/ |
65
|
1 |
|
public function findAll() |
66
|
|
|
{ |
67
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_GET_BATCH); |
68
|
|
|
|
69
|
1 |
|
return $this->fetch($this->queryPath); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validates if the requested method is allowed. |
74
|
|
|
* |
75
|
|
|
* @param $method |
76
|
|
|
* |
77
|
|
|
* @throws MethodNotAllowedException |
78
|
|
|
*/ |
79
|
7 |
|
private function validateMethodAllowed($method) |
80
|
|
|
{ |
81
|
7 |
|
if (!in_array($method, $this->methodsAllowed)) { |
82
|
|
|
throw new MethodNotAllowedException('Method ' . $method . ' is not allowed for ' . get_class($this)); |
83
|
|
|
} |
84
|
7 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fetch and build entity. |
88
|
|
|
* |
89
|
|
|
* @param $uri |
90
|
|
|
* @param string $method |
91
|
|
|
* @param null $body |
92
|
|
|
* @param array $headers |
93
|
|
|
* |
94
|
|
|
* @return array|mixed |
95
|
|
|
*/ |
96
|
7 |
|
protected function fetch($uri, $method = 'GET', $body = null, $headers = []) |
97
|
|
|
{ |
98
|
7 |
|
$response = $this->client->request($uri, $method, $body, $headers); |
99
|
|
|
|
100
|
7 |
|
return $this->createEntityFromResponse($response); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Creates an entity |
105
|
|
|
* |
106
|
|
|
* @param ResponseInterface $response |
107
|
|
|
* |
108
|
|
|
* @return array|mixed |
109
|
|
|
*/ |
110
|
7 |
|
protected function createEntityFromResponse(ResponseInterface $response) |
111
|
|
|
{ |
112
|
7 |
|
$content = $response->getBody()->getContents(); |
113
|
7 |
|
$content = json_decode($content); |
114
|
7 |
|
$content = $content->data; |
115
|
|
|
|
116
|
7 |
|
if (is_array($content)) { |
117
|
4 |
|
return array_map(function ($item) { |
118
|
2 |
|
return $this->createEntity($item); |
119
|
4 |
|
}, $content); |
120
|
|
|
} else { |
121
|
3 |
|
return $this->createEntity($content); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Creates an entity based on the getClass method. |
127
|
|
|
* |
128
|
|
|
* @param $content |
129
|
|
|
* |
130
|
|
|
* @return \LeadCommerce\Shopware\SDK\Entity\Base |
131
|
|
|
*/ |
132
|
5 |
|
protected function createEntity($content) |
133
|
|
|
{ |
134
|
5 |
|
$class = $this->getClass(); |
135
|
5 |
|
$entity = new $class(); |
136
|
|
|
|
137
|
5 |
|
if ($entity instanceof \LeadCommerce\Shopware\SDK\Entity\Base) { |
138
|
5 |
|
$content = json_decode(json_encode($content), true); |
139
|
5 |
|
$entity->setEntityAttributes($content); |
140
|
5 |
|
} |
141
|
|
|
|
142
|
5 |
|
return $entity; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets the class for the entities. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
abstract protected function getClass(); |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Finds an entity by its id. |
154
|
|
|
* |
155
|
|
|
* @param $id |
156
|
|
|
* |
157
|
|
|
* @return \LeadCommerce\Shopware\SDK\Entity\Base |
158
|
|
|
*/ |
159
|
1 |
|
public function findOne($id) |
160
|
|
|
{ |
161
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_GET); |
162
|
|
|
|
163
|
1 |
|
return $this->fetch($this->queryPath . '/' . $id); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Creates an entity. |
168
|
|
|
* |
169
|
|
|
* @param \LeadCommerce\Shopware\SDK\Entity\Base $entity |
170
|
|
|
* |
171
|
|
|
* @throws MethodNotAllowedException |
172
|
|
|
* |
173
|
|
|
* @return \LeadCommerce\Shopware\SDK\Entity\Base |
174
|
|
|
*/ |
175
|
1 |
|
public function create(\LeadCommerce\Shopware\SDK\Entity\Base $entity) |
176
|
|
|
{ |
177
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_CREATE); |
178
|
|
|
|
179
|
1 |
|
return $this->fetch($this->queryPath, 'POST', $entity->getArrayCopy()); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Updates an entity. |
184
|
|
|
* |
185
|
|
|
* @param \LeadCommerce\Shopware\SDK\Entity\Base $entity |
186
|
|
|
* |
187
|
|
|
* @throws MethodNotAllowedException |
188
|
|
|
* |
189
|
|
|
* @return array|mixed |
190
|
|
|
*/ |
191
|
1 |
|
public function update(\LeadCommerce\Shopware\SDK\Entity\Base $entity) |
192
|
|
|
{ |
193
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_UPDATE); |
194
|
|
|
|
195
|
1 |
|
return $this->fetch($this->queryPath . '/' . $entity->getId(), 'PUT', $entity->getArrayCopy()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Updates a batch of this entity. |
200
|
|
|
* |
201
|
|
|
* @param \LeadCommerce\Shopware\SDK\Entity\Base[] $entities |
202
|
|
|
* |
203
|
|
|
* @return \LeadCommerce\Shopware\SDK\Entity\Base[] |
204
|
|
|
*/ |
205
|
1 |
|
public function updateBatch($entities) |
206
|
|
|
{ |
207
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_UPDATE_BATCH); |
208
|
1 |
|
$body = []; |
209
|
1 |
|
foreach ($entities as $entity) { |
210
|
1 |
|
$body[] = $entity->getArrayCopy(); |
211
|
1 |
|
} |
212
|
|
|
|
213
|
1 |
|
return $this->fetch($this->queryPath . '/', 'PUT', $body); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Deletes an entity by its id.. |
218
|
|
|
* |
219
|
|
|
* @param $id |
220
|
|
|
* |
221
|
|
|
* @throws MethodNotAllowedException |
222
|
|
|
* |
223
|
|
|
* @return array|mixed |
224
|
|
|
*/ |
225
|
1 |
|
public function delete($id) |
226
|
|
|
{ |
227
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_DELETE); |
228
|
|
|
|
229
|
1 |
|
return $this->fetch($this->queryPath . '/' . $id, 'DELETE'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Deletes a batch of this entity given by ids. |
234
|
|
|
* |
235
|
|
|
* @param array $ids |
236
|
|
|
* |
237
|
|
|
* @throws MethodNotAllowedException |
238
|
|
|
* |
239
|
|
|
* @return array|mixed |
240
|
|
|
*/ |
241
|
1 |
|
public function deleteBatch(array $ids) |
242
|
|
|
{ |
243
|
1 |
|
$this->validateMethodAllowed(Constants::METHOD_DELETE_BATCH); |
244
|
|
|
|
245
|
1 |
|
return $this->fetch($this->queryPath . '/', 'DELETE', $ids); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param $uri |
250
|
|
|
* @param string $method |
251
|
|
|
* @param null $body |
252
|
|
|
* @param array $headers |
253
|
|
|
* |
254
|
|
|
* @return mixed|ResponseInterface |
255
|
|
|
*/ |
256
|
|
|
protected function fetchSimple($uri, $method = 'GET', $body = null, $headers = []) |
257
|
|
|
{ |
258
|
|
|
return $this->client->request($uri, $method, $body, $headers); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Fetch as json object. |
263
|
|
|
* |
264
|
|
|
* @param $uri |
265
|
|
|
* @param string $method |
266
|
|
|
* @param null $body |
267
|
|
|
* @param array $headers |
268
|
|
|
* |
269
|
|
|
* @return false|\stdClass |
270
|
|
|
*/ |
271
|
|
|
protected function fetchJson($uri, $method = 'GET', $body = null, $headers = []) |
272
|
|
|
{ |
273
|
|
|
$response = $this->client->request($uri, $method, $body, $headers); |
274
|
|
|
$response = json_decode($response->getBody()->getContents()); |
275
|
|
|
|
276
|
|
|
return $response ? $response : null; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|