src/Sylius/Bundle/AdminBundle/EventListener/ResourceDeleteSubscriber.php line 47

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\AdminBundle\EventListener;
  12. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  13. use Sylius\Bundle\CoreBundle\Provider\FlashBagProvider;
  14. use Sylius\Component\Resource\ResourceActions;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. final class ResourceDeleteSubscriber implements EventSubscriberInterface
  25. {
  26.     public function __construct(
  27.         private UrlGeneratorInterface $router,
  28.         private SessionInterface|RequestStack $requestStackOrSession,
  29.     ) {
  30.         if ($this->requestStackOrSession instanceof SessionInterface) {
  31.             trigger_deprecation('sylius/admin-bundle''1.12'sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of Sylius 1.12 and will be removed in 2.0. Pass an instance of %s instead.'SessionInterface::class, self::class, RequestStack::class));
  32.         }
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             KernelEvents::EXCEPTION => 'onResourceDelete',
  38.         ];
  39.     }
  40.     public function onResourceDelete(ExceptionEvent $event): void
  41.     {
  42.         $exception $event->getThrowable();
  43.         if (!$exception instanceof ForeignKeyConstraintViolationException) {
  44.             return;
  45.         }
  46.         if (\method_exists($event'isMainRequest')) {
  47.             $isMainRequest $event->isMainRequest();
  48.         } else {
  49.             /** @phpstan-ignore-next-line */
  50.             $isMainRequest $event->isMasterRequest();
  51.         }
  52.         if (!$isMainRequest || 'html' !== $event->getRequest()->getRequestFormat()) {
  53.             return;
  54.         }
  55.         $eventRequest $event->getRequest();
  56.         $requestAttributes $eventRequest->attributes;
  57.         $originalRoute $requestAttributes->get('_route');
  58.         if (!$this->isMethodDelete($eventRequest) ||
  59.             !$this->isSyliusRoute($originalRoute) ||
  60.             !$this->isAdminSection($requestAttributes->get('_sylius', []))
  61.         ) {
  62.             return;
  63.         }
  64.         $resourceName $this->getResourceNameFromRoute($originalRoute);
  65.         if (null === $requestAttributes->get('_controller')) {
  66.             return;
  67.         }
  68.         FlashBagProvider::getFlashBag($this->requestStackOrSession)->add('error', [
  69.             'message' => 'sylius.resource.delete_error',
  70.             'parameters' => ['%resource%' => $resourceName],
  71.         ]);
  72.         $referrer $eventRequest->headers->get('referer');
  73.         if (null !== $referrer) {
  74.             $event->setResponse(new RedirectResponse($referrer));
  75.             return;
  76.         }
  77.         $event->setResponse($this->createRedirectResponse($originalRouteResourceActions::INDEX));
  78.     }
  79.     private function getResourceNameFromRoute(string $route): string
  80.     {
  81.         $route str_replace('_bulk'''$route);
  82.         $routeArray explode('_'$route);
  83.         $routeArrayWithoutAction array_slice($routeArray0count($routeArray) - 1);
  84.         $routeArrayWithoutPrefixes array_slice($routeArrayWithoutAction2);
  85.         return trim(implode(' '$routeArrayWithoutPrefixes));
  86.     }
  87.     private function createRedirectResponse(string $originalRoutestring $targetAction): RedirectResponse
  88.     {
  89.         $redirectRoute str_replace(ResourceActions::DELETE$targetAction$originalRoute);
  90.         return new RedirectResponse($this->router->generate($redirectRoute));
  91.     }
  92.     private function isMethodDelete(Request $request): bool
  93.     {
  94.         return Request::METHOD_DELETE === $request->getMethod();
  95.     }
  96.     private function isSyliusRoute(string $route): bool
  97.     {
  98.         return str_starts_with($route'sylius');
  99.     }
  100.     private function isAdminSection(array $syliusParameters): bool
  101.     {
  102.         return array_key_exists('section'$syliusParameters) && 'admin' === $syliusParameters['section'];
  103.     }
  104. }