vendor/bitbag/cms-plugin/src/EventListener/ResourceDeleteSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusCmsPlugin\EventListener;
  9. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  10. use Sylius\Component\Resource\ResourceActions;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. final class ResourceDeleteSubscriber implements EventSubscriberInterface
  20. {
  21.     /** @var UrlGeneratorInterface */
  22.     private $router;
  23.     /** @var SessionInterface */
  24.     private $session;
  25.     public function __construct(UrlGeneratorInterface $routerSessionInterface $session)
  26.     {
  27.         $this->router $router;
  28.         $this->session $session;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::EXCEPTION => 'onResourceDelete',
  34.         ];
  35.     }
  36.     public function onResourceDelete(ExceptionEvent $event): void
  37.     {
  38.         $exception $event->getThrowable();
  39.         if (!$exception instanceof ForeignKeyConstraintViolationException) {
  40.             return;
  41.         }
  42.         if (!$event->isMasterRequest() || 'html' !== $event->getRequest()->getRequestFormat()) {
  43.             return;
  44.         }
  45.         $eventRequest $event->getRequest();
  46.         $requestAttributes $eventRequest->attributes;
  47.         $originalRoute $requestAttributes->get('_route');
  48.         if (!$this->isMethodDelete($eventRequest) ||
  49.             !$this->isProtectedRoute($originalRoute) ||
  50.             !$this->isAdminSection($requestAttributes->get('_sylius', []))
  51.         ) {
  52.             return;
  53.         }
  54.         $resourceName $this->getResourceNameFromRoute($originalRoute);
  55.         if (null === $requestAttributes->get('_controller')) {
  56.             return;
  57.         }
  58.         /** @var FlashBagInterface $flashBag */
  59.         $flashBag $this->session->getBag('flashes');
  60.         $flashBag->add('error', [
  61.             'message' => 'sylius.resource.delete_error',
  62.             'parameters' => ['%resource%' => $resourceName],
  63.         ]);
  64.         $referrer $eventRequest->headers->get('referer');
  65.         if (null !== $referrer) {
  66.             $event->setResponse(new RedirectResponse($referrer));
  67.             return;
  68.         }
  69.         $event->setResponse($this->createRedirectResponse($originalRouteResourceActions::INDEX));
  70.     }
  71.     private function getResourceNameFromRoute(string $route): string
  72.     {
  73.         $route str_replace('_bulk'''$route);
  74.         $routeArray explode('_'$route);
  75.         $routeArrayWithoutAction array_slice($routeArray0count($routeArray) - 1);
  76.         $routeArrayWithoutPrefixes array_slice($routeArrayWithoutAction2);
  77.         return trim(implode(' '$routeArrayWithoutPrefixes));
  78.     }
  79.     private function createRedirectResponse(string $originalRoutestring $targetAction): RedirectResponse
  80.     {
  81.         $redirectRoute str_replace(ResourceActions::DELETE$targetAction$originalRoute);
  82.         return new RedirectResponse($this->router->generate($redirectRoute));
  83.     }
  84.     private function isMethodDelete(Request $request): bool
  85.     {
  86.         return Request::METHOD_DELETE === $request->getMethod();
  87.     }
  88.     private function isProtectedRoute(string $route): bool
  89.     {
  90.         return === strpos($route'bitbag');
  91.     }
  92.     private function isAdminSection(array $syliusParameters): bool
  93.     {
  94.         return array_key_exists('section'$syliusParameters) && 'admin' === $syliusParameters['section'];
  95.     }
  96. }