src/EventListener/KernelListener.php line 49

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Doctrine\DBAL\Exception\TableNotFoundException;
  11. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  12. use EADPlataforma\Services\GeneralService;
  13. use EADPlataforma\Http\EADResponse;
  14. use EADPlataforma\Error\ActionInvalidException;
  15. use EADPlataforma\Error\FieldException;
  16. use EADPlataforma\Error\EADException;
  17. class KernelListener
  18. {
  19.     /**
  20.      *@var UrlGeneratorInterface
  21.      */
  22.     protected $router;
  23.     /**
  24.      *@var GeneralService
  25.      */
  26.     protected $generalService;
  27.     /**
  28.      * @param UrlGeneratorInterface Router
  29.      * @param GeneralService generalService
  30.      */
  31.     public function __construct(UrlGeneratorInterface $router
  32.                                 GeneralService $generalService)
  33.     {
  34.         $this->router $router;
  35.         $this->generalService $generalService;
  36.     }
  37.     public function onKernelRequest(RequestEvent $event)
  38.     {   
  39.         //$databaseManagerService = $this->generalService->getService('DatabaseManagerService');
  40.         //$databaseManagerService->executeMigrations();
  41.     }
  42.     public function onKernelException(ExceptionEvent $event)
  43.     {
  44.         $exception $event->getThrowable();
  45.         if($exception instanceof EADException){
  46.             $eadResponse = new EADResponse(
  47.                 $exception->getData(),
  48.                 $exception->getCode(),
  49.                 $exception->getHeaders()
  50.             );
  51.             $event->setResponse($eadResponse->getResponse());
  52.         }else if(
  53.             $exception instanceof NotFoundHttpException || 
  54.             $exception instanceof MethodNotAllowedHttpException
  55.         ) {
  56.             $route 'notFound';
  57.             if ($route === $event->getRequest()->get('_route')) {
  58.                 return;
  59.             }
  60.             $url $this->router->generate($route);
  61.             $response = new RedirectResponse($url);
  62.             $event->setResponse($response);
  63.         }else if (
  64.             $exception instanceof TableNotFoundException ||
  65.             $exception instanceof InvalidFieldNameException
  66.         ){
  67.             $databaseManagerService $this->generalService->getService(
  68.                 'DatabaseManagerService'
  69.             );
  70.             $databaseManagerService->executeMigrations();
  71.             sleep(2);
  72.             $route 'notFound';
  73.             if ($route === $event->getRequest()->get('_route')) {
  74.                 return;
  75.             }
  76.             $url $this->router->generate($route);
  77.             $response = new RedirectResponse($url);
  78.             $event->setResponse($response);
  79.         }
  80.     }