From 1e82d3cc4177cf51aa3126c7d902cc3c9e1359af Mon Sep 17 00:00:00 2001 From: Grzegorz Sadowski Date: Fri, 10 May 2019 16:03:43 +0200 Subject: [PATCH] Add variadics to taxon deletion listener --- .../EventListener/TaxonDeletionListener.php | 27 +++++++++---------- .../Resources/config/services/listeners.xml | 2 +- .../TaxonDeletionListenerSpec.php | 10 +++---- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index f52d5ff59f8..8f0dcdcda03 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -22,23 +22,19 @@ final class TaxonDeletionListener { - /** @var TaxonAwareRuleUpdaterInterface */ - private $hasTaxonRuleUpdater; - - /** @var TaxonAwareRuleUpdaterInterface */ - private $totalOfItemsFromTaxonRuleUpdater; - /** @var SessionInterface */ private $session; + /** @var TaxonAwareRuleUpdaterInterface[] */ + private $ruleUpdaters; + public function __construct( - TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session + SessionInterface $session, + TaxonAwareRuleUpdaterInterface ...$ruleUpdaters + ) { - $this->hasTaxonRuleUpdater = $hasTaxonRuleUpdater; - $this->totalOfItemsFromTaxonRuleUpdater = $totalOfItemsFromTaxonRuleUpdater; $this->session = $session; + $this->ruleUpdaters = $ruleUpdaters; } public function removeTaxonFromPromotionRules(GenericEvent $event): void @@ -46,16 +42,17 @@ public function removeTaxonFromPromotionRules(GenericEvent $event): void $taxon = $event->getSubject(); Assert::isInstanceOf($taxon, TaxonInterface::class); - $firstUpdatedPromotionCodes = $this->hasTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); - $secondUpdatedPromotionCodes = $this->totalOfItemsFromTaxonRuleUpdater->updateAfterDeletingTaxon($taxon->getCode()); - $updatedPromotionCodes = array_unique(array_merge($firstUpdatedPromotionCodes, $secondUpdatedPromotionCodes)); + $updatedPromotionCodes = []; + foreach ($this->ruleUpdaters as $ruleUpdater) { + $updatedPromotionCodes = array_merge($updatedPromotionCodes, $ruleUpdater->updateAfterDeletingTaxon($taxon->getCode())); + } if (!empty($updatedPromotionCodes)) { /** @var FlashBagInterface $flashes */ $flashes = $this->session->getBag('flashes'); $flashes->add('info', [ 'message' => 'sylius.promotion.update_rules', - 'parameters' => ['%codes%' => implode(', ', $updatedPromotionCodes)], + 'parameters' => ['%codes%' => implode(', ', array_unique($updatedPromotionCodes))], ]); } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index a595409342b..aa3bbb2cfe5 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -88,9 +88,9 @@ + - diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index b0c6fdcb4dd..02d9a988374 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -23,17 +23,17 @@ final class TaxonDeletionListenerSpec extends ObjectBehavior { function let( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, - TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session + TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater ): void { - $this->beConstructedWith($hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater, $session); + $this->beConstructedWith($session, $hasTaxonRuleUpdater, $totalOfItemsFromTaxonRuleUpdater); } function it_adds_flash_that_promotions_have_been_updated( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session, FlashBagInterface $flashes, GenericEvent $event, TaxonInterface $taxon @@ -57,9 +57,9 @@ function it_adds_flash_that_promotions_have_been_updated( } function it_does_nothing_if_no_promotion_has_been_updated( + SessionInterface $session, TaxonAwareRuleUpdaterInterface $hasTaxonRuleUpdater, TaxonAwareRuleUpdaterInterface $totalOfItemsFromTaxonRuleUpdater, - SessionInterface $session, GenericEvent $event, TaxonInterface $taxon ): void {