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 |
||
34 | final class PromotionContext implements Context |
||
35 | { |
||
36 | /** |
||
37 | * @var SharedStorageInterface |
||
38 | */ |
||
39 | private $sharedStorage; |
||
40 | |||
41 | /** |
||
42 | * @var PromotionActionFactoryInterface |
||
43 | */ |
||
44 | private $actionFactory; |
||
45 | |||
46 | /** |
||
47 | * @var PromotionCouponFactoryInterface |
||
48 | */ |
||
49 | private $couponFactory; |
||
50 | |||
51 | /** |
||
52 | * @var PromotionRuleFactoryInterface |
||
53 | */ |
||
54 | private $ruleFactory; |
||
55 | |||
56 | /** |
||
57 | * @var TestPromotionFactoryInterface |
||
58 | */ |
||
59 | private $testPromotionFactory; |
||
60 | |||
61 | /** |
||
62 | * @var PromotionRepositoryInterface |
||
63 | */ |
||
64 | private $promotionRepository; |
||
65 | |||
66 | /** |
||
67 | * @var ObjectManager |
||
68 | */ |
||
69 | private $objectManager; |
||
70 | |||
71 | /** |
||
72 | * @param SharedStorageInterface $sharedStorage |
||
73 | * @param PromotionActionFactoryInterface $actionFactory |
||
74 | * @param PromotionCouponFactoryInterface $couponFactory |
||
75 | * @param PromotionRuleFactoryInterface $ruleFactory |
||
76 | * @param TestPromotionFactoryInterface $testPromotionFactory |
||
77 | * @param PromotionRepositoryInterface $promotionRepository |
||
78 | * @param ObjectManager $objectManager |
||
79 | */ |
||
80 | public function __construct( |
||
81 | SharedStorageInterface $sharedStorage, |
||
82 | PromotionActionFactoryInterface $actionFactory, |
||
83 | PromotionCouponFactoryInterface $couponFactory, |
||
84 | PromotionRuleFactoryInterface $ruleFactory, |
||
85 | TestPromotionFactoryInterface $testPromotionFactory, |
||
86 | PromotionRepositoryInterface $promotionRepository, |
||
87 | ObjectManager $objectManager |
||
88 | ) { |
||
89 | $this->sharedStorage = $sharedStorage; |
||
90 | $this->actionFactory = $actionFactory; |
||
91 | $this->couponFactory = $couponFactory; |
||
92 | $this->ruleFactory = $ruleFactory; |
||
93 | $this->testPromotionFactory = $testPromotionFactory; |
||
94 | $this->promotionRepository = $promotionRepository; |
||
95 | $this->objectManager = $objectManager; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @Given there is a promotion :promotionName |
||
100 | * @Given there is a promotion :promotionName identified by :promotionCode code |
||
101 | */ |
||
102 | public function thereIsPromotion($promotionName, $promotionCode = null) |
||
103 | { |
||
104 | $promotion = $this->testPromotionFactory |
||
105 | ->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
||
106 | ; |
||
107 | |||
108 | if (null !== $promotionCode) { |
||
109 | $promotion->setCode($promotionCode); |
||
110 | } |
||
111 | |||
112 | $this->promotionRepository->add($promotion); |
||
113 | $this->sharedStorage->set('promotion', $promotion); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @Given /^there is a promotion "([^"]+)" with priority ([^"]+)$/ |
||
118 | */ |
||
119 | public function thereIsAPromotionWithPriority($promotionName, $priority) |
||
120 | { |
||
121 | $promotion = $this->testPromotionFactory |
||
122 | ->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
||
123 | ; |
||
124 | |||
125 | $promotion->setPriority((int) $priority); |
||
126 | |||
127 | $this->promotionRepository->add($promotion); |
||
128 | $this->sharedStorage->set('promotion', $promotion); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @Given /^there is an exclusive promotion "([^"]+)"(?:| with priority ([^"]+))$/ |
||
133 | */ |
||
134 | public function thereIsAnExclusivePromotionWithPriority($promotionName, $priority = 0) |
||
135 | { |
||
136 | $promotion = $this->testPromotionFactory |
||
137 | ->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
||
138 | ; |
||
139 | |||
140 | $promotion->setExclusive(true); |
||
141 | $promotion->setPriority((int) $priority); |
||
142 | |||
143 | $this->promotionRepository->add($promotion); |
||
144 | $this->sharedStorage->set('promotion', $promotion); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @Given there is a promotion :promotionName limited to :usageLimit usages |
||
149 | */ |
||
150 | public function thereIsPromotionLimitedToUsages($promotionName, $usageLimit) |
||
151 | { |
||
152 | $promotion = $this->testPromotionFactory->createForChannel($promotionName, $this->sharedStorage->get('channel')); |
||
153 | |||
154 | $promotion->setUsageLimit((int) $usageLimit); |
||
155 | |||
156 | $this->promotionRepository->add($promotion); |
||
157 | $this->sharedStorage->set('promotion', $promotion); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @Given the store has 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) |
||
165 | { |
||
166 | /** @var PromotionCouponInterface $coupon */ |
||
167 | $coupon = $this->couponFactory->createNew(); |
||
168 | $coupon->setCode($couponCode); |
||
169 | $coupon->setUsageLimit((null === $usageLimit) ? null : (int) $usageLimit); |
||
170 | |||
171 | $promotion = $this->testPromotionFactory |
||
172 | ->createForChannel($promotionName, $this->sharedStorage->get('channel')) |
||
173 | ; |
||
174 | $promotion->addCoupon($coupon); |
||
175 | $promotion->setCouponBased(true); |
||
176 | |||
177 | $this->promotionRepository->add($promotion); |
||
178 | |||
179 | $this->sharedStorage->set('promotion', $promotion); |
||
180 | $this->sharedStorage->set('coupon', $coupon); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @Given /^(this promotion) has already expired$/ |
||
185 | */ |
||
186 | public function thisPromotionHasExpired(PromotionInterface $promotion) |
||
192 | |||
193 | /** |
||
194 | * @Given /^(this promotion) expires tomorrow$/ |
||
195 | */ |
||
196 | public function thisPromotionExpiresTomorrow(PromotionInterface $promotion) |
||
197 | { |
||
198 | $promotion->setEndsAt(new \DateTime('tomorrow')); |
||
202 | |||
203 | /** |
||
204 | * @Given /^(this promotion) has started yesterday$/ |
||
205 | */ |
||
206 | public function thisPromotionHasStartedYesterday(PromotionInterface $promotion) |
||
212 | |||
213 | /** |
||
214 | * @Given /^(this promotion) starts tomorrow$/ |
||
215 | */ |
||
216 | public function thisPromotionStartsTomorrow(PromotionInterface $promotion) |
||
222 | |||
223 | /** |
||
224 | * @Given /^(this coupon) has already expired$/ |
||
225 | */ |
||
226 | public function thisCouponHasExpired(PromotionCouponInterface $coupon) |
||
232 | |||
233 | /** |
||
234 | * @Given /^(this coupon) expires tomorrow$/ |
||
235 | */ |
||
236 | public function thisCouponExpiresTomorrow(PromotionCouponInterface $coupon) |
||
242 | |||
243 | /** |
||
244 | * @Given /^(this coupon) has already reached its usage limit$/ |
||
245 | */ |
||
246 | public function thisCouponHasReachedItsUsageLimit(PromotionCouponInterface $coupon) |
||
253 | |||
254 | /** |
||
255 | * @Given /^(this coupon) can be used (\d+) times?$/ |
||
256 | */ |
||
257 | public function thisCouponCanBeUsedNTimes(PromotionCouponInterface $coupon, $usageLimit) |
||
263 | |||
264 | /** |
||
265 | * @Given /^(this coupon) can be used twice per customer$/ |
||
266 | */ |
||
267 | public function thisCouponCanBeUsedTwicePerCustomer(PromotionCouponInterface $coupon) |
||
273 | |||
274 | /** |
||
275 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order$/ |
||
276 | */ |
||
277 | public function itGivesFixedDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
281 | |||
282 | /** |
||
283 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel) and ("(?:€|£|\$)[^"]+") discount to every order in the ("[^"]+" channel)$/ |
||
284 | */ |
||
285 | public function thisPromotionGivesDiscountToEveryOrderInTheChannelAndDiscountToEveryOrderInTheChannel( |
||
302 | |||
303 | /** |
||
304 | * @Given /^([^"]+) gives ("[^"]+%") discount to every order$/ |
||
305 | */ |
||
306 | public function itGivesPercentageDiscountToEveryOrder(PromotionInterface $promotion, $discount) |
||
310 | |||
311 | /** |
||
312 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with quantity at least ([^"]+)$/ |
||
313 | */ |
||
314 | public function itGivesFixedDiscountToEveryOrderWithQuantityAtLeast( |
||
323 | |||
324 | /** |
||
325 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") discount to every order with items total at least ("[^"]+")$/ |
||
326 | */ |
||
327 | public function itGivesFixedDiscountToEveryOrderWithItemsTotalAtLeast( |
||
337 | |||
338 | /** |
||
339 | * @Given /^([^"]+) gives ("[^"]+%") off on every product when the item total is at least ("(?:€|£|\$)[^"]+")$/ |
||
340 | */ |
||
341 | public function itGivesOffOnEveryItemWhenItemTotalExceeds( |
||
351 | |||
352 | /** |
||
353 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order$/ |
||
354 | */ |
||
355 | public function itGivesPercentageDiscountOnShippingToEveryOrder(PromotionInterface $promotion, $discount) |
||
362 | |||
363 | /** |
||
364 | * @Given /^([^"]+) gives free shipping to every order$/ |
||
365 | */ |
||
366 | public function thePromotionGivesFreeShippingToEveryOrder(PromotionInterface $promotion) |
||
370 | |||
371 | /** |
||
372 | * @Given /^([^"]+) gives(?:| another) ("[^"]+%") off every product (classified as "[^"]+")$/ |
||
373 | */ |
||
374 | public function itGivesPercentageOffEveryProductClassifiedAs( |
||
381 | |||
382 | /** |
||
383 | * @Given /^([^"]+) gives(?:| another) ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+")$/ |
||
384 | */ |
||
385 | public function itGivesFixedOffEveryProductClassifiedAs( |
||
392 | |||
393 | /** |
||
394 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
395 | */ |
||
396 | public function thisPromotionGivesOffOnEveryProductWithMinimumPriceAt( |
||
403 | |||
404 | /** |
||
405 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
406 | */ |
||
407 | public function thisPromotionGivesOffOnEveryProductPricedBetween( |
||
419 | |||
420 | /** |
||
421 | * @Given /^([^"]+) gives ("[^"]+%") off on every product with minimum price at ("(?:€|£|\$)[^"]+")$/ |
||
422 | */ |
||
423 | public function thisPromotionPercentageGivesOffOnEveryProductWithMinimumPriceAt( |
||
430 | |||
431 | /** |
||
432 | * @Given /^([^"]+) gives ("[^"]+%") off on every product priced between ("(?:€|£|\$)[^"]+") and ("(?:€|£|\$)[^"]+")$/ |
||
433 | */ |
||
434 | public function thisPromotionPercentageGivesOffOnEveryProductPricedBetween( |
||
446 | |||
447 | /** |
||
448 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+")$/ |
||
449 | */ |
||
450 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAs( |
||
459 | |||
460 | /** |
||
461 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+" or "[^"]+")$/ |
||
462 | */ |
||
463 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsOr( |
||
472 | |||
473 | /** |
||
474 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains products (classified as "[^"]+") with a minimum value of ("(?:€|£|\$)[^"]+")$/ |
||
475 | */ |
||
476 | public function thePromotionGivesOffIfOrderContainsProductsClassifiedAsAndPricedAt( |
||
487 | |||
488 | /** |
||
489 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off customer's (\d)(?:st|nd|rd|th) order$/ |
||
490 | */ |
||
491 | public function itGivesFixedOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
497 | |||
498 | /** |
||
499 | * @Given /^([^"]+) gives ("[^"]+%") off on the customer's (\d)(?:st|nd|rd|th) order$/ |
||
500 | */ |
||
501 | public function itGivesPercentageOffCustomersNthOrder(PromotionInterface $promotion, $discount, $nth) |
||
507 | |||
508 | /** |
||
509 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and ("(?:€|£|\$)[^"]+") discount on every order$/ |
||
510 | */ |
||
511 | public function itGivesPercentageOffOnEveryProductClassifiedAsAndAmountDiscountOnOrder( |
||
520 | |||
521 | /** |
||
522 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on every product (classified as "[^"]+") and a free shipping to every order with items total equal at least ("[^"]+")$/ |
||
523 | */ |
||
524 | public function itGivesOffOnEveryProductClassifiedAsAndAFreeShippingToEveryOrderWithItemsTotalEqualAtLeast( |
||
538 | |||
539 | /** |
||
540 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") and a ("(?:€|£|\$)[^"]+") discount to every order with items total equal at least ("(?:€|£|\$)[^"]+")$/ |
||
541 | */ |
||
542 | public function itGivesOffOnEveryProductClassifiedAsAndAFixedDiscountToEveryOrderWithItemsTotalEqualAtLeast( |
||
562 | |||
563 | /** |
||
564 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+" or "[^"]+") if order contains any product (classified as "[^"]+" or "[^"]+")$/ |
||
565 | */ |
||
566 | public function itGivesOffOnEveryProductClassifiedAsOrIfOrderContainsAnyProductClassifiedAsOr( |
||
584 | |||
585 | /** |
||
586 | * @Given /^([^"]+) gives ("[^"]+%") off on every product (classified as "[^"]+") if order contains any product (classified as "[^"]+")$/ |
||
587 | */ |
||
588 | public function itGivesOffOnEveryProductClassifiedAsIfOrderContainsAnyProductClassifiedAs( |
||
603 | |||
604 | /** |
||
605 | * @Given /^(it) is coupon based promotion$/ |
||
606 | */ |
||
607 | public function itIsCouponBasedPromotion(PromotionInterface $promotion) |
||
613 | |||
614 | /** |
||
615 | * @Given /^(the promotion) was disabled for the (channel "[^"]+")$/ |
||
616 | */ |
||
617 | public function thePromotionWasDisabledForTheChannel(PromotionInterface $promotion, ChannelInterface $channel) |
||
623 | |||
624 | /** |
||
625 | * @Given /^the (coupon "[^"]+") was used up to its usage limit$/ |
||
626 | */ |
||
627 | public function theCouponWasUsed(PromotionCouponInterface $coupon) |
||
633 | |||
634 | /** |
||
635 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off if order contains (?:a|an) ("[^"]+" product)$/ |
||
636 | */ |
||
637 | public function thePromotionGivesOffIfOrderContainsProducts(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
643 | |||
644 | /** |
||
645 | * @Given /^([^"]+) gives ("(?:€|£|\$)[^"]+") off on a ("[^"]*" product)$/ |
||
646 | */ |
||
647 | public function itGivesFixedDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
651 | |||
652 | /** |
||
653 | * @Given /^([^"]+) gives ("[^"]+%") off on a ("[^"]*" product)$/ |
||
654 | */ |
||
655 | public function itGivesPercentageDiscountOffOnAProduct(PromotionInterface $promotion, $discount, ProductInterface $product) |
||
659 | |||
660 | /** |
||
661 | * @Given /^([^"]+) gives ("[^"]+%") off the order for customers from ("[^"]*" group)$/ |
||
662 | */ |
||
663 | public function thePromotionGivesOffTheOrderForCustomersFromGroup( |
||
674 | |||
675 | /** |
||
676 | * @Given /^([^"]+) gives ("[^"]+%") discount on shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
677 | */ |
||
678 | public function itGivesDiscountOnShippingToEveryOrderOver( |
||
689 | |||
690 | /** |
||
691 | * @Given /^([^"]+) gives free shipping to every order over ("(?:€|£|\$)[^"]+")$/ |
||
692 | */ |
||
693 | public function itGivesFreeShippingToEveryOrderOver(PromotionInterface $promotion, $itemTotal) |
||
697 | |||
698 | /** |
||
699 | * @param array $taxonCodes |
||
700 | * |
||
701 | * @return array |
||
702 | */ |
||
703 | private function getTaxonFilterConfiguration(array $taxonCodes) |
||
707 | |||
708 | /** |
||
709 | * @param array $productCodes |
||
710 | * |
||
711 | * @return array |
||
712 | */ |
||
713 | private function getProductsFilterConfiguration(array $productCodes) |
||
717 | |||
718 | /** |
||
719 | * @param int $minAmount |
||
720 | * @param int $maxAmount |
||
721 | * |
||
722 | * @return array |
||
723 | */ |
||
724 | private function getPriceRangeFilterConfiguration($minAmount, $maxAmount = null) |
||
733 | |||
734 | /** |
||
735 | * @param PromotionInterface $promotion |
||
736 | * @param int $discount |
||
737 | * @param array $configuration |
||
738 | * @param PromotionRuleInterface|null $rule |
||
739 | */ |
||
740 | private function createUnitFixedPromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
751 | |||
752 | /** |
||
753 | * @param PromotionInterface $promotion |
||
754 | * @param int $discount |
||
755 | * @param array $configuration |
||
756 | * @param PromotionRuleInterface|null $rule |
||
757 | */ |
||
758 | private function createUnitPercentagePromotion(PromotionInterface $promotion, $discount, array $configuration = [], PromotionRuleInterface $rule = null) |
||
769 | |||
770 | /** |
||
771 | * @param PromotionInterface $promotion |
||
772 | * @param int $discount |
||
773 | * @param array $configuration |
||
774 | * @param PromotionRuleInterface|null $rule |
||
775 | * @param ChannelInterface|null $channel |
||
776 | */ |
||
777 | private function createFixedPromotion( |
||
788 | |||
789 | /** |
||
790 | * @param PromotionInterface $promotion |
||
791 | * @param float $discount |
||
792 | * @param array $configuration |
||
793 | * @param PromotionRuleInterface $rule |
||
794 | */ |
||
795 | private function createPercentagePromotion( |
||
803 | |||
804 | /** |
||
805 | * @param PromotionInterface $promotion |
||
806 | * @param PromotionActionInterface $action |
||
807 | * @param array $configuration |
||
808 | * @param PromotionRuleInterface|null $rule |
||
809 | */ |
||
810 | private function persistPromotion(PromotionInterface $promotion, PromotionActionInterface $action, array $configuration, PromotionRuleInterface $rule = null) |
||
822 | } |
||
823 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.