vendor/shopware/storefront/Framework/Captcha/CaptchaRouteListener.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Captcha;
  3. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  4. use Shopware\Storefront\Framework\Captcha\Annotation\Captcha as CaptchaAnnotation;
  5. use Shopware\Storefront\Framework\Captcha\Exception\CaptchaInvalidException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class CaptchaRouteListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var iterable|AbstractCaptcha[]
  13.      */
  14.     private $captchas;
  15.     public function __construct(iterable $captchas)
  16.     {
  17.         $this->captchas $captchas;
  18.     }
  19.     /**
  20.      * {@inheritdoc}
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             KernelEvents::CONTROLLER => [
  26.                 ['validateCaptcha'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  27.             ],
  28.         ];
  29.     }
  30.     public function validateCaptcha(ControllerEvent $event): void
  31.     {
  32.         /** @var CaptchaAnnotation|bool $captchaAnnotation */
  33.         $captchaAnnotation $event->getRequest()->attributes->get('_captcha'false);
  34.         if ($captchaAnnotation === false) {
  35.             return;
  36.         }
  37.         if (!($captchaAnnotation instanceof CaptchaAnnotation)) {
  38.             return;
  39.         }
  40.         foreach ($this->captchas as $captcha) {
  41.             if (
  42.                 $captcha->supports($event->getRequest()) && !$captcha->isValid($event->getRequest())
  43.             ) {
  44.                 throw new CaptchaInvalidException($captcha);
  45.             }
  46.         }
  47.     }
  48. }