vendor/sylius/sylius/src/Sylius/Bundle/AdminBundle/EventListener/ResourceDeleteSubscriber.php line 46

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