<?php

namespace ReCaptcha\Form;

use ReCaptcha\Event\ReCaptchaCheckEvent;
use ReCaptcha\Event\ReCaptchaEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Thelia\Form\BaseForm;
use Thelia\Form\Exception\FormValidationException;

class MyTheliaFormValidator extends \Thelia\Core\Form\TheliaFormValidator
{
    /** @var EventDispatcherInterface  */
    protected $dispatcher;

    public function __construct(TranslatorInterface $translator, $environment, EventDispatcherInterface $dispatcher)
    {
        $this->dispatcher = $dispatcher;

        parent::__construct($translator, $environment);
    }

    public function validateForm(BaseForm $aBaseForm, $expectedMethod = null)
    {
        if ($aBaseForm->getRequest()->get('captcha')) {
            $checkCaptchaEvent = new ReCaptchaCheckEvent();
            $this->dispatcher->dispatch(ReCaptchaEvents::CHECK_CAPTCHA_EVENT, $checkCaptchaEvent);
            if ($checkCaptchaEvent->isHuman() == false) {
                throw new FormValidationException('Veuillez confirmer que vous n\'êtes pas un robot.');
            }
        }

        return parent::validateForm($aBaseForm, $expectedMethod); // TODO: Change the autogenerated stub
    }

}