src/ApiBundle/Exceptions/OAuth2ServerExceptionListener.php line 26

Open in your IDE?
  1. <?php
  2. namespace ApiBundle\Exceptions;
  3. use OAuth2\OAuth2ServerException;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  7. /**
  8.  * Class OAuth2ServerExceptionListener
  9.  * @package ApiBundle\Services
  10.  */
  11. class OAuth2ServerExceptionListener
  12. {
  13.     private $container;
  14.     public function __construct(ContainerInterface $container)
  15.     {
  16.         $this->container $container;
  17.     }
  18.     /**
  19.      * @param GetResponseForExceptionEvent $event
  20.      */
  21.     public function onKernelException(GetResponseForExceptionEvent $event)
  22.     {
  23.         $exception $event->getException();
  24.         if ($exception instanceof OAuth2ServerException) {
  25.             $translator $this->container->get('translator');
  26.             $data = [
  27.                 'message' => $translator->trans($exception->getMessage(), [], 'api_translations'),
  28.             ];
  29.             $response = new JsonResponse($data$exception->getHttpCode());
  30.             $event->setResponse($response);
  31.         }
  32.     }
  33. }