Total Complexity | 58 |
Total Lines | 468 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Base 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 Base, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | abstract class Base extends \Aimeos\Controller\Frontend\Base implements Iface |
||
22 | { |
||
23 | /** |
||
24 | * Calculates and returns the current price for the given order product and product prices. |
||
25 | * |
||
26 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $orderProduct Ordered product item |
||
27 | * @param \Aimeos\Map $prices List of price items implementing \Aimeos\MShop\Price\Item\Iface |
||
28 | * @param float $quantity New product quantity |
||
29 | * @return \Aimeos\MShop\Price\Item\Iface Price item with calculated price |
||
30 | */ |
||
31 | protected function calcPrice( \Aimeos\MShop\Order\Item\Base\Product\Iface $orderProduct, |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Returns the allowed quantity for the given product |
||
75 | * |
||
76 | * @param \Aimeos\MShop\Product\Item\Iface $product Product item including referenced items |
||
77 | * @param float $quantity New product quantity |
||
78 | * @return float Updated quantity value |
||
79 | */ |
||
80 | protected function checkQuantity( \Aimeos\MShop\Product\Item\Iface $product, float $quantity ) : float |
||
81 | { |
||
82 | $scale = $product->getScale(); |
||
83 | |||
84 | if( fmod( $quantity, $scale ) >= 0.0005 ) { |
||
85 | return round( ceil( $quantity / $scale ) * $scale, 4 ); |
||
86 | } |
||
87 | |||
88 | return $quantity; |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Checks if the attribute IDs are really associated to the product |
||
94 | * |
||
95 | * @param \Aimeos\MShop\Product\Item\Iface $product Product item with referenced items |
||
96 | * @param string $domain Domain the references must be of |
||
97 | * @param array $refMap Associative list of list type codes as keys and lists of reference IDs as values |
||
98 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If one or more of the IDs are not associated |
||
99 | */ |
||
100 | protected function checkAttributes( array $products, string $listType, array $refIds ) |
||
114 | } |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * Checks for a locale mismatch and migrates the products to the new basket if necessary. |
||
120 | * |
||
121 | * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale object from current basket |
||
122 | * @param string $type Basket type |
||
123 | */ |
||
124 | protected function checkLocale( \Aimeos\MShop\Locale\Item\Iface $locale, string $type ) |
||
163 | } |
||
164 | |||
165 | |||
166 | /** |
||
167 | * Migrates the addresses from the old basket to the current one. |
||
168 | * |
||
169 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
170 | * @param array $errors Associative list of previous errors |
||
171 | * @param string $localeKey Unique identifier of the site, language and currency |
||
172 | * @return array Associative list of errors occured |
||
173 | */ |
||
174 | protected function copyAddresses( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, string $localeKey ) : array |
||
175 | { |
||
176 | foreach( $basket->getAddresses() as $type => $items ) |
||
177 | { |
||
178 | foreach( $items as $pos => $item ) |
||
179 | { |
||
180 | try |
||
181 | { |
||
182 | $this->object()->get()->addAddress( $item, $type, $pos ); |
||
183 | } |
||
184 | catch( \Exception $e ) |
||
185 | { |
||
186 | $logger = $this->context()->logger(); |
||
187 | $errors['address'][$type] = $e->getMessage(); |
||
188 | |||
189 | $str = 'Error migrating address with type "%1$s" in basket to locale "%2$s": %3$s'; |
||
190 | $logger->info( sprintf( $str, $type, $localeKey, $e->getMessage() ), 'controller/frontend' ); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $basket->deleteAddress( $type ); |
||
195 | } |
||
196 | |||
197 | return $errors; |
||
198 | } |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Migrates the coupons from the old basket to the current one. |
||
203 | * |
||
204 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
205 | * @param array $errors Associative list of previous errors |
||
206 | * @param string $localeKey Unique identifier of the site, language and currency |
||
207 | * @return array Associative list of errors occured |
||
208 | */ |
||
209 | protected function copyCoupons( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, string $localeKey ) : array |
||
210 | { |
||
211 | foreach( $basket->getCoupons() as $code => $list ) |
||
212 | { |
||
213 | try |
||
214 | { |
||
215 | $this->object()->addCoupon( $code ); |
||
216 | $basket->deleteCoupon( $code, true ); |
||
217 | } |
||
218 | catch( \Exception $e ) |
||
219 | { |
||
220 | $logger = $this->context()->logger(); |
||
221 | $errors['coupon'][$code] = $e->getMessage(); |
||
222 | |||
223 | $str = 'Error migrating coupon with code "%1$s" in basket to locale "%2$s": %3$s'; |
||
224 | $logger->info( sprintf( $str, $code, $localeKey, $e->getMessage() ), 'controller/frontend' ); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | return $errors; |
||
229 | } |
||
230 | |||
231 | |||
232 | /** |
||
233 | * Migrates the products from the old basket to the current one. |
||
234 | * |
||
235 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
236 | * @param array $errors Associative list of previous errors |
||
237 | * @param string $localeKey Unique identifier of the site, language and currency |
||
238 | * @return array Associative list of errors occured |
||
239 | */ |
||
240 | protected function copyProducts( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, string $localeKey ) : array |
||
287 | } |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Migrates the services from the old basket to the current one. |
||
292 | * |
||
293 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
294 | * @param array $errors Associative list of previous errors |
||
295 | * @return array Associative list of errors occured |
||
296 | */ |
||
297 | protected function copyServices( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors ) : array |
||
330 | } |
||
331 | |||
332 | |||
333 | /** |
||
334 | * Creates the subscription entries for the ordered products with interval attributes |
||
335 | * |
||
336 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
337 | */ |
||
338 | protected function createSubscriptions( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
339 | { |
||
340 | $types = ['config', 'custom', 'hidden', 'variant']; |
||
341 | $manager = \Aimeos\MShop::create( $this->context(), 'subscription' ); |
||
342 | |||
343 | foreach( $basket->getProducts() as $orderProduct ) |
||
344 | { |
||
345 | if( ( $interval = $orderProduct->getAttribute( 'interval', $types ) ) !== null ) |
||
346 | { |
||
347 | $interval = is_array( $interval ) ? reset( $interval ) : $interval; |
||
348 | |||
349 | $item = $manager->create()->setInterval( $interval ) |
||
350 | ->setProductId( $orderProduct->getProductId() ) |
||
351 | ->setOrderProductId( $orderProduct->getId() ) |
||
352 | ->setOrderBaseId( $basket->getId() ); |
||
353 | |||
354 | if( ( $end = $orderProduct->getAttribute( 'intervalend', $types ) ) !== null ) { |
||
355 | $item = $item->setDateEnd( $end ); |
||
356 | } |
||
357 | |||
358 | $manager->save( $item, false ); |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | |||
363 | |||
364 | /** |
||
365 | * Returns the attribute items for the given attribute IDs. |
||
366 | * |
||
367 | * @param array $attributeIds List of attribute IDs |
||
368 | * @param string[] $domains Names of the domain items that should be fetched too |
||
369 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
370 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
371 | */ |
||
372 | protected function getAttributes( array $attributeIds, array $domains = ['text'] ) : \Aimeos\Map |
||
373 | { |
||
374 | if( empty( $attributeIds ) ) { |
||
375 | return map(); |
||
376 | } |
||
377 | |||
378 | $attributeManager = \Aimeos\MShop::create( $this->context(), 'attribute' ); |
||
379 | |||
380 | $search = $attributeManager->filter( true ) |
||
381 | ->add( ['attribute.id' => $attributeIds] ) |
||
382 | ->slice( 0, count( $attributeIds ) ); |
||
383 | |||
384 | $attrItems = $attributeManager->search( $search, $domains ); |
||
385 | |||
386 | if( $attrItems->count() !== count( $attributeIds ) ) |
||
387 | { |
||
388 | $i18n = $this->context()->i18n(); |
||
389 | $expected = implode( ',', $attributeIds ); |
||
390 | $actual = $attrItems->keys()->join( ',' ); |
||
391 | $msg = $i18n->dt( 'controller/frontend', 'Available attribute IDs "%1$s" do not match the given attribute IDs "%2$s"' ); |
||
392 | |||
393 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $actual, $expected ) ); |
||
394 | } |
||
395 | |||
396 | return $attrItems; |
||
397 | } |
||
398 | |||
399 | |||
400 | /** |
||
401 | * Returns the attribute items using the given order attribute items. |
||
402 | * |
||
403 | * @param \Aimeos\Map $orderAttributes List of items implementing \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface |
||
404 | * @return \Aimeos\Map List of attribute IDs as key and attribute items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
405 | */ |
||
406 | protected function getAttributeItems( \Aimeos\Map $orderAttributes ) : \Aimeos\Map |
||
407 | { |
||
408 | if( $orderAttributes->isEmpty() ) { |
||
409 | return map(); |
||
410 | } |
||
411 | |||
412 | $attributeManager = \Aimeos\MShop::create( $this->context(), 'attribute' ); |
||
413 | $search = $attributeManager->filter( true ); |
||
414 | $expr = []; |
||
415 | |||
416 | foreach( $orderAttributes as $item ) |
||
417 | { |
||
418 | if( is_scalar( $item->getValue() ) ) |
||
419 | { |
||
420 | $tmp = array( |
||
421 | $search->compare( '==', 'attribute.domain', 'product' ), |
||
422 | $search->compare( '==', 'attribute.code', $item->getValue() ), |
||
423 | $search->compare( '==', 'attribute.type', $item->getCode() ), |
||
424 | $search->compare( '>', 'attribute.status', 0 ), |
||
425 | $search->getConditions(), |
||
426 | ); |
||
427 | $expr[] = $search->and( $tmp ); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | $search->setConditions( $search->or( $expr ) ); |
||
432 | return $attributeManager->search( $search, array( 'price' ) ); |
||
433 | } |
||
434 | |||
435 | |||
436 | /** |
||
437 | * Returns the order product attribute items for the given IDs and values |
||
438 | * |
||
439 | * @param string $type Attribute type code |
||
440 | * @param array $ids List of attributes IDs of the given type |
||
441 | * @param array $values Associative list of attribute IDs as keys and their codes as values |
||
442 | * @param array $quantities Associative list of attribute IDs as keys and their quantities as values |
||
443 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
444 | */ |
||
445 | protected function getOrderProductAttributes( string $type, array $ids, array $values = [], array $quantities = [] ) : array |
||
474 | } |
||
475 | |||
476 | |||
477 | /** |
||
478 | * Returns the site ID of the ordered product |
||
479 | * |
||
480 | * @param \Aimeos\MShop\Product\Item\Iface $product Product item |
||
481 | * @return string Site ID for the ordered product |
||
482 | */ |
||
483 | protected function getSiteId( \Aimeos\MShop\Product\Item\Iface $product ) : string |
||
489 | } |
||
490 | } |
||
491 |