src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\EventListener;
  12. use Sylius\Component\Core\Model\TaxonInterface;
  13. use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface;
  14. use Symfony\Component\EventDispatcher\GenericEvent;
  15. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Webmozart\Assert\Assert;
  18. final class TaxonDeletionListener
  19. {
  20.     /** @var SessionInterface */
  21.     private $session;
  22.     /** @var TaxonAwareRuleUpdaterInterface[] */
  23.     private $ruleUpdaters;
  24.     public function __construct(
  25.         SessionInterface $session,
  26.         TaxonAwareRuleUpdaterInterface ...$ruleUpdaters
  27.     ) {
  28.         $this->session $session;
  29.         $this->ruleUpdaters $ruleUpdaters;
  30.     }
  31.     public function removeTaxonFromPromotionRules(GenericEvent $event): void
  32.     {
  33.         $taxon $event->getSubject();
  34.         Assert::isInstanceOf($taxonTaxonInterface::class);
  35.         $updatedPromotionCodes = [];
  36.         foreach ($this->ruleUpdaters as $ruleUpdater) {
  37.             $updatedPromotionCodes array_merge($updatedPromotionCodes$ruleUpdater->updateAfterDeletingTaxon($taxon));
  38.         }
  39.         if (!empty($updatedPromotionCodes)) {
  40.             /** @var FlashBagInterface $flashes */
  41.             $flashes $this->session->getBag('flashes');
  42.             $flashes->add('info', [
  43.                 'message' => 'sylius.promotion.update_rules',
  44.                 'parameters' => ['%codes%' => implode(', 'array_unique($updatedPromotionCodes))],
  45.             ]);
  46.         }
  47.     }
  48. }