Complex classes like ManagingChannelsContext 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 ManagingChannelsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | final class ManagingChannelsContext implements Context |
||
29 | { |
||
30 | /** @var IndexPageInterface */ |
||
31 | private $indexPage; |
||
32 | |||
33 | /** @var CreatePageInterface */ |
||
34 | private $createPage; |
||
35 | |||
36 | /** @var UpdatePageInterface */ |
||
37 | private $updatePage; |
||
38 | |||
39 | /** @var CurrentPageResolverInterface */ |
||
40 | private $currentPageResolver; |
||
41 | |||
42 | /** @var NotificationCheckerInterface */ |
||
43 | private $notificationChecker; |
||
44 | |||
45 | public function __construct( |
||
58 | |||
59 | /** |
||
60 | * @Given I want to create a new channel |
||
61 | */ |
||
62 | public function iWantToCreateANewChannel() |
||
66 | |||
67 | /** |
||
68 | * @When I specify its code as :code |
||
69 | * @When I do not specify its code |
||
70 | */ |
||
71 | public function iSpecifyItsCodeAs($code = null) |
||
75 | |||
76 | /** |
||
77 | * @When I name it :name |
||
78 | * @When I rename it to :name |
||
79 | * @When I do not name it |
||
80 | * @When I remove its name |
||
81 | */ |
||
82 | public function iNameIt($name = null) |
||
86 | |||
87 | /** |
||
88 | * @When I choose :currency as the base currency |
||
89 | * @When I do not choose base currency |
||
90 | */ |
||
91 | public function iChooseAsABaseCurrency(?CurrencyInterface $currency = null) |
||
95 | |||
96 | /** |
||
97 | * @When I choose :locale as a default locale |
||
98 | * @When I do not choose default locale |
||
99 | */ |
||
100 | public function iChooseAsADefaultLocale($locale = null) |
||
104 | |||
105 | /** |
||
106 | * @When I choose :firstCountry and :secondCountry as operating countries |
||
107 | */ |
||
108 | public function iChooseOperatingCountries(string ...$countries): void |
||
109 | { |
||
110 | $this->createPage->chooseOperatingCountries($countries); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @When I specify menu taxon as :menuTaxon |
||
115 | */ |
||
116 | public function iSpecifyMenuTaxonAs(string $menuTaxon): void |
||
117 | { |
||
118 | $this->createPage->specifyMenuTaxon($menuTaxon); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @When I change its menu taxon to :menuTaxon |
||
123 | */ |
||
124 | public function iChangeItsMenuTaxonTo(string $menuTaxon): void |
||
125 | { |
||
126 | $this->updatePage->changeMenuTaxon($menuTaxon); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @When I allow to skip shipping step if only one shipping method is available |
||
131 | */ |
||
132 | public function iAllowToSkipShippingStepIfOnlyOneShippingMethodIsAvailable() |
||
133 | { |
||
134 | $this->createPage->allowToSkipShippingStep(); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @When I allow to skip payment step if only one payment method is available |
||
139 | */ |
||
140 | public function iAllowToSkipPaymentStepIfOnlyOnePaymentMethodIsAvailable() |
||
141 | { |
||
142 | $this->createPage->allowToSkipPaymentStep(); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @When I add it |
||
147 | * @When I try to add it |
||
148 | */ |
||
149 | public function iAddIt() |
||
150 | { |
||
151 | $this->createPage->create(); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @Then I should see the channel :channelName in the list |
||
156 | * @Then the channel :channelName should appear in the registry |
||
157 | * @Then the channel :channelName should be in the registry |
||
158 | */ |
||
159 | public function theChannelShouldAppearInTheRegistry(string $channelName): void |
||
160 | { |
||
161 | $this->iWantToBrowseChannels(); |
||
162 | |||
163 | Assert::true($this->indexPage->isSingleResourceOnPage(['nameAndDescription' => $channelName])); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @Then /^(this channel) should still be in the registry$/ |
||
168 | */ |
||
169 | public function thisChannelShouldAppearInTheRegistry(ChannelInterface $channel) |
||
170 | { |
||
171 | $this->theChannelShouldAppearInTheRegistry($channel->getName()); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @When I describe it as :description |
||
176 | */ |
||
177 | public function iDescribeItAs($description) |
||
178 | { |
||
179 | $this->createPage->describeItAs($description); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @When I set its hostname as :hostname |
||
184 | */ |
||
185 | public function iSetItsHostnameAs($hostname) |
||
186 | { |
||
187 | $this->createPage->setHostname($hostname); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @When I set its contact email as :contactEmail |
||
192 | */ |
||
193 | public function iSetItsContactEmailAs($contactEmail) |
||
194 | { |
||
195 | $this->createPage->setContactEmail($contactEmail); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @When I define its color as :color |
||
200 | */ |
||
201 | public function iDefineItsColorAs($color) |
||
202 | { |
||
203 | $this->createPage->defineColor($color); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @When I enable it |
||
208 | */ |
||
209 | public function iEnableIt() |
||
210 | { |
||
211 | $this->updatePage->enable(); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @When I disable it |
||
216 | */ |
||
217 | public function iDisableIt() |
||
218 | { |
||
219 | /** @var CreatePageInterface|UpdatePageInterface $currentPage */ |
||
220 | $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]); |
||
221 | |||
222 | $currentPage->disable(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @Then I should be notified that at least one channel has to be defined |
||
227 | */ |
||
228 | public function iShouldBeNotifiedThatAtLeastOneChannelHasToBeDefinedIsRequired() |
||
229 | { |
||
230 | /** @var CreatePageInterface|UpdatePageInterface $currentPage */ |
||
231 | $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]); |
||
232 | |||
233 | Assert::same($currentPage->getValidationMessage('enabled'), 'Must have at least one enabled entity'); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @Then channel with :element :value should not be added |
||
238 | */ |
||
239 | public function channelWithShouldNotBeAdded($element, $value) |
||
240 | { |
||
241 | $this->iWantToBrowseChannels(); |
||
242 | |||
243 | Assert::false($this->indexPage->isSingleResourceOnPage([$element => $value])); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @Then /^I should be notified that ([^"]+) is required$/ |
||
248 | */ |
||
249 | public function iShouldBeNotifiedThatIsRequired($element) |
||
250 | { |
||
251 | /** @var CreatePageInterface|UpdatePageInterface $currentPage */ |
||
252 | $currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]); |
||
253 | |||
254 | Assert::same( |
||
255 | $currentPage->getValidationMessage(StringInflector::nameToCode($element)), |
||
256 | sprintf('Please enter channel %s.', $element) |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @Given I am modifying a channel :channel |
||
262 | * @When I want to modify a channel :channel |
||
263 | * @When /^I want to modify (this channel)$/ |
||
264 | */ |
||
265 | public function iWantToModifyChannel(ChannelInterface $channel): void |
||
266 | { |
||
267 | $this->updatePage->open(['id' => $channel->getId()]); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @Then /^(this channel) name should be "([^"]+)"$/ |
||
272 | * @Then /^(this channel) should still be named "([^"]+)"$/ |
||
273 | */ |
||
274 | public function thisChannelNameShouldBe(ChannelInterface $channel, $channelName) |
||
275 | { |
||
276 | $this->iWantToBrowseChannels(); |
||
277 | |||
278 | Assert::true($this->indexPage->isSingleResourceOnPage([ |
||
279 | 'code' => $channel->getCode(), |
||
280 | 'nameAndDescription' => $channelName, |
||
281 | ])); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @When I save my changes |
||
286 | * @When I try to save my changes |
||
287 | */ |
||
288 | public function iSaveMyChanges() |
||
289 | { |
||
290 | $this->updatePage->saveChanges(); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @Then I should be notified that channel with this code already exists |
||
295 | */ |
||
296 | public function iShouldBeNotifiedThatChannelWithThisCodeAlreadyExists() |
||
300 | |||
301 | /** |
||
302 | * @Then there should still be only one channel with :element :value |
||
303 | */ |
||
304 | public function thereShouldStillBeOnlyOneChannelWithCode($element, $value) |
||
310 | |||
311 | /** |
||
312 | * @When I browse channels |
||
313 | * @When I want to browse channels |
||
314 | */ |
||
315 | public function iWantToBrowseChannels(): void |
||
319 | |||
320 | /** |
||
321 | * @When I check (also) the :channelName channel |
||
322 | */ |
||
323 | public function iCheckTheChannel(string $channelName): void |
||
327 | |||
328 | /** |
||
329 | * @When I delete them |
||
330 | */ |
||
331 | public function iDeleteThem(): void |
||
335 | |||
336 | /** |
||
337 | * @Then I should see a single channel in the list |
||
338 | * @Then I should see :numberOfChannels channels in the list |
||
339 | */ |
||
340 | public function iShouldSeeChannelsInTheList(int $numberOfChannels = 1): void |
||
344 | |||
345 | /** |
||
346 | * @Then the code field should be disabled |
||
347 | */ |
||
348 | public function theCodeFieldShouldBeDisabled() |
||
352 | |||
353 | /** |
||
354 | * @Then /^(this channel) should be disabled$/ |
||
355 | */ |
||
356 | public function thisChannelShouldBeDisabled(ChannelInterface $channel) |
||
360 | |||
361 | /** |
||
362 | * @Then /^(this channel) should be enabled$/ |
||
363 | * @Then channel with name :channel should still be enabled |
||
364 | */ |
||
365 | public function thisChannelShouldBeEnabled(ChannelInterface $channel) |
||
369 | |||
370 | /** |
||
371 | * @When I delete channel :channel |
||
372 | */ |
||
373 | public function iDeleteChannel(ChannelInterface $channel) |
||
378 | |||
379 | /** |
||
380 | * @Then the :channelName channel should no longer exist in the registry |
||
381 | */ |
||
382 | public function thisChannelShouldNoLongerExistInTheRegistry($channelName) |
||
386 | |||
387 | /** |
||
388 | * @Then I should be notified that it cannot be deleted |
||
389 | */ |
||
390 | public function iShouldBeNotifiedThatItCannotBeDeleted() |
||
397 | |||
398 | /** |
||
399 | * @When I make it available (only) in :locale |
||
400 | */ |
||
401 | public function iMakeItAvailableIn(string $localeName): void |
||
408 | |||
409 | /** |
||
410 | * @Then the channel :channel should be available in :locale |
||
411 | */ |
||
412 | public function theChannelShouldBeAvailableIn(ChannelInterface $channel, $locale) |
||
418 | |||
419 | /** |
||
420 | * @When I allow for paying in :currencyCode |
||
421 | */ |
||
422 | public function iAllowToPayingForThisChannel($currencyCode) |
||
429 | |||
430 | /** |
||
431 | * @Then paying in :currencyCode should be possible for the :channel channel |
||
432 | */ |
||
433 | public function payingInEuroShouldBePossibleForTheChannel($currencyCode, ChannelInterface $channel) |
||
439 | |||
440 | /** |
||
441 | * @When I select the :taxZone as default tax zone |
||
442 | */ |
||
443 | public function iSelectDefaultTaxZone($taxZone) |
||
450 | |||
451 | /** |
||
452 | * @Given I remove its default tax zone |
||
453 | */ |
||
454 | public function iRemoveItsDefaultTaxZone() |
||
458 | |||
459 | /** |
||
460 | * @When I select the :taxCalculationStrategy as tax calculation strategy |
||
461 | */ |
||
462 | public function iSelectTaxCalculationStrategy($taxCalculationStrategy) |
||
469 | |||
470 | /** |
||
471 | * @Then the default tax zone for the :channel channel should be :taxZone |
||
472 | */ |
||
473 | public function theDefaultTaxZoneForTheChannelShouldBe(ChannelInterface $channel, $taxZone) |
||
479 | |||
480 | /** |
||
481 | * @Given channel :channel should not have default tax zone |
||
482 | */ |
||
483 | public function channelShouldNotHaveDefaultTaxZone(ChannelInterface $channel) |
||
484 | { |
||
485 | $this->updatePage->open(['id' => $channel->getId()]); |
||
486 | |||
487 | Assert::false($this->updatePage->isAnyDefaultTaxZoneChosen()); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * @Then the tax calculation strategy for the :channel channel should be :taxCalculationStrategy |
||
492 | */ |
||
493 | public function theTaxCalculationStrategyForTheChannelShouldBe(ChannelInterface $channel, $taxCalculationStrategy) |
||
494 | { |
||
495 | $this->updatePage->open(['id' => $channel->getId()]); |
||
496 | |||
497 | Assert::true($this->updatePage->isTaxCalculationStrategyChosen($taxCalculationStrategy)); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * @Then the base currency field should be disabled |
||
502 | */ |
||
503 | public function theBaseCurrencyFieldShouldBeDisabled() |
||
507 | |||
508 | /** |
||
509 | * @Then I should be notified that the default locale has to be enabled |
||
510 | */ |
||
511 | public function iShouldBeNotifiedThatTheDefaultLocaleHasToBeEnabled(): void |
||
518 | |||
519 | /** |
||
520 | * @Given /^(this channel) menu taxon should be "([^"]+)"$/ |
||
521 | * @Given the channel :channel should have :menuTaxon as a menu taxon |
||
522 | */ |
||
523 | public function thisChannelMenuTaxonShouldBe(ChannelInterface $channel, string $menuTaxon): void |
||
531 | |||
532 | /** |
||
533 | * @param bool $state |
||
534 | */ |
||
535 | private function assertChannelState(ChannelInterface $channel, $state) |
||
544 | } |
||
545 |