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

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.     /** @var TaxonAwareRuleUpdaterInterface[] */
  22.     private array $ruleUpdaters;
  23.     public function __construct(
  24.         private SessionInterface $session,
  25.         private ChannelRepositoryInterface $channelRepository,
  26.         TaxonAwareRuleUpdaterInterface ...$ruleUpdaters,
  27.     ) {
  28.         $this->ruleUpdaters $ruleUpdaters;
  29.     }
  30.     public function protectFromRemovingMenuTaxon(GenericEvent $event): void
  31.     {
  32.         $taxon $event->getSubject();
  33.         Assert::isInstanceOf($taxonTaxonInterface::class);
  34.         $channel $this->channelRepository->findOneBy(['menuTaxon' => $taxon]);
  35.         if ($channel !== null) {
  36.             /** @var FlashBagInterface $flashes */
  37.             $flashes $this->session->getBag('flashes');
  38.             $flashes->add('error''sylius.taxon.menu_taxon_delete');
  39.             $event->stopPropagation();
  40.         }
  41.     }
  42.     public function removeTaxonFromPromotionRules(GenericEvent $event): void
  43.     {
  44.         $taxon $event->getSubject();
  45.         Assert::isInstanceOf($taxonTaxonInterface::class);
  46.         $updatedPromotionCodes = [];
  47.         foreach ($this->ruleUpdaters as $ruleUpdater) {
  48.             $updatedPromotionCodes array_merge($updatedPromotionCodes$ruleUpdater->updateAfterDeletingTaxon($taxon));
  49.         }
  50.         if (!empty($updatedPromotionCodes)) {
  51.             /** @var FlashBagInterface $flashes */
  52.             $flashes $this->session->getBag('flashes');
  53.             $flashes->add('info', [
  54.                 'message' => 'sylius.promotion.update_rules',
  55.                 'parameters' => ['%codes%' => implode(', 'array_unique($updatedPromotionCodes))],
  56.             ]);
  57.         }
  58.     }
  59.     public function handleRemovingRootTaxonAtPositionZero(GenericEvent $event): void
  60.     {
  61.         /** @var TaxonInterface $taxon */
  62.         $taxon $event->getSubject();
  63.         Assert::isInstanceOf($taxonTaxonInterface::class);
  64.         if ($taxon->getPosition() === 0) {
  65.             $taxon->setPosition(-1);
  66.         }
  67.     }
  68. }