Complex classes like ManagingPromotionsContext 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 ManagingPromotionsContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class ManagingPromotionsContext implements Context |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var SharedStorageInterface |
||
| 32 | */ |
||
| 33 | private $sharedStorage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var IndexPageInterface |
||
| 37 | */ |
||
| 38 | private $indexPage; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var CreatePageInterface |
||
| 42 | */ |
||
| 43 | private $createPage; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var UpdatePageInterface |
||
| 47 | */ |
||
| 48 | private $updatePage; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var CurrentPageResolverInterface |
||
| 52 | */ |
||
| 53 | private $currentPageResolver; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var NotificationCheckerInterface |
||
| 57 | */ |
||
| 58 | private $notificationChecker; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param SharedStorageInterface $sharedStorage |
||
| 62 | * @param IndexPageInterface $indexPage |
||
| 63 | * @param CreatePageInterface $createPage |
||
| 64 | * @param UpdatePageInterface $updatePage |
||
| 65 | * @param CurrentPageResolverInterface $currentPageResolver |
||
| 66 | * @param NotificationCheckerInterface $notificationChecker |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @Given I want to create a new promotion |
||
| 86 | */ |
||
| 87 | public function iWantToCreateANewPromotion() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @Given I want to browse promotions |
||
| 94 | * @When I browse promotions |
||
| 95 | */ |
||
| 96 | public function iWantToBrowsePromotions() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @When I specify its code as :code |
||
| 103 | * @When I do not specify its code |
||
| 104 | */ |
||
| 105 | public function iSpecifyItsCodeAs($code = null) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @When I name it :name |
||
| 112 | * @When I do not name it |
||
| 113 | * @When I remove its name |
||
| 114 | */ |
||
| 115 | public function iNameIt($name = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @Then the :promotionName promotion should appear in the registry |
||
| 122 | * @Then the :promotionName promotion should exist in the registry |
||
| 123 | * @Then this promotion should still be named :promotionName |
||
| 124 | * @Then promotion :promotionName should still exist in the registry |
||
| 125 | */ |
||
| 126 | public function thePromotionShouldAppearInTheRegistry($promotionName) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @When I add it |
||
| 138 | * @When I try to add it |
||
| 139 | */ |
||
| 140 | public function iAddIt() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @Given I add the "Contains number of items from taxon" rule configured with :count :taxonName |
||
| 147 | */ |
||
| 148 | public function iAddTheContainsTaxonRuleConfiguredWith($count, $taxonName) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @Given I add the "Taxon" rule configured with :firstTaxon |
||
| 157 | * @Given I add the "Taxon" rule configured with :firstTaxon and :secondTaxon |
||
| 158 | */ |
||
| 159 | public function iAddTheTaxonRuleConfiguredWith($firstTaxon, $secondTaxon = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @Given I add the "Total price of items from taxon" rule configured with :count :taxonName |
||
| 171 | */ |
||
| 172 | public function iAddTheRuleConfiguredWith($count, $taxonName) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @Given /^I add the "([^"]+)" action configured with amount of "(?:€|£|\$)([^"]+)"$/ |
||
| 181 | */ |
||
| 182 | public function iAddTheActionConfiguredWithAmount($actionType, $amount) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @When /^I specify that this action should be applied to items with price greater then "(?:€|£|\$)([^"]+)"$/ |
||
| 190 | */ |
||
| 191 | public function iAddAMinPriceFilterRange($minimum) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @When /^I specify that this action should be applied to items with price lesser then "(?:€|£|\$)([^"]+)"$/ |
||
| 198 | */ |
||
| 199 | public function iAddAMaxPriceFilterRange($maximum) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @When /^I specify that this action should be applied to items with price between "(?:€|£|\$)([^"]+)" and "(?:€|£|\$)([^"]+)"$/ |
||
| 206 | */ |
||
| 207 | public function iAddAMinMaxPriceFilterRange($minimum, $maximum) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @When I specify that this action should be applied to items from :taxonName category |
||
| 215 | */ |
||
| 216 | public function iSpecifyThatThisActionShouldBeAppliedToItemsFromCategory($taxonName) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @Given I add the :actionType action configured with a percentage value of :percentage% |
||
| 224 | * @Given I add the :actionType action configured without a percentage value |
||
| 225 | */ |
||
| 226 | public function iAddTheActionConfiguredWithAPercentageValue($actionType, $percentage = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @Then /^there should be (\d+) promotion(?:|s)$/ |
||
| 234 | */ |
||
| 235 | public function thereShouldBePromotion($number) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @Then /^(this promotion) should be coupon based$/ |
||
| 246 | */ |
||
| 247 | public function thisPromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @Then /^I should be able to manage coupons for (this promotion)$/ |
||
| 257 | */ |
||
| 258 | public function iShouldBeAbleToManageCouponsForThisPromotion(PromotionInterface $promotion) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @Then I should be notified that :element is required |
||
| 268 | */ |
||
| 269 | public function iShouldBeNotifiedThatIsRequired($element) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @Then I should be notified that a :element value should be a numeric value |
||
| 276 | */ |
||
| 277 | public function iShouldBeNotifiedThatAMinimalValueShouldBeNumeric($element) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @Then I should be notified that promotion with this code already exists |
||
| 284 | */ |
||
| 285 | public function iShouldBeNotifiedThatPromotionWithThisCodeAlreadyExists() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @Then promotion with :element :name should not be added |
||
| 292 | */ |
||
| 293 | public function promotionWithElementValueShouldNotBeAdded($element, $name) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @Then there should still be only one promotion with :element :value |
||
| 305 | */ |
||
| 306 | public function thereShouldStillBeOnlyOnePromotionWith($element, $value) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @When I set its usage limit to :usageLimit |
||
| 318 | */ |
||
| 319 | public function iSetItsUsageLimitTo($usageLimit) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @Then the :promotion promotion should be available to be used only :usageLimit times |
||
| 328 | */ |
||
| 329 | public function thePromotionShouldBeAvailableToUseOnlyTimes(PromotionInterface $promotion, $usageLimit) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @When I make it exclusive |
||
| 341 | */ |
||
| 342 | public function iMakeItExclusive() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @Then the :promotion promotion should be exclusive |
||
| 351 | */ |
||
| 352 | public function thePromotionShouldBeExclusive(PromotionInterface $promotion) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @When I make it coupon based |
||
| 359 | */ |
||
| 360 | public function iMakeItCouponBased() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @Then the :promotion promotion should be coupon based |
||
| 369 | */ |
||
| 370 | public function thePromotionShouldBeCouponBased(PromotionInterface $promotion) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @When I make it applicable for the :channelName channel |
||
| 377 | */ |
||
| 378 | public function iMakeItApplicableForTheChannel($channelName) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @Then the :promotion promotion should be applicable for the :channelName channel |
||
| 387 | */ |
||
| 388 | public function thePromotionShouldBeApplicableForTheChannel(PromotionInterface $promotion, $channelName) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @Given I want to modify a :promotion promotion |
||
| 400 | * @Given /^I want to modify (this promotion)$/ |
||
| 401 | */ |
||
| 402 | public function iWantToModifyAPromotion(PromotionInterface $promotion) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @Then the code field should be disabled |
||
| 409 | */ |
||
| 410 | public function theCodeFieldShouldBeDisabled() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @When I save my changes |
||
| 420 | * @When I try to save my changes |
||
| 421 | */ |
||
| 422 | public function iSaveMyChanges() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @When /^I delete a ("([^"]+)" promotion)$/ |
||
| 429 | * @When /^I try to delete a ("([^"]+)" promotion)$/ |
||
| 430 | */ |
||
| 431 | public function iDeletePromotion(PromotionInterface $promotion) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @Then /^(this promotion) should no longer exist in the promotion registry$/ |
||
| 441 | */ |
||
| 442 | public function promotionShouldNotExistInTheRegistry(PromotionInterface $promotion) |
||
| 443 | { |
||
| 444 | $this->indexPage->open(); |
||
| 445 | |||
| 446 | Assert::false( |
||
| 447 | $this->indexPage->isSingleResourceOnPage(['code' => $promotion->getCode()]), |
||
| 448 | sprintf('Promotion with code %s exists but should not.', $promotion->getCode()) |
||
| 449 | ); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @Then I should be notified that it is in use and cannot be deleted |
||
| 454 | */ |
||
| 455 | public function iShouldBeNotifiedOfFailure() |
||
| 456 | { |
||
| 457 | $this->notificationChecker->checkNotification( |
||
| 458 | 'Cannot delete, the promotion is in use.', |
||
| 459 | NotificationType::failure() |
||
| 460 | ); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @When I make it available from :startsDate to :endsDate |
||
| 465 | */ |
||
| 466 | public function iMakeItAvailableFromTo(\DateTime $startsDate, \DateTime $endsDate) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @Then the :promotion promotion should be available from :startsDate to :endsDate |
||
| 476 | */ |
||
| 477 | public function thePromotionShouldBeAvailableFromTo(PromotionInterface $promotion, \DateTime $startsDate, \DateTime $endsDate) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @Then I should be notified that promotion cannot end before it start |
||
| 494 | */ |
||
| 495 | public function iShouldBeNotifiedThatPromotionCannotEndBeforeItsEvenStart() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @Then I should be notified that this value should not be blank |
||
| 505 | */ |
||
| 506 | public function iShouldBeNotifiedThatThisValueShouldNotBeBlank() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @Then I should be notified that the maximum value of a percentage discount is 100% |
||
| 516 | */ |
||
| 517 | public function iShouldBeNotifiedThatTheMaximumValueOfAPercentageDiscountIs100() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @Then I should be notified that a percentage discount value must be at least 0% |
||
| 527 | */ |
||
| 528 | public function iShouldBeNotifiedThatAPercentageDiscountValueMustBeAtLeast0() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * @Then the promotion :promotion should be used :usage time(s) |
||
| 538 | */ |
||
| 539 | public function thePromotionShouldBeUsedTime(PromotionInterface $promotion, $usage) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @When I add the "Contains product" rule configured with the :productName product |
||
| 550 | */ |
||
| 551 | public function iAddTheRuleConfiguredWithTheProduct($productName) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @When I specify that this action should be applied to the :productName product |
||
| 559 | */ |
||
| 560 | public function iSpecifyThatThisActionShouldBeAppliedToTheProduct($productName) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $element |
||
| 567 | * @param string $expectedMessage |
||
| 568 | */ |
||
| 569 | private function assertFieldValidationMessage($element, $expectedMessage) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param PromotionInterface $promotion |
||
| 579 | * @param string $field |
||
| 580 | */ |
||
| 581 | private function assertIfFieldIsTrue(PromotionInterface $promotion, $field) |
||
| 590 | } |
||
| 591 |