Total Complexity | 50 |
Total Lines | 417 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Standard |
||
24 | extends \Aimeos\Client\JsonApi\Basket\Base |
||
25 | implements \Aimeos\Client\JsonApi\Iface |
||
26 | { |
||
27 | /** client/jsonapi/basket/product/name |
||
28 | * Class name of the used basket/product client implementation |
||
29 | * |
||
30 | * Each default JSON API client can be replace by an alternative imlementation. |
||
31 | * To use this implementation, you have to set the last part of the class |
||
32 | * name as configuration value so the client factory knows which class it |
||
33 | * has to instantiate. |
||
34 | * |
||
35 | * For example, if the name of the default class is |
||
36 | * |
||
37 | * \Aimeos\Client\JsonApi\Basket\Product\Standard |
||
38 | * |
||
39 | * and you want to replace it with your own version named |
||
40 | * |
||
41 | * \Aimeos\Client\JsonApi\Basket\Product\Mybasket/product |
||
42 | * |
||
43 | * then you have to set the this configuration option: |
||
44 | * |
||
45 | * client/jsonapi/basket/product/name = Mybasket/product |
||
46 | * |
||
47 | * The value is the last part of your own class name and it's case sensitive, |
||
48 | * so take care that the configuration value is exactly named like the last |
||
49 | * part of the class name. |
||
50 | * |
||
51 | * The allowed characters of the class name are A-Z, a-z and 0-9. No other |
||
52 | * characters are possible! You should always start the last part of the class |
||
53 | * name with an upper case character and continue only with lower case characters |
||
54 | * or numbers. Avoid chamel case names like "MyProduct"! |
||
55 | * |
||
56 | * @param string Last part of the class name |
||
57 | * @since 2017.03 |
||
58 | * @category Developer |
||
59 | */ |
||
60 | |||
61 | /** client/jsonapi/basket/product/decorators/excludes |
||
62 | * Excludes decorators added by the "common" option from the JSON API clients |
||
63 | * |
||
64 | * Decorators extend the functionality of a class by adding new aspects |
||
65 | * (e.g. log what is currently done), executing the methods of the underlying |
||
66 | * class only in certain conditions (e.g. only for logged in users) or |
||
67 | * modify what is returned to the caller. |
||
68 | * |
||
69 | * This option allows you to remove a decorator added via |
||
70 | * "client/jsonapi/common/decorators/default" before they are wrapped |
||
71 | * around the JsonApi client. |
||
72 | * |
||
73 | * client/jsonapi/decorators/excludes = array( 'decorator1' ) |
||
74 | * |
||
75 | * This would remove the decorator named "decorator1" from the list of |
||
76 | * common decorators ("\Aimeos\Client\JsonApi\Common\Decorator\*") added via |
||
77 | * "client/jsonapi/common/decorators/default" for the JSON API client. |
||
78 | * |
||
79 | * @param array List of decorator names |
||
80 | * @since 2017.07 |
||
81 | * @category Developer |
||
82 | * @see client/jsonapi/common/decorators/default |
||
83 | * @see client/jsonapi/basket/product/decorators/global |
||
84 | * @see client/jsonapi/basket/product/decorators/local |
||
85 | */ |
||
86 | |||
87 | /** client/jsonapi/basket/product/decorators/global |
||
88 | * Adds a list of globally available decorators only to the JsonApi client |
||
89 | * |
||
90 | * Decorators extend the functionality of a class by adding new aspects |
||
91 | * (e.g. log what is currently done), executing the methods of the underlying |
||
92 | * class only in certain conditions (e.g. only for logged in users) or |
||
93 | * modify what is returned to the caller. |
||
94 | * |
||
95 | * This option allows you to wrap global decorators |
||
96 | * ("\Aimeos\Client\JsonApi\Common\Decorator\*") around the JsonApi |
||
97 | * client. |
||
98 | * |
||
99 | * client/jsonapi/basket/product/decorators/global = array( 'decorator1' ) |
||
100 | * |
||
101 | * This would add the decorator named "decorator1" defined by |
||
102 | * "\Aimeos\Client\JsonApi\Common\Decorator\Decorator1" only to the |
||
103 | * "basket" JsonApi client. |
||
104 | * |
||
105 | * @param array List of decorator names |
||
106 | * @since 2017.07 |
||
107 | * @category Developer |
||
108 | * @see client/jsonapi/common/decorators/default |
||
109 | * @see client/jsonapi/basket/product/decorators/excludes |
||
110 | * @see client/jsonapi/basket/product/decorators/local |
||
111 | */ |
||
112 | |||
113 | /** client/jsonapi/basket/product/decorators/local |
||
114 | * Adds a list of local decorators only to the JsonApi client |
||
115 | * |
||
116 | * Decorators extend the functionality of a class by adding new aspects |
||
117 | * (e.g. log what is currently done), executing the methods of the underlying |
||
118 | * class only in certain conditions (e.g. only for logged in users) or |
||
119 | * modify what is returned to the caller. |
||
120 | * |
||
121 | * This option allows you to wrap local decorators |
||
122 | * ("\Aimeos\Client\JsonApi\Basket\Product\Decorator\*") around the JsonApi |
||
123 | * client. |
||
124 | * |
||
125 | * client/jsonapi/basket/product/decorators/local = array( 'decorator2' ) |
||
126 | * |
||
127 | * This would add the decorator named "decorator2" defined by |
||
128 | * "\Aimeos\Client\JsonApi\Basket\Product\Decorator\Decorator2" only to the |
||
129 | * "basket product" JsonApi client. |
||
130 | * |
||
131 | * @param array List of decorator names |
||
132 | * @since 2017.07 |
||
133 | * @category Developer |
||
134 | * @see client/jsonapi/common/decorators/default |
||
135 | * @see client/jsonapi/basket/product/decorators/excludes |
||
136 | * @see client/jsonapi/basket/product/decorators/global |
||
137 | */ |
||
138 | |||
139 | |||
140 | private \Aimeos\Controller\Frontend\Basket\Iface $controller; |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Initializes the client |
||
145 | * |
||
146 | * @param \Aimeos\MShop\ContextIface $context MShop context object |
||
147 | */ |
||
148 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
153 | } |
||
154 | |||
155 | |||
156 | /** |
||
157 | * Deletes the resource or the resource list |
||
158 | * |
||
159 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
160 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
161 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
162 | */ |
||
163 | public function delete( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
164 | { |
||
165 | $view = $this->view(); |
||
166 | |||
167 | try |
||
168 | { |
||
169 | $this->clearCache(); |
||
170 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
171 | |||
172 | $relId = $view->param( 'relatedid' ); |
||
173 | $body = (string) $request->getBody(); |
||
174 | |||
175 | if( $relId === '' || $relId === null ) |
||
176 | { |
||
177 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
178 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
179 | } |
||
180 | |||
181 | if( !is_array( $payload->data ) ) { |
||
182 | $payload->data = [$payload->data]; |
||
183 | } |
||
184 | |||
185 | foreach( $payload->data as $entry ) |
||
186 | { |
||
187 | if( !isset( $entry->id ) ) { |
||
188 | throw new \Aimeos\Client\JsonApi\Exception( 'Position (ID) is missing', 400 ); |
||
189 | } |
||
190 | |||
191 | $this->controller->deleteProduct( $entry->id ); |
||
192 | } |
||
193 | } |
||
194 | else |
||
195 | { |
||
196 | $this->controller->deleteProduct( $relId ); |
||
197 | } |
||
198 | |||
199 | |||
200 | $view->item = $this->controller->get(); |
||
201 | $status = 200; |
||
202 | } |
||
203 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
204 | { |
||
205 | $status = 409; |
||
206 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
207 | } |
||
208 | catch( \Aimeos\MShop\Exception $e ) |
||
209 | { |
||
210 | $status = 404; |
||
211 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
212 | } |
||
213 | catch( \Exception $e ) |
||
214 | { |
||
215 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
216 | $view->errors = $this->getErrorDetails( $e ); |
||
217 | } |
||
218 | |||
219 | return $this->render( $response, $view, $status ); |
||
220 | } |
||
221 | |||
222 | |||
223 | /** |
||
224 | * Updates the resource or the resource list partitially |
||
225 | * |
||
226 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
227 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
228 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
229 | */ |
||
230 | public function patch( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
231 | { |
||
232 | $view = $this->view(); |
||
233 | |||
234 | try |
||
235 | { |
||
236 | $this->clearCache(); |
||
237 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
238 | |||
239 | $body = (string) $request->getBody(); |
||
240 | $relId = $view->param( 'relatedid' ); |
||
241 | |||
242 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) || !isset( $payload->data->attributes ) ) { |
||
243 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
244 | } |
||
245 | |||
246 | if( !is_array( $payload->data ) ) { |
||
|
|||
247 | $payload->data = [$payload->data]; |
||
248 | } |
||
249 | |||
250 | foreach( $payload->data as $entry ) |
||
251 | { |
||
252 | if( $relId !== '' && $relId !== null ) { |
||
253 | $entry->id = $relId; |
||
254 | } |
||
255 | |||
256 | if( !isset( $entry->id ) ) { |
||
257 | throw new \Aimeos\Client\JsonApi\Exception( 'Position (ID) is missing', 400 ); |
||
258 | } |
||
259 | |||
260 | $qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
||
261 | $this->controller->updateProduct( $entry->id, $qty ); |
||
262 | } |
||
263 | |||
264 | $view->item = $this->controller->get(); |
||
265 | $status = 200; |
||
266 | } |
||
267 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
268 | { |
||
269 | $status = 409; |
||
270 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
271 | } |
||
272 | catch( \Aimeos\MShop\Exception $e ) |
||
273 | { |
||
274 | $status = 404; |
||
275 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
276 | } |
||
277 | catch( \Exception $e ) |
||
278 | { |
||
279 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
280 | $view->errors = $this->getErrorDetails( $e ); |
||
281 | } |
||
282 | |||
283 | return $this->render( $response, $view, $status ); |
||
284 | } |
||
285 | |||
286 | |||
287 | /** |
||
288 | * Creates or updates the resource or the resource list |
||
289 | * |
||
290 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
291 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
292 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
293 | */ |
||
294 | public function post( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
295 | { |
||
296 | $view = $this->view(); |
||
297 | |||
298 | try |
||
299 | { |
||
300 | $this->clearCache(); |
||
301 | $this->controller->setType( $view->param( 'id', 'default' ) ); |
||
302 | |||
303 | $body = (string) $request->getBody(); |
||
304 | |||
305 | if( ( $payload = json_decode( $body ) ) === null || !isset( $payload->data ) ) { |
||
306 | throw new \Aimeos\Client\JsonApi\Exception( 'Invalid JSON in body', 400 ); |
||
307 | } |
||
308 | |||
309 | if( !is_array( $payload->data ) ) { |
||
310 | $payload->data = [$payload->data]; |
||
311 | } |
||
312 | |||
313 | foreach( $payload->data as $entry ) |
||
314 | { |
||
315 | if( !isset( $entry->attributes ) || !isset( $entry->attributes->{'product.id'} ) ) { |
||
316 | throw new \Aimeos\Client\JsonApi\Exception( 'Product ID is missing', 400 ); |
||
317 | } |
||
318 | } |
||
319 | |||
320 | $cntl = \Aimeos\Controller\Frontend::create( $this->context(), 'product' ) |
||
321 | ->uses( ['attribute', 'catalog', 'locale/site', 'media', 'price', 'product', 'text'] ); |
||
322 | |||
323 | foreach( $payload->data as $entry ) |
||
324 | { |
||
325 | $item = $cntl->get( $entry->attributes->{'product.id'} ); |
||
326 | |||
327 | $qty = ( isset( $entry->attributes->quantity ) ? $entry->attributes->quantity : 1 ); |
||
328 | $stock = ( isset( $entry->attributes->stocktype ) ? $entry->attributes->stocktype : 'default' ); |
||
329 | $varIds = ( isset( $entry->attributes->variant ) ? (array) $entry->attributes->variant : [] ); |
||
330 | $confIds = ( isset( $entry->attributes->config ) ? get_object_vars( $entry->attributes->config ) : [] ); |
||
331 | $custIds = ( isset( $entry->attributes->custom ) ? get_object_vars( $entry->attributes->custom ) : [] ); |
||
332 | $siteId = ( isset( $entry->attributes->siteid ) ? $entry->attributes->siteid : null ); |
||
333 | |||
334 | $this->controller->addProduct( $item, $qty, $varIds, $confIds, $custIds, $stock, $siteId ); |
||
335 | } |
||
336 | |||
337 | |||
338 | $view->item = $this->controller->get(); |
||
339 | $status = 201; |
||
340 | } |
||
341 | catch( \Aimeos\MShop\Plugin\Provider\Exception $e ) |
||
342 | { |
||
343 | $status = 409; |
||
344 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
345 | } |
||
346 | catch( \Aimeos\MShop\Exception $e ) |
||
347 | { |
||
348 | $status = 404; |
||
349 | $view->errors = $this->getErrorDetails( $e, 'mshop' ); |
||
350 | } |
||
351 | catch( \Exception $e ) |
||
352 | { |
||
353 | $status = $e->getCode() >= 100 && $e->getCode() < 600 ? $e->getCode() : 500; |
||
354 | $view->errors = $this->getErrorDetails( $e ); |
||
355 | } |
||
356 | |||
357 | return $this->render( $response, $view, $status ); |
||
358 | } |
||
359 | |||
360 | |||
361 | /** |
||
362 | * Returns the available REST verbs and the available parameters |
||
363 | * |
||
364 | * @param \Psr\Http\Message\ServerRequestInterface $request Request object |
||
365 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
366 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
367 | */ |
||
368 | public function options( ServerRequestInterface $request, ResponseInterface $response ) : \Psr\Http\Message\ResponseInterface |
||
369 | { |
||
370 | $view = $this->view(); |
||
371 | |||
372 | $view->attributes = [ |
||
373 | 'product.id' => [ |
||
374 | 'label' => 'Product ID from article, bundle or selection product (POST only)', |
||
375 | 'type' => 'string', 'default' => '', 'required' => true, |
||
376 | ], |
||
377 | 'quantity' => [ |
||
378 | 'label' => 'Number of product items (POST only)', |
||
379 | 'type' => 'string', 'default' => '1', 'required' => false, |
||
380 | ], |
||
381 | 'stocktype' => [ |
||
382 | 'label' => 'Code of the warehouse/location type (POST only)', |
||
383 | 'type' => 'string', 'default' => 'default', 'required' => false, |
||
384 | ], |
||
385 | 'variant' => [ |
||
386 | 'label' => 'List of attribute IDs of the selected variant attributes (POST only)', |
||
387 | 'type' => 'array', 'default' => '[]', 'required' => false, |
||
388 | ], |
||
389 | 'config' => [ |
||
390 | 'label' => 'List of attribute IDs of the selected config attributes (POST only)', |
||
391 | 'type' => 'array', 'default' => '[]', 'required' => false, |
||
392 | ], |
||
393 | 'hidden' => [ |
||
394 | 'label' => 'List of attribute IDs of the hidden product attributes that will be added but should be invisible (POST only)', |
||
395 | 'type' => 'array', 'default' => '[]', 'required' => false, |
||
396 | ], |
||
397 | 'custom' => [ |
||
398 | 'label' => 'List of values entered by the user for the custom attributes with the attribute IDs as keys (POST only)', |
||
399 | 'type' => 'array[<attrid>]', 'default' => '[]', 'required' => false, |
||
400 | ], |
||
401 | 'codes' => [ |
||
402 | 'label' => 'List of product options (added via "config") that should be removed (PATCH only)', |
||
403 | 'type' => 'array', '' => '[]', 'required' => false, |
||
404 | ], |
||
405 | ]; |
||
406 | |||
407 | $tplconf = 'client/jsonapi/template-options'; |
||
408 | $default = 'options-standard'; |
||
409 | |||
410 | $body = $view->render( $view->config( $tplconf, $default ) ); |
||
411 | |||
412 | return $response->withHeader( 'Allow', 'DELETE,GET,OPTIONS,PATCH,POST' ) |
||
413 | ->withHeader( 'Cache-Control', 'max-age=300' ) |
||
414 | ->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
||
415 | ->withBody( $view->response()->createStreamFromString( $body ) ) |
||
416 | ->withStatus( 200 ); |
||
417 | } |
||
418 | |||
419 | |||
420 | /** |
||
421 | * Returns the response object with the rendered header and body |
||
422 | * |
||
423 | * @param \Psr\Http\Message\ResponseInterface $response Response object |
||
424 | * @param \Aimeos\Base\View\Iface $view View instance |
||
425 | * @param int $status HTTP status code |
||
426 | * @return \Psr\Http\Message\ResponseInterface Modified response object |
||
427 | */ |
||
428 | protected function render( ResponseInterface $response, \Aimeos\Base\View\Iface $view, int $status ) : \Psr\Http\Message\ResponseInterface |
||
440 | } |
||
441 | } |
||
442 |