src/Core/Form/EventSubscriber/ReCaptchaValidationSubscriber.php line 44
<?phpdeclare(strict_types=1);/*** Copyright (c) 2023 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2023 TECLA Consulting Group oü*/namespace App\Core\Form\EventSubscriber;use ReCaptcha\ReCaptcha;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\Form\FormError;use Symfony\Component\Form\FormEvent;use Symfony\Component\Form\FormEvents;use Symfony\Component\HttpFoundation\Request;final class ReCaptchaValidationSubscriber implements EventSubscriberInterface{private ReCaptcha $reCaptcha;public function __construct(ReCaptcha $reCaptcha){$this->reCaptcha = $reCaptcha;}public static function getSubscribedEvents(): array{return [FormEvents::POST_SUBMIT => 'onPostSubmit',];}public function onPostSubmit(FormEvent $event): void{$request = Request::createFromGlobals();$result = $this->reCaptcha->setExpectedHostname($request->getHost())->verify($request->request->get('g-recaptcha-response'), $request->getClientIp());if (!$result->isSuccess()) {$event->getForm()->addError(new FormError('The captcha is invalid. Please try again.'));}}}