53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Altcha;
|
||
|
|
||
|
use Symfony\Component\Form\FormError;
|
||
|
use Symfony\Component\Form\FormEvent;
|
||
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||
|
|
||
|
class AltchaValidator
|
||
|
{
|
||
|
public function __construct(
|
||
|
private readonly string $altchaHost,
|
||
|
private readonly string $altchaBaseUrl,
|
||
|
private readonly HttpClientInterface $httpClient,
|
||
|
private readonly TranslatorInterface $translator
|
||
|
) {
|
||
|
}
|
||
|
|
||
|
public function validate(FormEvent $formEvent): void
|
||
|
{
|
||
|
$form = $formEvent->getForm();
|
||
|
$data = $form->getData();
|
||
|
|
||
|
$response = $this->httpClient->request(
|
||
|
'POST',
|
||
|
$this->altchaHost.$this->altchaBaseUrl.'/verify',
|
||
|
[
|
||
|
'body' => json_encode($data),
|
||
|
'headers' => [
|
||
|
'Content-Type' => 'application/json',
|
||
|
],
|
||
|
],
|
||
|
);
|
||
|
|
||
|
if (Response::HTTP_OK !== $response->getStatusCode()) {
|
||
|
$form->addError(new FormError($this->translator->trans('altcha.validator.server_validation_error', [], 'form')));
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
$content = $response->getContent();
|
||
|
$parsedResponse = json_decode($content);
|
||
|
|
||
|
if (true !== $parsedResponse->success) {
|
||
|
$form->addError(new FormError($this->translator->trans('altcha.validator.server_validation_error', [], 'form')));
|
||
|
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|