Complex classes like ManagingProductsContext 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 ManagingProductsContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | final class ManagingProductsContext implements Context |
||
41 | { |
||
42 | /** |
||
43 | * @var SharedStorageInterface |
||
44 | */ |
||
45 | private $sharedStorage; |
||
46 | |||
47 | /**x |
||
48 | * @var CreateSimpleProductPageInterface |
||
49 | */ |
||
50 | private $createSimpleProductPage; |
||
51 | |||
52 | /** |
||
53 | * @var CreateConfigurableProductPageInterface |
||
54 | */ |
||
55 | private $createConfigurableProductPage; |
||
56 | |||
57 | /** |
||
58 | * @var IndexPageInterface |
||
59 | */ |
||
60 | private $indexPage; |
||
61 | |||
62 | /** |
||
63 | * @var UpdateSimpleProductPageInterface |
||
64 | */ |
||
65 | private $updateSimpleProductPage; |
||
66 | |||
67 | /** |
||
68 | * @var UpdateConfigurableProductPageInterface |
||
69 | */ |
||
70 | private $updateConfigurableProductPage; |
||
71 | |||
72 | /** |
||
73 | * @var ProductReviewIndexPageInterface |
||
74 | */ |
||
75 | private $productReviewIndexPage; |
||
76 | |||
77 | /** |
||
78 | * @var IndexPerTaxonPageInterface |
||
79 | */ |
||
80 | private $indexPerTaxonPage; |
||
81 | |||
82 | /** |
||
83 | * @var CurrentPageResolverInterface |
||
84 | */ |
||
85 | private $currentPageResolver; |
||
86 | |||
87 | /** |
||
88 | * @var NotificationCheckerInterface |
||
89 | */ |
||
90 | private $notificationChecker; |
||
91 | |||
92 | /** |
||
93 | * @param SharedStorageInterface $sharedStorage |
||
94 | * @param CreateSimpleProductPageInterface $createSimpleProductPage |
||
95 | * @param CreateConfigurableProductPageInterface $createConfigurableProductPage |
||
96 | * @param IndexPageInterface $indexPage |
||
97 | * @param UpdateSimpleProductPageInterface $updateSimpleProductPage |
||
98 | * @param UpdateConfigurableProductPageInterface $updateConfigurableProductPage |
||
99 | * @param ProductReviewIndexPageInterface $productReviewIndexPage |
||
100 | * @param IndexPerTaxonPageInterface $indexPerTaxonPage |
||
101 | * @param CurrentPageResolverInterface $currentPageResolver |
||
102 | * @param NotificationCheckerInterface $notificationChecker |
||
103 | */ |
||
104 | public function __construct( |
||
127 | |||
128 | /** |
||
129 | * @Given I want to create a new simple product |
||
130 | */ |
||
131 | public function iWantToCreateANewSimpleProduct() |
||
135 | |||
136 | /** |
||
137 | * @Given I want to create a new configurable product |
||
138 | */ |
||
139 | public function iWantToCreateANewConfigurableProduct() |
||
143 | |||
144 | /** |
||
145 | * @When I specify its code as :code |
||
146 | * @When I do not specify its code |
||
147 | */ |
||
148 | public function iSpecifyItsCodeAs($code = null) |
||
154 | |||
155 | /** |
||
156 | * @When I name it :name in :language |
||
157 | * @When I rename it to :name in :language |
||
158 | */ |
||
159 | public function iRenameItToIn($name, $language) |
||
165 | |||
166 | /** |
||
167 | * @When I add it |
||
168 | * @When I try to add it |
||
169 | */ |
||
170 | public function iAddIt() |
||
177 | |||
178 | /** |
||
179 | * @When I disable its inventory tracking |
||
180 | */ |
||
181 | public function iDisableItsTracking() |
||
185 | |||
186 | /** |
||
187 | * @When I enable its inventory tracking |
||
188 | */ |
||
189 | public function iEnableItsTracking() |
||
193 | |||
194 | /** |
||
195 | * @When /^I set its(?:| default) price to "(?:€|£|\$)([^"]+)" for "([^"]+)" channel$/ |
||
196 | */ |
||
197 | public function iSetItsPriceTo($price, $channelName) |
||
201 | |||
202 | /** |
||
203 | * @When I make it available in channel :channel |
||
204 | */ |
||
205 | public function iMakeItAvailableInChannel($channel) |
||
209 | |||
210 | /** |
||
211 | * @When I assign it to channel :channel |
||
212 | */ |
||
213 | public function iAssignItToChannel($channel) |
||
218 | |||
219 | /** |
||
220 | * @When I choose :calculatorName calculator |
||
221 | */ |
||
222 | public function iChooseCalculator($calculatorName) |
||
226 | |||
227 | /** |
||
228 | * @When I set its slug to :slug |
||
229 | * @When I set its slug to :slug in :language |
||
230 | * @When I remove its slug |
||
231 | */ |
||
232 | public function iSetItsSlugToIn($slug = null, $language = 'en_US') |
||
236 | |||
237 | /** |
||
238 | * @When I enable slug modification |
||
239 | * @When I enable slug modification in :localeCode |
||
240 | */ |
||
241 | public function iEnableSlugModification($localeCode = 'en_US') |
||
246 | |||
247 | /** |
||
248 | * @Then the product :productName should appear in the store |
||
249 | * @Then the product :productName should be in the shop |
||
250 | * @Then this product should still be named :productName |
||
251 | */ |
||
252 | public function theProductShouldAppearInTheShop($productName) |
||
253 | { |
||
254 | $this->iWantToBrowseProducts(); |
||
255 | |||
256 | Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $productName])); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @Given I am browsing products |
||
261 | * @When I want to browse products |
||
262 | */ |
||
263 | public function iWantToBrowseProducts() |
||
264 | { |
||
265 | $this->indexPage->open(); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @When /^I am browsing products from ("([^"]+)" taxon)$/ |
||
270 | */ |
||
271 | public function iAmBrowsingProductsFromTaxon(TaxonInterface $taxon) |
||
272 | { |
||
273 | $this->indexPerTaxonPage->open(['taxonId' => $taxon->getId()]); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @When I filter them by :taxonName taxon |
||
278 | */ |
||
279 | public function iFilterThemByTaxon($taxonName) |
||
280 | { |
||
281 | $this->indexPage->filterByTaxon($taxonName); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @Then I should( still) see a product with :field :value |
||
286 | */ |
||
287 | public function iShouldSeeProductWith($field, $value) |
||
288 | { |
||
289 | Assert::true($this->indexPage->isSingleResourceOnPage([$field => $value])); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @Then I should not see any product with :field :value |
||
294 | */ |
||
295 | public function iShouldNotSeeAnyProductWith($field, $value) |
||
296 | { |
||
297 | Assert::false($this->indexPage->isSingleResourceOnPage([$field => $value])); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @Then the first product on the list should have :field :value |
||
302 | */ |
||
303 | public function theFirstProductOnTheListShouldHave($field, $value) |
||
304 | { |
||
305 | $currentPage = $this->resolveCurrentPage(); |
||
306 | |||
307 | Assert::same($currentPage->getColumnFields($field)[0], $value); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @Then the last product on the list should have :field :value |
||
312 | */ |
||
313 | public function theLastProductOnTheListShouldHave($field, $value) |
||
314 | { |
||
315 | $values = $this->indexPerTaxonPage->getColumnFields($field); |
||
316 | |||
317 | Assert::same(end($values), $value); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @When I switch the way products are sorted by :field |
||
322 | * @When I start sorting products by :field |
||
323 | * @Given the products are already sorted by :field |
||
324 | */ |
||
325 | public function iSortProductsBy($field) |
||
326 | { |
||
327 | $this->indexPage->sortBy($field); |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @Then I should see :numberOfProducts products in the list |
||
332 | */ |
||
333 | public function iShouldSeeProductsInTheList($numberOfProducts) |
||
334 | { |
||
335 | Assert::same($this->indexPage->countItems(), (int) $numberOfProducts); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @When I delete the :product product |
||
340 | * @When I try to delete the :product product |
||
341 | */ |
||
342 | public function iDeleteProduct(ProductInterface $product) |
||
343 | { |
||
344 | $this->sharedStorage->set('product', $product); |
||
345 | |||
346 | $this->iWantToBrowseProducts(); |
||
347 | $this->indexPage->deleteResourceOnPage(['name' => $product->getName()]); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * @Then /^(this product) should not exist in the product catalog$/ |
||
352 | */ |
||
353 | public function productShouldNotExist(ProductInterface $product) |
||
354 | { |
||
355 | $this->iWantToBrowseProducts(); |
||
356 | |||
357 | Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $product->getCode()])); |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @Then I should be notified that this product is in use and cannot be deleted |
||
362 | */ |
||
363 | public function iShouldBeNotifiedOfFailure() |
||
364 | { |
||
365 | $this->notificationChecker->checkNotification( |
||
366 | 'Cannot delete, the product is in use.', |
||
367 | NotificationType::failure() |
||
368 | ); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @Then /^(this product) should still exist in the product catalog$/ |
||
373 | */ |
||
374 | public function productShouldExistInTheProductCatalog(ProductInterface $product) |
||
375 | { |
||
376 | $this->theProductShouldAppearInTheShop($product->getName()); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @When I want to modify the :product product |
||
381 | * @When /^I want to modify (this product)$/ |
||
382 | */ |
||
383 | public function iWantToModifyAProduct(ProductInterface $product) |
||
384 | { |
||
385 | $this->sharedStorage->set('product', $product); |
||
386 | |||
387 | if ($product->isSimple()) { |
||
388 | $this->updateSimpleProductPage->open(['id' => $product->getId()]); |
||
389 | return; |
||
390 | } |
||
391 | |||
392 | $this->updateConfigurableProductPage->open(['id' => $product->getId()]); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @Then the code field should be disabled |
||
397 | */ |
||
398 | public function theCodeFieldShouldBeDisabled() |
||
399 | { |
||
400 | $currentPage = $this->resolveCurrentPage(); |
||
401 | |||
402 | Assert::true($currentPage->isCodeDisabled()); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @Then the slug field should not be editable |
||
407 | * @Then the slug field in :localeCode (also )should not be editable |
||
408 | */ |
||
409 | public function theSlugFieldShouldNotBeEditable($localeCode = 'en_US') |
||
410 | { |
||
411 | Assert::true($this->updateSimpleProductPage->isSlugReadOnlyIn($localeCode)); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @Then this product name should be :name |
||
416 | */ |
||
417 | public function thisProductElementShouldBe($name) |
||
418 | { |
||
419 | $this->assertElementValue('name', $name); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @Then /^I should be notified that (code|name|slug) is required$/ |
||
424 | */ |
||
425 | public function iShouldBeNotifiedThatIsRequired($element) |
||
426 | { |
||
427 | $this->assertValidationMessage($element, sprintf('Please enter product %s.', $element)); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @When I save my changes |
||
432 | * @When I try to save my changes |
||
433 | */ |
||
434 | public function iSaveMyChanges() |
||
435 | { |
||
436 | $currentPage = $this->resolveCurrentPage(); |
||
437 | |||
438 | $currentPage->saveChanges(); |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * @When /^I change its price to (?:€|£|\$)([^"]+) for "([^"]+)" channel$/ |
||
443 | */ |
||
444 | public function iChangeItsPriceTo($price, $channelName) |
||
448 | |||
449 | /** |
||
450 | * @Given I add the :optionName option to it |
||
451 | */ |
||
452 | public function iAddTheOptionToIt($optionName) |
||
456 | |||
457 | /** |
||
458 | * @When I set its :attribute attribute to :value |
||
459 | * @When I set its :attribute attribute to :value in :language |
||
460 | */ |
||
461 | public function iSetItsAttributeTo($attribute, $value, $language = 'en_US') |
||
465 | |||
466 | /** |
||
467 | * @When I remove its :attribute attribute |
||
468 | * @When I remove its :attribute attribute from :language |
||
469 | */ |
||
470 | public function iRemoveItsAttribute($attribute, $language = 'en_US') |
||
474 | |||
475 | /** |
||
476 | * @When I try to add new attributes |
||
477 | */ |
||
478 | public function iTryToAddNewAttributes() |
||
482 | |||
483 | /** |
||
484 | * @Then attribute :attributeName of product :product should be :value |
||
485 | * @Then attribute :attributeName of product :product should be :value in :language |
||
486 | */ |
||
487 | public function itsAttributeShouldBe($attributeName, ProductInterface $product, $value, $language = 'en_US') |
||
493 | |||
494 | /** |
||
495 | * @Then /^(product "[^"]+") should not have a "([^"]+)" attribute$/ |
||
496 | */ |
||
497 | public function productShouldNotHaveAttribute(ProductInterface $product, $attribute) |
||
503 | |||
504 | /** |
||
505 | * @Then /^product "[^"]+" should not have any attributes$/ |
||
506 | * @Then /^product "[^"]+" should have (\d+) attributes?$/ |
||
507 | */ |
||
508 | public function productShouldNotHaveAnyAttributes($count = 0) |
||
509 | { |
||
510 | Assert::same($this->updateSimpleProductPage->getNumberOfAttributes(), (int) $count); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @Given product with :element :value should not be added |
||
515 | */ |
||
516 | public function productWithNameShouldNotBeAdded($element, $value) |
||
522 | |||
523 | /** |
||
524 | * @When I remove its name from :language translation |
||
525 | */ |
||
526 | public function iRemoveItsNameFromTranslation($language) |
||
532 | |||
533 | /** |
||
534 | * @Then /^this product should have (?:a|an) "([^"]+)" option$/ |
||
535 | */ |
||
536 | public function thisProductShouldHaveOption($productOption) |
||
540 | |||
541 | /** |
||
542 | * @Then the option field should be disabled |
||
543 | */ |
||
544 | public function theOptionFieldShouldBeDisabled() |
||
548 | |||
549 | /** |
||
550 | * @When /^I choose main (taxon "([^"]+)")$/ |
||
551 | */ |
||
552 | public function iChooseMainTaxon(TaxonInterface $taxon) |
||
558 | |||
559 | /** |
||
560 | * @Then /^the slug of the ("[^"]+" product) should(?:| still) be "([^"]+)"$/ |
||
561 | * @Then /^the slug of the ("[^"]+" product) should(?:| still) be "([^"]+)" (in the "[^"]+" locale)$/ |
||
562 | */ |
||
563 | public function productSlugShouldBe(ProductInterface $product, $slug, $locale = 'en_US') |
||
569 | |||
570 | /** |
||
571 | * @Then /^(this product) main taxon should be "([^"]+)"$/ |
||
572 | */ |
||
573 | public function thisProductMainTaxonShouldBe(ProductInterface $product, $taxonName) |
||
580 | |||
581 | /** |
||
582 | * @Then /^inventory of (this product) should not be tracked$/ |
||
583 | */ |
||
584 | public function thisProductShouldNotBeTracked(ProductInterface $product) |
||
590 | |||
591 | /** |
||
592 | * @Then /^inventory of (this product) should be tracked$/ |
||
593 | */ |
||
594 | public function thisProductShouldBeTracked(ProductInterface $product) |
||
600 | |||
601 | /** |
||
602 | * @When I attach the :path image with :type type |
||
603 | * @When I attach the :path image |
||
604 | */ |
||
605 | public function iAttachImageWithType($path, $type = null) |
||
606 | { |
||
607 | $currentPage = $this->resolveCurrentPage(); |
||
608 | |||
609 | $currentPage->attachImage($path, $type); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * @When I associate as :productAssociationType the :productName product |
||
614 | * @When I associate as :productAssociationType the :firstProductName and :secondProductName products |
||
615 | */ |
||
616 | public function iAssociateProductsAsProductAssociation( |
||
617 | ProductAssociationTypeInterface $productAssociationType, |
||
618 | ...$productsNames |
||
619 | ) { |
||
620 | $currentPage = $this->resolveCurrentPage(); |
||
621 | |||
622 | $currentPage->associateProducts($productAssociationType, $productsNames); |
||
624 | |||
625 | /** |
||
626 | * @When I remove an associated product :productName from :productAssociationType |
||
627 | */ |
||
628 | public function iRemoveAnAssociatedProductFromProductAssociation( |
||
636 | |||
637 | /** |
||
638 | * @Then /^(?:this product|the product "[^"]+"|it) should(?:| also) have an image with "([^"]*)" type$/ |
||
639 | */ |
||
640 | public function thisProductShouldHaveAnImageWithType($type) |
||
646 | |||
647 | /** |
||
648 | * @Then /^(?:this product|it)(?:| also) should not have any images with "([^"]*)" type$/ |
||
649 | */ |
||
650 | public function thisProductShouldNotHaveAnyImagesWithType($code) |
||
656 | |||
657 | /** |
||
658 | * @When I change the image with the :type type to :path |
||
659 | */ |
||
660 | public function iChangeItsImageToPathForTheType($type, $path) |
||
666 | |||
667 | /** |
||
668 | * @When /^I(?:| also) remove an image with "([^"]*)" type$/ |
||
669 | */ |
||
670 | public function iRemoveAnImageWithType($code) |
||
676 | |||
677 | /** |
||
678 | * @When I remove the first image |
||
679 | */ |
||
680 | public function iRemoveTheFirstImage() |
||
686 | |||
687 | /** |
||
688 | * @When I change the first image type to :type |
||
689 | */ |
||
690 | public function iChangeTheFirstImageTypeTo($type) |
||
696 | |||
697 | /** |
||
698 | * @Then /^(this product) should not have any images$/ |
||
699 | */ |
||
700 | public function thisProductShouldNotHaveImages(ProductInterface $product) |
||
708 | |||
709 | /** |
||
710 | * @Then /^(this product) should(?:| still) have (?:only one|(\d+)) images?$/ |
||
711 | */ |
||
712 | public function thereShouldStillBeOnlyOneImageInThisProduct(ProductInterface $product, $count = 1) |
||
720 | |||
721 | /** |
||
722 | * @Then /^there should be no reviews of (this product)$/ |
||
723 | */ |
||
724 | public function thereAreNoProductReviews(ProductInterface $product) |
||
730 | |||
731 | /** |
||
732 | * @Then this product should( also) have an association :productAssociationType with product :productName |
||
733 | * @Then this product should( also) have an association :productAssociationType with products :firstProductName and :secondProductName |
||
734 | */ |
||
735 | public function theProductShouldHaveAnAssociationWithProducts( |
||
750 | |||
751 | /** |
||
752 | * @Then this product should not have an association :productAssociationType with product :productName |
||
753 | */ |
||
754 | public function theProductShouldNotHaveAnAssociationWithProduct( |
||
760 | |||
761 | /** |
||
762 | * @Then I should be notified that simple product code has to be unique |
||
763 | */ |
||
764 | public function iShouldBeNotifiedThatSimpleProductCodeHasToBeUnique() |
||
768 | |||
769 | /** |
||
770 | * @Then I should be notified that slug has to be unique |
||
771 | */ |
||
772 | public function iShouldBeNotifiedThatSlugHasToBeUnique() |
||
776 | |||
777 | /** |
||
778 | * @Then I should be notified that code has to be unique |
||
779 | */ |
||
780 | public function iShouldBeNotifiedThatCodeHasToBeUnique() |
||
784 | |||
785 | /** |
||
786 | * @Then I should be notified that price must be defined for every channel |
||
787 | */ |
||
788 | public function iShouldBeNotifiedThatPriceMustBeDefinedForEveryChannel() |
||
792 | |||
793 | /** |
||
794 | * @Then they should have order like :firstProductName, :secondProductName and :thirdProductName |
||
795 | */ |
||
796 | public function theyShouldHaveOrderLikeAnd(...$productNames) |
||
800 | |||
801 | /** |
||
802 | * @When I save my new configuration |
||
803 | */ |
||
804 | public function iSaveMyNewConfiguration() |
||
808 | |||
809 | /** |
||
810 | * @When I set the position of :productName to :position |
||
811 | */ |
||
812 | public function iSetThePositionOfTo($productName, $position) |
||
816 | |||
817 | /** |
||
818 | * @Then this product should( still) have slug :value in :language |
||
819 | */ |
||
820 | public function thisProductElementShouldHaveSlugIn($slug, $language) |
||
824 | |||
825 | /** |
||
826 | * @When I set its shipping category as :shippingCategoryName |
||
827 | */ |
||
828 | public function iSetItsShippingCategoryAs($shippingCategoryName) |
||
832 | |||
833 | /** |
||
834 | * @Then /^(it|this product) should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
835 | * @Then /^(product "[^"]+") should be priced at (?:€|£|\$)([^"]+) for channel "([^"]+)"$/ |
||
836 | */ |
||
837 | public function itShouldBePricedAtForChannel(ProductInterface $product, $price, $channelName) |
||
843 | |||
844 | /** |
||
845 | * @Then /^(this product) should no longer have price for channel "([^"]+)"$/ |
||
846 | */ |
||
847 | public function thisProductShouldNoLongerHavePriceForChannel(ProductInterface $product, $channelName) |
||
861 | |||
862 | /** |
||
863 | * @Then I should be notified that I have to define product variants' prices for newly assigned channels first |
||
864 | */ |
||
865 | public function iShouldBeNotifiedThatIHaveToDefineProductVariantsPricesForNewlyAssignedChannelsFirst() |
||
872 | |||
873 | /** |
||
874 | * @param string $element |
||
875 | * @param string $value |
||
876 | */ |
||
877 | private function assertElementValue($element, $value) |
||
889 | |||
890 | /** |
||
891 | * @param string $element |
||
892 | * @param string $message |
||
893 | */ |
||
894 | private function assertValidationMessage($element, $message) |
||
901 | |||
902 | /** |
||
903 | * @return SymfonyPageInterface|IndexPageInterface|IndexPerTaxonPageInterface|CreateSimpleProductPageInterface|CreateConfigurableProductPageInterface|UpdateSimpleProductPageInterface|UpdateConfigurableProductPageInterface |
||
904 | */ |
||
905 | private function resolveCurrentPage() |
||
916 | } |
||
917 |