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 ) |
||
94 | { |
||
95 | if( empty( $refMap ) ) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | $context = $this->getContext(); |
||
100 | $productManager = \Aimeos\MShop\Factory::createManager( $context, 'product' ); |
||
101 | $search = $productManager->createSearch( true ); |
||
102 | |||
103 | $expr = array( |
||
104 | $search->compare( '==', 'product.id', $prodId ), |
||
105 | $search->getConditions(), |
||
106 | ); |
||
107 | |||
108 | foreach( $refMap as $listType => $refIds ) |
||
109 | { |
||
110 | foreach( $refIds as $key => $refId ) |
||
111 | { |
||
112 | $cmpfunc = $search->createFunction( 'product.list', [$domain, $listType, (string) $refId] ); |
||
113 | $expr[] = $search->compare( '!=', $cmpfunc, null ); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
118 | |||
119 | if( count( $productManager->searchItems( $search, [] ) ) === 0 ) |
||
120 | { |
||
121 | $msg = $context->getI18n()->dt( 'controller/frontend', 'Invalid "%1$s" references for product with ID %2$s' ); |
||
122 | throw new \Aimeos\Controller\Frontend\Basket\Exception( sprintf( $msg, $domain, json_encode( $prodId ) ) ); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | |||
127 | /** |
||
128 | * Checks for a locale mismatch and migrates the products to the new basket if necessary. |
||
129 | * |
||
130 | * @param \Aimeos\MShop\Locale\Item\Iface $locale Locale object from current basket |
||
131 | * @param string $type Basket type |
||
132 | */ |
||
133 | protected function checkLocale( \Aimeos\MShop\Locale\Item\Iface $locale, $type ) |
||
134 | { |
||
135 | $errors = []; |
||
136 | $context = $this->getContext(); |
||
137 | $session = $context->getSession(); |
||
138 | |||
139 | $localeStr = $session->get( 'aimeos/basket/locale' ); |
||
140 | $localeKey = $locale->getSite()->getCode() . '|' . $locale->getLanguageId() . '|' . $locale->getCurrencyId(); |
||
141 | |||
142 | if( $localeStr !== null && $localeStr !== $localeKey ) |
||
143 | { |
||
144 | $locParts = explode( '|', $localeStr ); |
||
145 | $locSite = ( isset( $locParts[0] ) ? $locParts[0] : '' ); |
||
146 | $locLanguage = ( isset( $locParts[1] ) ? $locParts[1] : '' ); |
||
147 | $locCurrency = ( isset( $locParts[2] ) ? $locParts[2] : '' ); |
||
148 | |||
149 | $localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' ); |
||
150 | $locale = $localeManager->bootstrap( $locSite, $locLanguage, $locCurrency, false ); |
||
151 | |||
152 | $context = clone $context; |
||
153 | $context->setLocale( $locale ); |
||
154 | |||
155 | $manager = \Aimeos\MShop\Factory::createManager( $context, 'order/base' ); |
||
156 | $basket = $manager->getSession( $type ); |
||
157 | |||
158 | $this->copyAddresses( $basket, $errors, $localeKey ); |
||
159 | $this->copyServices( $basket, $errors ); |
||
160 | $this->copyProducts( $basket, $errors, $localeKey ); |
||
161 | $this->copyCoupons( $basket, $errors, $localeKey ); |
||
162 | |||
163 | $manager->setSession( $basket, $type ); |
||
164 | } |
||
165 | |||
166 | $session->set( 'aimeos/basket/locale', $localeKey ); |
||
167 | } |
||
168 | |||
169 | |||
170 | /** |
||
171 | * Migrates the addresses from the old basket to the current one. |
||
172 | * |
||
173 | * @param \Aimeos\MShop\Order\Item\Base\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\Base\Iface $basket, array $errors, $localeKey ) |
||
179 | { |
||
180 | foreach( $basket->getAddresses() as $type => $item ) |
||
181 | { |
||
182 | try |
||
183 | { |
||
184 | $this->setAddress( $type, $item->toArray() ); |
||
185 | $basket->deleteAddress( $type ); |
||
186 | } |
||
187 | catch( \Exception $e ) |
||
188 | { |
||
189 | $logger = $this->getContext()->getLogger(); |
||
190 | $errors['address'][$type] = $e->getMessage(); |
||
191 | |||
192 | $str = 'Error migrating address with type "%1$s" in basket to locale "%2$s": %3$s'; |
||
193 | $logger->log( sprintf( $str, $type, $localeKey, $e->getMessage() ), \Aimeos\MW\Logger\Base::INFO ); |
||
194 | } |
||
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, $localeKey ) |
||
210 | { |
||
211 | foreach( $basket->getCoupons() as $code => $list ) |
||
212 | { |
||
213 | try |
||
214 | { |
||
215 | $this->addCoupon( $code ); |
||
216 | $basket->deleteCoupon( $code, true ); |
||
217 | } |
||
218 | catch( \Exception $e ) |
||
219 | { |
||
220 | $logger = $this->getContext()->getLogger(); |
||
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->log( sprintf( $str, $code, $localeKey, $e->getMessage() ), \Aimeos\MW\Logger\Base::INFO ); |
||
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, $localeKey ) |
||
241 | { |
||
242 | foreach( $basket->getProducts() as $pos => $product ) |
||
243 | { |
||
244 | if( $product->getFlags() & \Aimeos\MShop\Order\Item\Base\Product\Base::FLAG_IMMUTABLE ) { |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | try |
||
249 | { |
||
250 | $variantIds = $configIds = $customIds = []; |
||
251 | |||
252 | foreach( $product->getAttributeItems() as $attrItem ) |
||
253 | { |
||
254 | switch( $attrItem->getType() ) |
||
255 | { |
||
256 | case 'variant': $variantIds[] = $attrItem->getAttributeId(); break; |
||
257 | case 'config': $configIds[$attrItem->getAttributeId()] = $attrItem->getQuantity(); break; |
||
258 | case 'custom': $customIds[$attrItem->getAttributeId()] = $attrItem->getValue(); break; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | $this->addProduct( |
||
263 | $product->getProductId(), $product->getQuantity(), $product->getStockType(), |
||
264 | $variantIds, $configIds, [], $customIds |
||
265 | ); |
||
266 | |||
267 | $basket->deleteProduct( $pos ); |
||
268 | } |
||
269 | catch( \Exception $e ) |
||
270 | { |
||
271 | $code = $product->getProductCode(); |
||
272 | $logger = $this->getContext()->getLogger(); |
||
273 | $errors['product'][$pos] = $e->getMessage(); |
||
274 | |||
275 | $str = 'Error migrating product with code "%1$s" in basket to locale "%2$s": %3$s'; |
||
276 | $logger->log( sprintf( $str, $code, $localeKey, $e->getMessage() ), \Aimeos\MW\Logger\Base::INFO ); |
||
277 | } |
||
278 | } |
||
279 | |||
280 | return $errors; |
||
281 | } |
||
282 | |||
283 | |||
284 | /** |
||
285 | * Migrates the services from the old basket to the current one. |
||
286 | * |
||
287 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
288 | * @param array $errors Associative list of previous errors |
||
289 | * @return array Associative list of errors occured |
||
290 | */ |
||
291 | protected function copyServices( \Aimeos\MShop\Order\Item\Base\Iface $basket, array $errors ) |
||
292 | { |
||
293 | foreach( $basket->getServices() as $type => $list ) |
||
294 | { |
||
295 | foreach( $list as $item ) |
||
296 | { |
||
297 | try |
||
298 | { |
||
299 | $attributes = []; |
||
300 | |||
301 | foreach( $item->getAttributes() as $attrItem ) { |
||
302 | $attributes[$attrItem->getCode()] = $attrItem->getValue(); |
||
303 | } |
||
304 | |||
305 | $this->addService( $type, $item->getServiceId(), $attributes ); |
||
306 | $basket->deleteService( $type ); |
||
307 | } |
||
308 | catch( \Exception $e ) { ; } // Don't notify the user as appropriate services can be added automatically |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return $errors; |
||
313 | } |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Creates the subscription entries for the ordered products with interval attributes |
||
318 | * |
||
319 | * @param \Aimeos\MShop\Order\Item\Base\Iface $basket Basket object |
||
320 | */ |
||
321 | protected function createSubscriptions( \Aimeos\MShop\Order\Item\Base\Iface $basket ) |
||
322 | { |
||
323 | $manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'subscription' ); |
||
324 | |||
325 | foreach( $basket->getProducts() as $orderProduct ) |
||
326 | { |
||
327 | if( ( $interval = $orderProduct->getAttribute( 'interval', 'config' ) ) !== null ) |
||
328 | { |
||
329 | $item = $manager->createItem(); |
||
330 | $item->setOrderBaseId( $basket->getId() ); |
||
331 | $item->setOrderProductId( $orderProduct->getId() ); |
||
332 | $item->setInterval( $interval ); |
||
333 | $item->setStatus( 1 ); |
||
334 | |||
335 | if( ( $end = $orderProduct->getAttribute( 'intervalend', 'custom' ) ) !== null |
||
336 | || ( $end = $orderProduct->getAttribute( 'intervalend', 'config' ) ) !== null |
||
337 | || ( $end = $orderProduct->getAttribute( 'intervalend', 'hidden' ) ) !== null |
||
338 | ) { |
||
339 | $item->setDateEnd( $end ); |
||
340 | } |
||
341 | |||
342 | $manager->saveItem( $item, false ); |
||
343 | } |
||
344 | } |
||
345 | } |
||
346 | |||
347 | |||
348 | /** |
||
349 | * Returns the attribute items for the given attribute IDs. |
||
350 | * |
||
351 | * @param array $attributeIds List of attribute IDs |
||
352 | * @param string[] $domains Names of the domain items that should be fetched too |
||
353 | * @return array List of items implementing \Aimeos\MShop\Attribute\Item\Iface |
||
354 | * @throws \Aimeos\Controller\Frontend\Basket\Exception If the actual attribute number doesn't match the expected one |
||
355 | */ |
||
356 | protected function getAttributes( array $attributeIds, array $domains = array( 'price', 'text' ) ) |
||
386 | |||
387 | |||
388 | /** |
||
389 | * Returns the attribute items using the given order attribute items. |
||
390 | * |
||
391 | * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Item[] $orderAttributes List of order product attribute items |
||
392 | * @return \Aimeos\MShop\Attribute\Item\Iface[] Associative list of attribute IDs as key and attribute items as values |
||
393 | */ |
||
394 | protected function getAttributeItems( array $orderAttributes ) |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Returns the order product attribute items for the given IDs and values |
||
424 | * |
||
425 | * @param string $type Attribute type code |
||
426 | * @param array $ids List of attributes IDs of the given type |
||
427 | * @param array $values Associative list of attribute IDs as keys and their codes as values |
||
428 | * @param array $quantities Associative list of attribute IDs as keys and their quantities as values |
||
429 | * @return array List of items implementing \Aimeos\MShop\Order\Item\Product\Attribute\Iface |
||
430 | */ |
||
431 | protected function getOrderProductAttributes( $type, array $ids, array $values = [], array $quantities = [] ) |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Returns the list type item for the given domain and code. |
||
460 | * |
||
461 | * @param string $domain Domain name of the list type |
||
462 | * @param string $code Code of the list type |
||
463 | * @return \Aimeos\MShop\Common\Item\Type\Iface List type item |
||
464 | */ |
||
465 | protected function getProductListTypeItem( $domain, $code ) |
||
486 | |||
487 | |||
488 | /** |
||
489 | * Returns the product variants of a selection product that match the given attributes. |
||
490 | * |
||
491 | * @param \Aimeos\MShop\Product\Item\Iface $productItem Product item including sub-products |
||
492 | * @param array $variantAttributeIds IDs for the variant-building attributes |
||
493 | * @param array $domains Names of the domain items that should be fetched too |
||
494 | * @return array List of products matching the given attributes |
||
495 | */ |
||
496 | protected function getProductVariants( \Aimeos\MShop\Product\Item\Iface $productItem, array $variantAttributeIds, |
||
526 | |||
527 | |||
528 | /** |
||
529 | * Returns the value of an array or the default value if it's not available. |
||
530 | * |
||
531 | * @param array $values Associative list of key/value pairs |
||
532 | * @param string $name Name of the key to return the value for |
||
533 | * @param mixed $default Default value if no value is available for the given name |
||
534 | * @return mixed Value from the array or default value |
||
535 | */ |
||
536 | protected function getValue( array $values, $name, $default = null ) |
||
544 | } |
||
545 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.