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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 | private $listTypeItems = []; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Calculates and returns the current price for the given order product and product prices. |
||
| 28 | * |
||
| 29 | * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $product Ordered product item |
||
| 30 | * @param \Aimeos\MShop\Price\Item\Iface[] $prices List of price items |
||
| 31 | * @param integer $quantity New product quantity |
||
| 32 | * @return \Aimeos\MShop\Price\Item\Iface Price item with calculated price |
||
| 33 | */ |
||
| 34 | protected function calcPrice( \Aimeos\MShop\Order\Item\Base\Product\Iface $product, array $prices, $quantity ) |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Checks if the reference IDs are really associated to the product |
||
| 87 | * |
||
| 88 | * @param string|array $prodId Unique ID of the product or list of product IDs |
||
| 89 | * @param string $domain Domain the references must be of |
||
| 90 | * @param array $refMap Associative list of list type codes as keys and lists of reference IDs as values |
||
| 91 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If one or more of the IDs are not associated |
||
| 92 | */ |
||
| 93 | protected function checkListRef( $prodId, $domain, array $refMap ) |
||
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * Checks for a locale mismatch and migrates the products to the new basket if necessary. |
||
| 136 | * |
||
| 137 | * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale object from current basket |
||
| 138 | * @param string $type Basket type |
||
| 139 | */ |
||
| 140 | protected function checkLocale( \Aimeos\MShop\Locale\Item\Iface $locale, $type ) |
||
| 141 | { |
||
| 142 | $errors = []; |
||
| 143 | $context = $this->getContext(); |
||
| 144 | $session = $context->getSession(); |
||
| 145 | |||
| 146 | $localeStr = $session->get( 'aimeos/basket/locale' ); |
||
| 147 | $localeKey = $locale->getSite()->getCode() . '|' . $locale->getLanguageId() . '|' . $locale->getCurrencyId(); |
||
| 148 | |||
| 149 | if( $localeStr !== null && $localeStr !== $localeKey ) |
||
| 150 | { |
||
| 151 | $locParts = explode( '|', $localeStr ); |
||
| 152 | $locSite = ( isset( $locParts[0] ) ? $locParts[0] : '' ); |
||
| 153 | $locLanguage = ( isset( $locParts[1] ) ? $locParts[1] : '' ); |
||
| 154 | $locCurrency = ( isset( $locParts[2] ) ? $locParts[2] : '' ); |
||
| 155 | |||
| 156 | $localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' ); |
||
| 157 | $locale = $localeManager->bootstrap( $locSite, $locLanguage, $locCurrency, false ); |
||
| 158 | |||
| 159 | $context = clone $context; |
||
| 160 | $context->setLocale( $locale ); |
||
| 161 | |||
| 162 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' ); |
||
| 163 | $basket = $manager->getSession( $type ); |
||
| 164 | |||
| 165 | $this->copyAddresses( $basket, $errors, $localeKey ); |
||
| 166 | $this->copyServices( $basket, $errors ); |
||
| 167 | $this->copyProducts( $basket, $errors, $localeKey ); |
||
| 168 | $this->copyCoupons( $basket, $errors, $localeKey ); |
||
| 169 | |||
| 170 | $manager->setSession( $basket, $type ); |
||
| 171 | } |
||
| 172 | |||
| 173 | $session->set( 'aimeos/basket/locale', $localeKey ); |
||
| 174 | } |
||
| 175 | |||
| 176 | |||
| 177 | /** |
||
| 178 | * Migrates the addresses from the old basket to the current one. |
||
| 179 | * |
||
| 180 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
| 181 | * @param array $errors Associative list of previous errors |
||
| 182 | * @param string $localeKey Unique identifier of the site, language and currency |
||
| 183 | * @return array Associative list of errors occured |
||
| 184 | */ |
||
| 185 | protected function copyAddresses( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Migrates the coupons from the old basket to the current one. |
||
| 210 | * |
||
| 211 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
| 212 | * @param array $errors Associative list of previous errors |
||
| 213 | * @param string $localeKey Unique identifier of the site, language and currency |
||
| 214 | * @return array Associative list of errors occured |
||
| 215 | */ |
||
| 216 | protected function copyCoupons( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Migrates the products from the old basket to the current one. |
||
| 241 | * |
||
| 242 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
| 243 | * @param array $errors Associative list of previous errors |
||
| 244 | * @param string $localeKey Unique identifier of the site, language and currency |
||
| 245 | * @return array Associative list of errors occured |
||
| 246 | */ |
||
| 247 | protected function copyProducts( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors, $localeKey ) |
||
| 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 ) |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * Creates the subscription entries for the ordered products with interval attributes |
||
| 324 | * |
||
| 325 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
| 326 | */ |
||
| 327 | protected function createSubscriptions( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns the attribute items for the given attribute IDs. |
||
| 349 | * |
||
| 350 | * @param array $attributeIds List of attribute IDs |
||
| 351 | * @param string[] $domains Names of the domain items that should be fetched too |
||
| 352 | * @return array List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
| 353 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
| 354 | */ |
||
| 355 | protected function getAttributes( array $attributeIds, array $domains = array( 'price', 'text' ) ) |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the attribute items using the given order attribute items. |
||
| 389 | * |
||
| 390 | * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Item[] $orderAttributes List of order product attribute items |
||
| 391 | * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute IDs as key and attribute items as values |
||
| 392 | */ |
||
| 393 | protected function getAttributeItems( array $orderAttributes ) |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the order product attribute items for the given IDs and values |
||
| 423 | * |
||
| 424 | * @param string $type Attribute type code |
||
| 425 | * @param array $ids List of attributes IDs of the given type |
||
| 426 | * @param array $values Associative list of attribute IDs as keys and their codes as values |
||
| 427 | * @param array $quantities Associative list of attribute IDs as keys and their quantities as values |
||
| 428 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
| 429 | */ |
||
| 430 | protected function getOrderProductAttributes( $type, array $ids, array $values = [], array $quantities = [] ) |
||
| 455 | |||
| 456 | |||
| 457 | /** |
||
| 458 | * Returns the list type item for the given domain and code. |
||
| 459 | * |
||
| 460 | * @param string $domain Domain name of the list type |
||
| 461 | * @param string $code Code of the list type |
||
| 462 | * @return \Aimeos\MShop\Common\Item\Type\Iface List type item |
||
| 463 | */ |
||
| 464 | protected function getProductListTypeItem( $domain, $code ) |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * Returns the product variants of a selection product that match the given attributes. |
||
| 489 | * |
||
| 490 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item including sub-products |
||
| 491 | * @param array $variantAttributeIds IDs for the variant-building attributes |
||
| 492 | * @param array $domains Names of the domain items that should be fetched too |
||
| 493 | * @return array List of products matching the given attributes |
||
| 494 | */ |
||
| 495 | protected function getProductVariants( \Aimeos\MShop\Product\Item\Iface $productItem, array $variantAttributeIds, |
||
| 533 | |||
| 534 | |||
| 535 | /** |
||
| 536 | * Returns the value of an array or the default value if it's not available. |
||
| 537 | * |
||
| 538 | * @param array $values Associative list of key/value pairs |
||
| 539 | * @param string $name Name of the key to return the value for |
||
| 540 | * @param mixed $default Default value if no value is available for the given name |
||
| 541 | * @return mixed Value from the array or default value |
||
| 542 | */ |
||
| 543 | protected function getValue( array $values, $name, $default = null ) |
||
| 551 | } |
||
| 552 |