src/EventListener/KernelListener.php line 44

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 Doctrine\DBAL\Exception\ConnectionException;
  13. use EADPlataforma\Services\GeneralService;
  14. use EADPlataforma\Http\EADResponse;
  15. use EADPlataforma\Error\ActionInvalidException;
  16. use EADPlataforma\Error\FieldException;
  17. use EADPlataforma\Error\EADException;
  18. class KernelListener
  19. {
  20.     /**
  21.      *@var UrlGeneratorInterface
  22.      */
  23.     protected $router;
  24.     /**
  25.      *@var GeneralService
  26.      */
  27.     protected $generalService;
  28.     /**
  29.      * @param UrlGeneratorInterface Router
  30.      * @param GeneralService generalService
  31.      */
  32.     public function __construct(UrlGeneratorInterface $router
  33.                                 GeneralService $generalService)
  34.     {
  35.         $this->router $router;
  36.         $this->generalService $generalService;
  37.     }
  38.     public function onKernelRequest(RequestEvent $event)
  39.     {   
  40.         //$databaseManagerService = $this->generalService->getService('DatabaseManagerService');
  41.         //$databaseManagerService->executeMigrations();
  42.     }
  43.     public function onKernelException(ExceptionEvent $event)
  44.     {
  45.         $exception $event->getThrowable();
  46.         if($exception instanceof EADException){
  47.             $eadResponse = new EADResponse(
  48.                 $exception->getData(),
  49.                 $exception->getCode(),
  50.                 $exception->getHeaders()
  51.             );
  52.             $event->setResponse($eadResponse->getResponse());
  53.         }else if(
  54.             $exception instanceof NotFoundHttpException || 
  55.             $exception instanceof MethodNotAllowedHttpException
  56.         ) {
  57.             $route 'notFound';
  58.             if ($route === $event->getRequest()->get('_route')) {
  59.                 return;
  60.             }
  61.             $url $this->router->generate($route);
  62.             $response = new RedirectResponse($url);
  63.             $event->setResponse($response);
  64.         }else if (
  65.             $exception instanceof TableNotFoundException ||
  66.             $exception instanceof InvalidFieldNameException
  67.         ){
  68.             $databaseManagerService $this->generalService->getService(
  69.                 'DatabaseManagerService'
  70.             );
  71.             $databaseManagerService->executeMigrations();
  72.             sleep(2);
  73.             $route 'notFound';
  74.             if ($route === $event->getRequest()->get('_route')) {
  75.                 return;
  76.             }
  77.             $url $this->router->generate($route);
  78.             $response = new RedirectResponse($url);
  79.             $event->setResponse($response);
  80.         }else if($exception instanceof ConnectionException){
  81.             print_r('Estamos em manutenção, retornaremos dentro de instantes.');
  82.             exit;
  83.         }
  84.     }