1 | <?php |
||
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
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 = []) |
|
321 | |||
322 | /** |
||
323 | * @return array |
||
324 | */ |
||
325 | public function getData() |
||
329 | |||
330 | /** |
||
331 | * @return int |
||
332 | */ |
||
333 | public function getTotal() |
||
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 = []) |
||
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 = []) |
||
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.