vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php line 43

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\Channel\Repository\ChannelRepositoryInterface;
  13. use Sylius\Component\Core\Model\TaxonInterface;
  14. use Sylius\Component\Core\Promotion\Updater\Rule\TaxonAwareRuleUpdaterInterface;
  15. use Symfony\Component\EventDispatcher\GenericEvent;
  16. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  17. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18. use Webmozart\Assert\Assert;
  19. final class TaxonDeletionListener
  20. {
  21.     private SessionInterface $session;
  22.     private ChannelRepositoryInterface $channelRepository;
  23.     /** @var TaxonAwareRuleUpdaterInterface[] */
  24.     private array $ruleUpdaters;
  25.     public function __construct(
  26.         SessionInterface $session,
  27.         ChannelRepositoryInterface $channelRepository,
  28.         TaxonAwareRuleUpdaterInterface ...$ruleUpdaters
  29.     ) {
  30.         $this->session $session;
  31.         $this->channelRepository $channelRepository;
  32.         $this->ruleUpdaters $ruleUpdaters;
  33.     }
  34.     public function protectFromRemovingMenuTaxon(GenericEvent $event): void
  35.     {
  36.         $taxon $event->getSubject();
  37.         Assert::isInstanceOf($taxonTaxonInterface::class);
  38.         $channel $this->channelRepository->findOneBy(['menuTaxon' => $taxon]);
  39.         if ($channel !== null) {
  40.             /** @var FlashBagInterface $flashes */
  41.             $flashes $this->session->getBag('flashes');
  42.             $flashes->add('error''sylius.taxon.menu_taxon_delete');
  43.             $event->stopPropagation();
  44.         }
  45.     }
  46.     public function removeTaxonFromPromotionRules(GenericEvent $event): void
  47.     {
  48.         $taxon $event->getSubject();
  49.         Assert::isInstanceOf($taxonTaxonInterface::class);
  50.         $updatedPromotionCodes = [];
  51.         foreach ($this->ruleUpdaters as $ruleUpdater) {
  52.             $updatedPromotionCodes array_merge($updatedPromotionCodes$ruleUpdater->updateAfterDeletingTaxon($taxon));
  53.         }
  54.         if (!empty($updatedPromotionCodes)) {
  55.             /** @var FlashBagInterface $flashes */
  56.             $flashes $this->session->getBag('flashes');
  57.             $flashes->add('info', [
  58.                 'message' => 'sylius.promotion.update_rules',
  59.                 'parameters' => ['%codes%' => implode(', 'array_unique($updatedPromotionCodes))],
  60.             ]);
  61.         }
  62.     }
  63. }