Skip to content

Commit

Permalink
Add variadics to taxon deletion listener
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee committed May 10, 2019
1 parent 060e969 commit 1e82d3c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,37 @@

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
{
$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))],
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
</service>

<service id="sylius.listener.taxon_deletion" class="Sylius\Bundle\CoreBundle\EventListener\TaxonDeletionListener">
<argument type="service" id="session" />
<argument type="service" id="sylius.promotion_rule_updater.total_of_items_from_taxon" />
<argument type="service" id="sylius.promotion_rule_updater.has_taxon" />
<argument type="service" id="session" />
<tag name="kernel.event_listener" event="sylius.taxon.post_delete" method="removeTaxonFromPromotionRules" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 1e82d3c

Please sign in to comment.