Total Complexity | 59 |
Total Lines | 474 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 1 |
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\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\Product\Iface $orderProduct, |
||
32 | \Aimeos\Map $prices, float $quantity ) : \Aimeos\MShop\Price\Item\Iface |
||
33 | { |
||
34 | $context = $this->context(); |
||
35 | $priceManager = \Aimeos\MShop::create( $context, 'price' ); |
||
36 | $price = $priceManager->getLowestPrice( $prices, $quantity, null, $orderProduct->getSiteId() ); |
||
37 | |||
38 | // customers can pay what they would like to pay |
||
39 | if( ( $attr = $orderProduct->getAttributeItem( 'price', 'custom' ) ) !== null ) |
||
40 | { |
||
41 | $amount = $attr->getValue(); |
||
42 | |||
43 | if( preg_match( '/^[0-9]*(\.[0-9]+)?$/', $amount ) !== 1 || ( (double) $amount ) < 0.01 ) |
||
|
|||
44 | { |
||
45 | $msg = $context->translate( 'controller/frontend', 'Invalid price value "%1$s"' ); |
||
46 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $amount ) ); |
||
47 | } |
||
48 | |||
49 | $price = $price->setValue( $amount ); |
||
50 | } |
||
51 | |||
52 | $orderAttributes = $orderProduct->getAttributeItems(); |
||
53 | $attrItems = $this->getAttributeItems( $orderAttributes ); |
||
54 | |||
55 | // add prices of (optional) attributes |
||
56 | foreach( $orderAttributes as $orderAttrItem ) |
||
57 | { |
||
58 | if( !( $attrItem = $attrItems->get( $orderAttrItem->getAttributeId() ) ) ) { |
||
59 | continue; |
||
60 | } |
||
61 | |||
62 | $prices = $attrItem->getRefItems( 'price', 'default', 'default' ); |
||
63 | |||
64 | if( !$prices->isEmpty() ) |
||
65 | { |
||
66 | $attrPrice = $priceManager->getLowestPrice( $prices, $orderAttrItem->getQuantity(), null, $orderProduct->getSiteId() ); |
||
67 | $price = $price->addItem( clone $attrPrice, $orderAttrItem->getQuantity() ); |
||
68 | $orderAttrItem->setPrice( $attrPrice->addItem( $attrPrice, $orderAttrItem->getQuantity() - 1 )->getValue() ); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | // remove product rebate of original price in favor to rebates granted for the order |
||
73 | return $price->setRebate( '0.00' ); |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Returns the allowed quantity for the given product |
||
79 | * |
||
80 | * @param \Aimeos\MShop\Product\Item\Iface $product Product item including referenced items |
||
81 | * @param float $quantity New product quantity |
||
82 | * @return float Updated quantity value |
||
83 | */ |
||
84 | protected function checkQuantity( \Aimeos\MShop\Product\Item\Iface $product, float $quantity ) : float |
||
85 | { |
||
86 | $scale = $product->getScale(); |
||
87 | |||
88 | if( fmod( $quantity, $scale ) >= 0.0005 ) { |
||
89 | return round( ceil( $quantity / $scale ) * $scale, 4 ); |
||
90 | } |
||
91 | |||
92 | return $quantity; |
||
93 | } |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Checks if the attribute IDs are really associated to the product |
||
98 | * |
||
99 | * @param \Aimeos\MShop\Product\Item\Iface $product Product item with referenced items |
||
100 | * @param string $domain Domain the references must be of |
||
101 | * @param array $refMap Associative list of list type codes as keys and lists of reference IDs as values |
||
102 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If one or more of the IDs are not associated |
||
103 | */ |
||
104 | protected function checkAttributes( array $products, string $listType, array $refIds ) |
||
105 | { |
||
106 | $attrIds = map(); |
||
107 | |||
108 | foreach( $products as $product ) { |
||
109 | $attrIds->merge( $product->getRefItems( 'attribute', null, $listType )->keys() ); |
||
110 | } |
||
111 | |||
112 | if( $attrIds->intersect( $refIds )->count() !== count( $refIds ) ) |
||
113 | { |
||
114 | $i18n = $this->context()->i18n(); |
||
115 | $prodIds = map( $products )->getId()->join( ', ' ); |
||
116 | $msg = $i18n->dt( 'controller/frontend', 'Invalid "%1$s" references for product with ID %2$s' ); |
||
117 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, 'attribute', $prodIds ) ); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Checks for a locale mismatch and migrates the products to the new basket if necessary. |
||
124 | * |
||
125 | * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale object from current basket |
||
126 | * @param string $type Basket type |
||
127 | */ |
||
128 | protected function checkLocale( \Aimeos\MShop\Locale\Item\Iface $locale, string $type ) |
||
167 | } |
||
168 | |||
169 | |||
170 | /** |
||
171 | * Migrates the addresses from the old basket to the current one. |
||
172 | * |
||
173 | * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object |
||
174 | * @param array $errors Associative list of previous errors |
||
175 | * @param string $localeKey Unique identifier of the site, language and currency |
||
176 | * @return array Associative list of errors occured |
||
177 | */ |
||
178 | protected function copyAddresses( \Aimeos\MShop\Order\Item\Iface $basket, array $errors, string $localeKey ) : array |
||
202 | } |
||
203 | |||
204 | |||
205 | /** |
||
206 | * Migrates the coupons from the old basket to the current one. |
||
207 | * |
||
208 | * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object |
||
209 | * @param array $errors Associative list of previous errors |
||
210 | * @param string $localeKey Unique identifier of the site, language and currency |
||
211 | * @return array Associative list of errors occured |
||
212 | */ |
||
213 | protected function copyCoupons( \Aimeos\MShop\Order\Item\Iface $basket, array $errors, string $localeKey ) : array |
||
233 | } |
||
234 | |||
235 | |||
236 | /** |
||
237 | * Migrates the products from the old basket to the current one. |
||
238 | * |
||
239 | * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object |
||
240 | * @param array $errors Associative list of previous errors |
||
241 | * @param string $localeKey Unique identifier of the site, language and currency |
||
242 | * @return array Associative list of errors occured |
||
243 | */ |
||
244 | protected function copyProducts( \Aimeos\MShop\Order\Item\Iface $basket, array $errors, string $localeKey ) : array |
||
291 | } |
||
292 | |||
293 | |||
294 | /** |
||
295 | * Migrates the services from the old basket to the current one. |
||
296 | * |
||
297 | * @param \Aimeos\MShop\Order\Item\Iface $basket Basket object |
||
298 | * @param array $errors Associative list of previous errors |
||
299 | * @return array Associative list of errors occured |
||
300 | */ |
||
301 | protected function copyServices( \Aimeos\MShop\Order\Item\Iface $basket, array $errors ) : array |
||
336 | } |
||
337 | |||
338 | |||
339 | /** |
||
340 | * Creates the subscription entries for the ordered products with interval attributes |
||
341 | * |
||
342 | * @param \Aimeos\MShop\Order\Item\Iface $order Basket object |
||
343 | */ |
||
344 | protected function createSubscriptions( \Aimeos\MShop\Order\Item\Iface $order ) |
||
345 | { |
||
346 | $types = ['config', 'custom', 'hidden', 'variant']; |
||
347 | $manager = \Aimeos\MShop::create( $this->context(), 'subscription' ); |
||
348 | |||
349 | foreach( $order->getProducts() as $orderProduct ) |
||
350 | { |
||
351 | if( ( $interval = $orderProduct->getAttribute( 'interval', $types ) ) !== null ) |
||
352 | { |
||
353 | $interval = is_array( $interval ) ? reset( $interval ) : $interval; |
||
354 | |||
355 | $item = $manager->create()->setInterval( $interval ) |
||
356 | ->setProductId( $orderProduct->getProductId() ) |
||
357 | ->setOrderProductId( $orderProduct->getId() ) |
||
358 | ->setOrderId( $order->getId() ); |
||
359 | |||
360 | if( ( $end = $orderProduct->getAttribute( 'intervalend', $types ) ) !== null ) { |
||
361 | $item = $item->setDateEnd( $end ); |
||
362 | } |
||
363 | |||
364 | $manager->save( $item, false ); |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Returns the attribute items for the given attribute IDs. |
||
372 | * |
||
373 | * @param array $attributeIds List of attribute IDs |
||
374 | * @param string[] $domains Names of the domain items that should be fetched too |
||
375 | * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
376 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
377 | */ |
||
378 | protected function getAttributes( array $attributeIds, array $domains = ['text'] ) : \Aimeos\Map |
||
379 | { |
||
380 | if( empty( $attributeIds ) ) { |
||
381 | return map(); |
||
382 | } |
||
383 | |||
384 | $attributeManager = \Aimeos\MShop::create( $this->context(), 'attribute' ); |
||
385 | |||
386 | $search = $attributeManager->filter( true ) |
||
387 | ->add( ['attribute.id' => $attributeIds] ) |
||
388 | ->slice( 0, count( $attributeIds ) ); |
||
389 | |||
390 | $attrItems = $attributeManager->search( $search, $domains ); |
||
391 | |||
392 | if( $attrItems->count() !== count( $attributeIds ) ) |
||
393 | { |
||
394 | $i18n = $this->context()->i18n(); |
||
395 | $expected = implode( ',', $attributeIds ); |
||
396 | $actual = $attrItems->keys()->join( ',' ); |
||
397 | $msg = $i18n->dt( 'controller/frontend', 'Available attribute IDs "%1$s" do not match the given attribute IDs "%2$s"' ); |
||
398 | |||
399 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $actual, $expected ) ); |
||
400 | } |
||
401 | |||
402 | return $attrItems; |
||
403 | } |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Returns the attribute items using the given order attribute items. |
||
408 | * |
||
409 | * @param \Aimeos\Map $orderAttributes List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
410 | * @return \Aimeos\Map List of attribute IDs as key and attribute items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
411 | */ |
||
412 | protected function getAttributeItems( \Aimeos\Map $orderAttributes ) : \Aimeos\Map |
||
413 | { |
||
414 | if( $orderAttributes->isEmpty() ) { |
||
415 | return map(); |
||
416 | } |
||
417 | |||
418 | $attributeManager = \Aimeos\MShop::create( $this->context(), 'attribute' ); |
||
419 | $search = $attributeManager->filter( true ); |
||
420 | $expr = []; |
||
421 | |||
422 | foreach( $orderAttributes as $item ) |
||
423 | { |
||
424 | if( is_scalar( $item->getValue() ) ) |
||
425 | { |
||
426 | $tmp = array( |
||
427 | $search->compare( '==', 'attribute.domain', 'product' ), |
||
428 | $search->compare( '==', 'attribute.code', $item->getValue() ), |
||
429 | $search->compare( '==', 'attribute.type', $item->getCode() ), |
||
430 | $search->compare( '>', 'attribute.status', 0 ), |
||
431 | $search->getConditions(), |
||
432 | ); |
||
433 | $expr[] = $search->and( $tmp ); |
||
434 | } |
||
435 | } |
||
436 | |||
437 | $search->setConditions( $search->or( $expr ) ); |
||
438 | return $attributeManager->search( $search, array( 'price' ) ); |
||
439 | } |
||
440 | |||
441 | |||
442 | /** |
||
443 | * Returns the order product attribute items for the given IDs and values |
||
444 | * |
||
445 | * @param string $type Attribute type code |
||
446 | * @param array $ids List of attributes IDs of the given type |
||
447 | * @param array $values Associative list of attribute IDs as keys and their codes as values |
||
448 | * @param array $quantities Associative list of attribute IDs as keys and their quantities as values |
||
449 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
450 | */ |
||
451 | protected function getOrderProductAttributes( string $type, array $ids, array $values = [], array $quantities = [] ) : array |
||
480 | } |
||
481 | |||
482 | |||
483 | /** |
||
484 | * Returns the vendor for the given site ID |
||
485 | * |
||
486 | * @param string $siteId Unique ID of the site |
||
487 | * @return string Vendor name |
||
488 | */ |
||
489 | protected function getVendor( string $siteId ) : string |
||
495 | } |
||
496 | } |
||
497 |