custom/plugins/GbmedForm/src/Storefront/Framework/Captcha/CaptchaExecptionListener.php line 66

Open in your IDE?
  1. <?php
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedForm
  12.  * @copyright      Copyright (c) 2020, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. declare(strict_types=1);
  18. namespace Gbmed\Form\Storefront\Framework\Captcha;
  19. use Gbmed\Form\Framework\Exception\CaptchaInvalidException;
  20. use Gbmed\Form\Framework\Captcha\FormRoutes\Collection;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  25. use Symfony\Component\HttpKernel\KernelEvents;
  26. use Twig\Environment;
  27. class CaptchaExecptionListener implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var Collection
  31.      */
  32.     private $formRoutes;
  33.     /**
  34.      * CaptchaRouteListener constructor.
  35.      * @param Collection $formRoutes
  36.      * @param Environment $twig
  37.      */
  38.     public function __construct(Collection $formRoutes)
  39.     {
  40.         $this->formRoutes $formRoutes;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::EXCEPTION => [
  49.                 ['exceptionValidateCaptcha'10]
  50.             ],
  51.         ];
  52.     }
  53.     /**
  54.      * handle CaptchaInvalidException
  55.      *
  56.      * @param ExceptionEvent $event
  57.      * @throws \Exception
  58.      */
  59.     public function exceptionValidateCaptcha(ExceptionEvent $event): void
  60.     {
  61.         $exception $event->getThrowable();
  62.         if(($exception instanceof CaptchaInvalidException)){
  63.             try {
  64.                 $response $this->getResponse($event->getRequest(), $exception);
  65.                 if(($response instanceof Response)){
  66.                     $event->setResponse($response);
  67.                 }
  68.             } catch (\Exception $e) {
  69.                 throw $e;
  70.             }
  71.         }
  72.     }
  73.     /**
  74.      * @param Request $request
  75.      * @param CaptchaInvalidException $exception
  76.      * @return Response|null
  77.      */
  78.     private function getResponse(Request $requestCaptchaInvalidException $exception): ?Response
  79.     {
  80.         if($route $this->formRoutes->findRoute($request)){
  81.             return $route->response($exception);
  82.         }
  83.         return null;
  84.     }
  85. }