Complex classes like PromotionContext 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 PromotionContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | final class PromotionContext implements Context |
||
34 | { |
||
35 | /** |
||
36 | * @var SharedStorageInterface |
||
37 | */ |
||
38 | private $sharedStorage; |
||
39 | |||
40 | /** |
||
41 | * @var PromotionActionFactoryInterface |
||
42 | */ |
||
43 | private $actionFactory; |
||
44 | |||
45 | /** |
||
46 | * @var PromotionCouponFactoryInterface |
||
47 | */ |
||
48 | private $couponFactory; |
||
49 | |||
50 | /** |
||
51 | * @var PromotionRuleFactoryInterface |
||
52 | */ |
||
53 | private $ruleFactory; |
||
54 | |||
55 | /** |
||
56 | * @var TestPromotionFactoryInterface |
||
57 | */ |
||
58 | private $testPromotionFactory; |
||
59 | |||
60 | /** |
||
61 | * @var PromotionRepositoryInterface |
||
62 | */ |
||
63 | private $promotionRepository; |
||
64 | |||
65 | /** |
||
66 | * @var ObjectManager |
||
67 | */ |
||
68 | private $objectManager; |
||
69 | |||
70 | /** |
||
71 | * @param SharedStorageInterface $sharedStorage |
||
72 | * @param PromotionActionFactoryInterface $actionFactory |
||
73 | * @param PromotionCouponFactoryInterface $couponFactory |
||
74 | * @param PromotionRuleFactoryInterface $ruleFactory |
||
75 | * @param TestPromotionFactoryInterface $testPromotionFactory |
||
76 | * @param PromotionRepositoryInterface $promotionRepository |
||
77 | * @param ObjectManager $objectManager |
||
78 | */ |
||
79 | public function __construct( |
||
96 | |||
97 | /** |
||
98 | * @Given there is a promotion :promotionName |
||
99 | * @Given there is a promotion :promotionName identified by :promotionCode code |
||
100 | */ |
||
101 | public function thereIsPromotion($promotionName, $promotionCode = null) |
||
114 | |||
115 | /** |
||
116 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
117 | */ |
||
118 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
129 | |||
130 | /** |
||
131 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
132 | */ |
||
133 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
145 | |||
146 | /** |
||
147 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
148 | */ |
||
149 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
158 | |||
159 | /** |
||
160 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
161 | * @Given the store has also promotion :promotionName with coupon :couponCode |
||
162 | * @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
||
163 | */ |
||
164 | public function thereIsPromotionWithCoupon($promotionName, $couponCode, $usageLimit = null) |
||
182 | |||
183 | /** |
||
184 | * @Given /^(this promotion) has already expired$/ |
||
185 | */ |
||
186 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
192 | |||
193 | /** |
||
194 | * @Given /^(this coupon) has already expired$/ |
||
195 | */ |
||
196 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
202 | |||
203 | /** |
||
204 | * @Given /^(this coupon) expires tomorrow$/ |
||
205 | */ |
||
206 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
212 | |||
213 | /** |
||
214 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
215 | */ |
||
216 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
223 | |||
224 | /** |
||
225 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
226 | */ |
||
227 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
233 | |||
234 | /** |
||
235 | * @Given /^(this coupon) can be used twice per customer$/ |
||
236 | */ |
||
237 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
243 | |||
244 | /** |
||
245 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
246 | */ |
||
247 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
251 | |||
252 | /** |
||
253 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel) and ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel)$/ |
||
254 | */ |
||
255 | public function thisPromotionGivesDiscountToEveryOrderInTheChannelAndDiscountToEveryOrderInTheChannel( |
||
272 | |||
273 | /** |
||
274 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
275 | */ |
||
276 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
280 | |||
281 | /** |
||
282 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
283 | */ |
||
284 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
293 | |||
294 | /** |
||
295 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
296 | */ |
||
297 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
307 | |||
308 | /** |
||
309 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
310 | */ |
||
311 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
321 | |||
322 | /** |
||
323 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
324 | */ |
||
325 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
332 | |||
333 | /** |
||
334 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
335 | */ |
||
336 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
340 | |||
341 | /** |
||
342 | * @Given /^([^"]+) gives ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
343 | */ |
||
344 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
351 | |||
352 | /** |
||
353 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
354 | */ |
||
355 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
362 | |||
363 | /** |
||
364 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
365 | */ |
||
366 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
373 | |||
374 | /** |
||
375 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") in base currency or ("(?:€|£|\$)[^"]+") in "([^"]+)" off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
376 | */ |
||
377 | public function thisPromotionGivesInDifferentCurrenciesOffOnEveryProductWithMinimumPriceAt( |
||
389 | |||
390 | /** |
||
391 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
392 | */ |
||
393 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
405 | |||
406 | /** |
||
407 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
408 | */ |
||
409 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
416 | |||
417 | /** |
||
418 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
419 | */ |
||
420 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
432 | |||
433 | /** |
||
434 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
435 | */ |
||
436 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
445 | |||
446 | /** |
||
447 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
448 | */ |
||
449 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
458 | |||
459 | /** |
||
460 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
461 | */ |
||
462 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
473 | |||
474 | /** |
||
475 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
476 | */ |
||
477 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
483 | |||
484 | /** |
||
485 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
486 | */ |
||
487 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
493 | |||
494 | /** |
||
495 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
496 | */ |
||
497 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
506 | |||
507 | /** |
||
508 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
509 | */ |
||
510 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
524 | |||
525 | /** |
||
526 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
527 | */ |
||
528 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
548 | |||
549 | /** |
||
550 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
551 | */ |
||
552 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
570 | |||
571 | /** |
||
572 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
573 | */ |
||
574 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
589 | |||
590 | /** |
||
591 | * @Given /^(it) is coupon based promotion$/ |
||
592 | */ |
||
593 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
599 | |||
600 | /** |
||
601 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
602 | */ |
||
603 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
609 | |||
610 | /** |
||
611 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
612 | */ |
||
613 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
619 | |||
620 | /** |
||
621 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
622 | */ |
||
623 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
629 | |||
630 | /** |
||
631 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
632 | */ |
||
633 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
637 | |||
638 | /** |
||
639 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
640 | */ |
||
641 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
645 | |||
646 | /** |
||
647 | * @param array $taxonCodes |
||
648 | * |
||
649 | * @return array |
||
650 | */ |
||
651 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
655 | |||
656 | /** |
||
657 | * @param array $productCodes |
||
658 | * |
||
659 | * @return array |
||
660 | */ |
||
661 | private function getProductsFilterConfiguration(array $productCodes) |
||
665 | |||
666 | /** |
||
667 | * @param int $minAmount |
||
668 | * @param int $maxAmount |
||
669 | * |
||
670 | * @return array |
||
671 | */ |
||
672 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
681 | |||
682 | /** |
||
683 | * @param PromotionInterface $promotion |
||
684 | * @param int $discount |
||
685 | * @param array $configuration |
||
686 | * @param PromotionRuleInterface|null $rule |
||
687 | */ |
||
688 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
699 | |||
700 | /** |
||
701 | * @param PromotionInterface $promotion |
||
702 | * @param int $discount |
||
703 | * @param array $configuration |
||
704 | * @param PromotionRuleInterface|null $rule |
||
705 | */ |
||
706 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
717 | |||
718 | /** |
||
719 | * @param PromotionInterface $promotion |
||
720 | * @param int $discount |
||
721 | * @param array $configuration |
||
722 | * @param PromotionRuleInterface|null $rule |
||
723 | * @param ChannelInterface|null $channel |
||
724 | */ |
||
725 | private function createFixedPromotion( |
||
736 | |||
737 | /** |
||
738 | * @param PromotionInterface $promotion |
||
739 | * @param float $discount |
||
740 | * @param array $configuration |
||
741 | * @param PromotionRuleInterface $rule |
||
742 | */ |
||
743 | private function createPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
747 | |||
748 | /** |
||
749 | * @param PromotionInterface $promotion |
||
750 | * @param PromotionActionInterface $action |
||
751 | * @param array $configuration |
||
752 | * @param PromotionRuleInterface|null $rule |
||
753 | */ |
||
754 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
766 | } |
||
767 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.