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 |
||
37 | final class PromotionContext implements Context |
||
38 | { |
||
39 | /** |
||
40 | * @var SharedStorageInterface |
||
41 | */ |
||
42 | private $sharedStorage; |
||
43 | |||
44 | /** |
||
45 | * @var PromotionActionFactoryInterface |
||
46 | */ |
||
47 | private $actionFactory; |
||
48 | |||
49 | /** |
||
50 | * @var PromotionCouponFactoryInterface |
||
51 | */ |
||
52 | private $couponFactory; |
||
53 | |||
54 | /** |
||
55 | * @var PromotionRuleFactoryInterface |
||
56 | */ |
||
57 | private $ruleFactory; |
||
58 | |||
59 | /** |
||
60 | * @var TestPromotionFactoryInterface |
||
61 | */ |
||
62 | private $testPromotionFactory; |
||
63 | |||
64 | /** |
||
65 | * @var PromotionRepositoryInterface |
||
66 | */ |
||
67 | private $promotionRepository; |
||
68 | |||
69 | /** |
||
70 | * @var ObjectManager |
||
71 | */ |
||
72 | private $objectManager; |
||
73 | |||
74 | /** |
||
75 | * @param SharedStorageInterface $sharedStorage |
||
76 | * @param PromotionActionFactoryInterface $actionFactory |
||
77 | * @param PromotionCouponFactoryInterface $couponFactory |
||
78 | * @param PromotionRuleFactoryInterface $ruleFactory |
||
79 | * @param TestPromotionFactoryInterface $testPromotionFactory |
||
80 | * @param PromotionRepositoryInterface $promotionRepository |
||
81 | * @param ObjectManager $objectManager |
||
82 | */ |
||
83 | public function __construct( |
||
100 | |||
101 | /** |
||
102 | * @Given there is a promotion :promotionName |
||
103 | * @Given there is a promotion :promotionName identified by :promotionCode code |
||
104 | */ |
||
105 | public function thereIsPromotion($promotionName, $promotionCode = null) |
||
118 | |||
119 | /** |
||
120 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
121 | */ |
||
122 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
133 | |||
134 | /** |
||
135 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
136 | */ |
||
137 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
149 | |||
150 | /** |
||
151 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
152 | */ |
||
153 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
162 | |||
163 | /** |
||
164 | * @Given the store has promotion :promotionName with coupon :couponCode |
||
165 | * @Given the store has a promotion :promotionName with a coupon :couponCode that is limited to :usageLimit usages |
||
166 | */ |
||
167 | public function thereIsPromotionWithCoupon($promotionName, $couponCode, $usageLimit = null) |
||
168 | { |
||
169 | /** @var PromotionCouponInterface $coupon */ |
||
170 | $coupon = $this->couponFactory->createNew(); |
||
171 | $coupon->setCode($couponCode); |
||
172 | $coupon->setUsageLimit((null === $usageLimit) ? null : (int) $usageLimit); |
||
173 | |||
174 | $promotion = $this->testPromotionFactory |
||
175 | ->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
||
176 | ; |
||
177 | $promotion->addCoupon($coupon); |
||
178 | $promotion->setCouponBased(true); |
||
179 | |||
180 | $this->promotionRepository->add($promotion); |
||
181 | |||
182 | $this->sharedStorage->set('promotion', $promotion); |
||
183 | $this->sharedStorage->set('coupon', $coupon); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @Given /^(this promotion) has already expired$/ |
||
188 | */ |
||
189 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
195 | |||
196 | /** |
||
197 | * @Given /^(this promotion) expires tomorrow$/ |
||
198 | */ |
||
199 | public function thisPromotionExpiresTomorrow(PromotionInterface $promotion) |
||
205 | |||
206 | /** |
||
207 | * @Given /^(this promotion) has started yesterday$/ |
||
208 | */ |
||
209 | public function thisPromotionHasStartedYesterday(PromotionInterface $promotion) |
||
215 | |||
216 | /** |
||
217 | * @Given /^(this promotion) starts tomorrow$/ |
||
218 | */ |
||
219 | public function thisPromotionStartsTomorrow(PromotionInterface $promotion) |
||
225 | |||
226 | /** |
||
227 | * @Given /^(this coupon) has already expired$/ |
||
228 | */ |
||
229 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
235 | |||
236 | /** |
||
237 | * @Given /^(this coupon) expires tomorrow$/ |
||
238 | */ |
||
239 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
245 | |||
246 | /** |
||
247 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
248 | */ |
||
249 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
256 | |||
257 | /** |
||
258 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
259 | */ |
||
260 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
266 | |||
267 | /** |
||
268 | * @Given /^(this coupon) can be used twice per customer$/ |
||
269 | */ |
||
270 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
276 | |||
277 | /** |
||
278 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
279 | */ |
||
280 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
284 | |||
285 | /** |
||
286 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel) and ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel)$/ |
||
287 | */ |
||
288 | public function thisPromotionGivesDiscountToEveryOrderInTheChannelAndDiscountToEveryOrderInTheChannel( |
||
305 | |||
306 | /** |
||
307 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
308 | */ |
||
309 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
313 | |||
314 | /** |
||
315 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
316 | */ |
||
317 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
326 | |||
327 | /** |
||
328 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
329 | */ |
||
330 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
340 | |||
341 | /** |
||
342 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
343 | */ |
||
344 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
354 | |||
355 | /** |
||
356 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
357 | */ |
||
358 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
365 | |||
366 | /** |
||
367 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
368 | */ |
||
369 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
373 | |||
374 | /** |
||
375 | * @Given /^([^"]+) gives(?:| another) ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
376 | */ |
||
377 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
384 | |||
385 | /** |
||
386 | * @Given /^([^"]+) gives(?:| another) ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
387 | */ |
||
388 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
395 | |||
396 | /** |
||
397 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
398 | */ |
||
399 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
406 | |||
407 | /** |
||
408 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
409 | */ |
||
410 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
422 | |||
423 | /** |
||
424 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
425 | */ |
||
426 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
433 | |||
434 | /** |
||
435 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
436 | */ |
||
437 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
449 | |||
450 | /** |
||
451 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
452 | */ |
||
453 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
462 | |||
463 | /** |
||
464 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
465 | */ |
||
466 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
475 | |||
476 | /** |
||
477 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
478 | */ |
||
479 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
490 | |||
491 | /** |
||
492 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
493 | */ |
||
494 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
500 | |||
501 | /** |
||
502 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
503 | */ |
||
504 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
510 | |||
511 | /** |
||
512 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
513 | */ |
||
514 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
523 | |||
524 | /** |
||
525 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
526 | */ |
||
527 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
541 | |||
542 | /** |
||
543 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
544 | */ |
||
545 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
565 | |||
566 | /** |
||
567 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
568 | */ |
||
569 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
587 | |||
588 | /** |
||
589 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
590 | */ |
||
591 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
606 | |||
607 | /** |
||
608 | * @Given /^(it) is coupon based promotion$/ |
||
609 | */ |
||
610 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
616 | |||
617 | /** |
||
618 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
619 | */ |
||
620 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
626 | |||
627 | /** |
||
628 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
629 | */ |
||
630 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
636 | |||
637 | /** |
||
638 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
639 | */ |
||
640 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
646 | |||
647 | /** |
||
648 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
649 | */ |
||
650 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
654 | |||
655 | /** |
||
656 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
657 | */ |
||
658 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
662 | |||
663 | /** |
||
664 | * @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/ |
||
665 | */ |
||
666 | public function thePromotionGivesOffTheOrderForCustomersFromGroup( |
||
677 | |||
678 | /** |
||
679 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
680 | */ |
||
681 | public function itGivesDiscountOnShippingToEveryOrderOver( |
||
692 | |||
693 | /** |
||
694 | * @Given /^([^"]+) gives free shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
695 | */ |
||
696 | public function itGivesFreeShippingToEveryOrderOver(PromotionInterface $promotion, $itemTotal) |
||
700 | |||
701 | /** |
||
702 | * @param array $taxonCodes |
||
703 | * |
||
704 | * @return array |
||
705 | */ |
||
706 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
710 | |||
711 | /** |
||
712 | * @param array $productCodes |
||
713 | * |
||
714 | * @return array |
||
715 | */ |
||
716 | private function getProductsFilterConfiguration(array $productCodes) |
||
720 | |||
721 | /** |
||
722 | * @param int $minAmount |
||
723 | * @param int $maxAmount |
||
724 | * |
||
725 | * @return array |
||
726 | */ |
||
727 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
736 | |||
737 | /** |
||
738 | * @param PromotionInterface $promotion |
||
739 | * @param int $discount |
||
740 | * @param array $configuration |
||
741 | * @param PromotionRuleInterface|null $rule |
||
742 | */ |
||
743 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
754 | |||
755 | /** |
||
756 | * @param PromotionInterface $promotion |
||
757 | * @param int $discount |
||
758 | * @param array $configuration |
||
759 | * @param PromotionRuleInterface|null $rule |
||
760 | */ |
||
761 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
772 | |||
773 | /** |
||
774 | * @param PromotionInterface $promotion |
||
775 | * @param int $discount |
||
776 | * @param array $configuration |
||
777 | * @param PromotionRuleInterface|null $rule |
||
778 | * @param ChannelInterface|null $channel |
||
779 | */ |
||
780 | private function createFixedPromotion( |
||
791 | |||
792 | /** |
||
793 | * @param PromotionInterface $promotion |
||
794 | * @param float $discount |
||
795 | * @param array $configuration |
||
796 | * @param PromotionRuleInterface $rule |
||
797 | */ |
||
798 | private function createPercentagePromotion( |
||
806 | |||
807 | /** |
||
808 | * @param PromotionInterface $promotion |
||
809 | * @param PromotionActionInterface $action |
||
810 | * @param array $configuration |
||
811 | * @param PromotionRuleInterface|null $rule |
||
812 | */ |
||
813 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
825 | } |
||
826 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.