src/EventListener/ModelManagerExceptionResponseListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Gedmo\Exception\ReferenceIntegrityStrictException;
  5. use Sonata\AdminBundle\Exception\ModelManagerException;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Translation\Translator;
  11. class ModelManagerExceptionResponseListener
  12. {
  13.     private $session;
  14.     private $router;
  15.     private $em;
  16.     private $translator;
  17.     public function __construct(SessionInterface $sessionUrlGeneratorInterface $router,
  18.         EntityManagerInterface $emTranslator $translator)
  19.     {
  20.         $this->session $session;
  21.         $this->router $router;
  22.         $this->em $em;
  23.         $this->translator $translator;
  24.     }
  25.     public function onKernelException(ExceptionEvent $event)
  26.     {
  27.         // get the exception
  28.         $exception =  $event->getThrowable();
  29.         // we proceed only if it is ModelManagerException
  30.         if (!$exception instanceof ModelManagerException && !$exception instanceof ReferenceIntegrityStrictException) {
  31.             return;
  32.         }
  33.         // get the route and id
  34.         // if it wasn't a delete route we don't want to proceed
  35.         $request $event->getRequest();
  36.         $route $request->get('_route');
  37.         // Si tiene un childId entonces nos quedamos con ese id como vĂ¡lido.
  38.         $id $request->get('id');
  39.         $childId $request->get('childId');
  40.         if ($childId)
  41.         {
  42.             $id $childId;
  43.         }
  44.         if (substr($route, -6) !== 'delete') {
  45.             return;
  46.         }
  47.         //$route = str_replace('delete', 'edit', $route);
  48.         // get the message
  49.         // we proceed only if it is the desired message
  50.         $message $exception->getMessage();
  51.         $failure 'Failed to delete object: ';
  52.         if (strpos($message$failure) < 0) {
  53.             return;
  54.         }
  55.         // get the object that can't be deleted
  56.         $entity str_replace($failure''$message);
  57.         $repository $this->em->getRepository($entity);
  58.         $object $repository->findOneById($id);
  59.         $this->session->getFlashBag()
  60.             ->add(
  61.                 'sonata_flash_error',
  62.                 $this->translator->trans('validation.modelmanagerexception', ['%object%' => $object], 'validators')
  63.             )
  64.         ;
  65.         // redirect to the edit form of the object
  66.         $url $this->router->generate($route, ['id' => $id'childId' => $childId]);
  67.         $response = new RedirectResponse($url);
  68.         $event->setResponse($response);
  69.     }
  70. }