src/Sylius/Bundle/AddressingBundle/EventListener/ZoneMemberIntegrityListener.php line 35

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\AddressingBundle\EventListener;
  12. use Sylius\Component\Addressing\Checker\CountryProvincesDeletionCheckerInterface;
  13. use Sylius\Component\Addressing\Checker\ZoneDeletionCheckerInterface;
  14. use Sylius\Component\Addressing\Model\CountryInterface;
  15. use Sylius\Component\Addressing\Model\ZoneInterface;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Webmozart\Assert\Assert;
  21. final class ZoneMemberIntegrityListener
  22. {
  23.     public function __construct(
  24.         private RequestStack $requestStack,
  25.         private ZoneDeletionCheckerInterface $zoneDeletionChecker,
  26.         private CountryProvincesDeletionCheckerInterface $countryProvincesDeletionChecker,
  27.     ) {
  28.     }
  29.     public function protectFromRemovingZone(GenericEvent $event): void
  30.     {
  31.         $zone $event->getSubject();
  32.         Assert::isInstanceOf($zoneZoneInterface::class);
  33.         if (!$this->zoneDeletionChecker->isDeletable($zone)) {
  34.             /** @var FlashBagInterface $flashes */
  35.             $flashes $this->getSession()->getBag('flashes');
  36.             $flashes->add('error', [
  37.                 'message' => 'sylius.resource.delete_error',
  38.                 'parameters' => ['%resource%' => 'zone'],
  39.             ]);
  40.             $event->stopPropagation();
  41.         }
  42.     }
  43.     public function protectFromRemovingProvinceWithinCountry(GenericEvent $event): void
  44.     {
  45.         /** @var CountryInterface $country */
  46.         $country $event->getSubject();
  47.         Assert::isInstanceOf($countryCountryInterface::class);
  48.         if (!$this->countryProvincesDeletionChecker->isDeletable($country)) {
  49.             /** @var FlashBagInterface $flashes */
  50.             $flashes $this->getSession()->getBag('flashes');
  51.             $flashes->add('error', [
  52.                 'message' => 'sylius.resource.delete_error',
  53.                 'parameters' => ['%resource%' => 'province'],
  54.             ]);
  55.             $event->stopPropagation();
  56.         }
  57.     }
  58.     private function getSession(): SessionInterface
  59.     {
  60.         // bc-layer for Symfony 4
  61.         if (!method_exists(RequestStack::class, 'getSession')) {
  62.             /** @phpstan-ignore-next-line */
  63.             return $this->requestStack->getMasterRequest()->getSession();
  64.         }
  65.         return $this->requestStack->getSession();
  66.     }
  67. }