<commit_before><?php
/**
 * This file is part of the login-cidadao project or it's bundles.
 *
 * (c) <NAME> <guilhermednt on github>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LoginCidadao\DynamicFormBundle\Service;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use LoginCidadao\DynamicFormBundle\DynamicFormEvents;
use LoginCidadao\CoreBundle\Entity\City;
use LoginCidadao\CoreBundle\Entity\Country;
use LoginCidadao\CoreBundle\Entity\PersonAddress;
use LoginCidadao\CoreBundle\Entity\State;
use LoginCidadao\CoreBundle\Entity\StateRepository;
use LoginCidadao\CoreBundle\Model\IdCardInterface;
use LoginCidadao\CoreBundle\Model\PersonInterface;
use LoginCidadao\CoreBundle\Model\LocationSelectData;
use LoginCidadao\DynamicFormBundle\Form\DynamicFormBuilder;
use LoginCidadao\DynamicFormBundle\Model\DynamicFormData;
use LoginCidadao\OAuthBundle\Model\ClientInterface;
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTask;
use LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

class DynamicFormService implements DynamicFormServiceInterface
{
    /** @var EntityManagerInterface */
    private $em;

    /** @var EventDispatcherInterface */
    private $dispatcher;

    /** @var UserManagerInterface */
    private $userManager;

    /** @var TaskStackManagerInterface */
    private $taskStackManager;

    /** @var DynamicFormBuilder */
    private $dynamicFormBuilder;

    /** @var RouterInterface */
    private $router;

    /**
     * DynamicFormService constructor.
     * @param EntityManagerInterface $em
     * @param EventDispatcherInterface $dispatcher
     * @param UserManagerInterface $userManager
     * @param TaskStackManagerInterface $taskStackManager
     * @param DynamicFormBuilder $dynamicFormBuilder
     * @param RouterInterface $router
     */
    public function __construct(
        EntityManagerInterface $em,
        EventDispatcherInterface $dispatcher,
        UserManagerInterface $userManager,
        TaskStackManagerInterface $taskStackManager,
        DynamicFormBuilder $dynamicFormBuilder,
        RouterInterface $router
    ) {
        $this->em = $em;
        $this->dispatcher = $dispatcher;
        $this->userManager = $userManager;
        $this->taskStackManager = $taskStackManager;
        $this->dynamicFormBuilder = $dynamicFormBuilder;
        $this->router = $router;
    }

    public function getDynamicFormData(PersonInterface $person, Request $request, $scope)
    {
        $nextTask = $this->taskStackManager->getNextTask();
        $redirectUrl = $request->get('redirect_url');
        if ($nextTask) {
            $redirectUrl = $this->taskStackManager->getTargetUrl($nextTask->getTarget());
        }
        if (!$redirectUrl) {
            $redirectUrl = $this->router->generate('lc_dashboard');
        }

        $placeOfBirth = new LocationSelectData();
        $placeOfBirth->getFromObject($person);

        $data = new DynamicFormData();
        $data->setPerson($person)
            ->setRedirectUrl($redirectUrl)
            ->setScope($scope)
            ->setPlaceOfBirth($placeOfBirth)
            ->setIdCardState($this->getStateFromRequest($request));

        $this->dispatchProfileEditInitialize($request, $person);

        return $data;
    }

    public function buildForm(FormInterface $form, DynamicFormData $data, array $scopes)
    {
        foreach ($scopes as $scope) {
            $this->dynamicFormBuilder->addFieldFromScope($form, $scope, $data);
        }
        $form->add('redirect_url', 'hidden')
            ->add('scope', 'hidden');

        return $form;
    }

    public function processForm(FormInterface $form, Request $request)
    {
        $dynamicFormResponse = [
            'response' => null,
            'form' => $form,
        ];
        $form->handleRequest($request);
        if (!$form->isValid()) {
            return $dynamicFormResponse;
        }

        $this->dispatchFormEvent($form, $request, DynamicFormEvents::POST_FORM_VALIDATION);
        $event = null;
        if ($form->has('person')) {
            $event = $this->dispatchFormEvent($form->get('person'), $request, FOSUserEvents::PROFILE_EDIT_SUCCESS);
        }

        /** @var DynamicFormData $data */
        $data = $form->getData();
        $person = $data->getPerson();
        $address = $data->getAddress();
        $idCard = $data->getIdCard();
        $placeOfBirth = $data->getPlaceOfBirth();<commit_msg>fixed dynamic form service PROCERGS#808<commit_after><?php
/**
 * This file is part of the login-cidadao project or it's bundles.
 *
 * (c) <NAME> <guilhermednt on github>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace LoginCidadao\DynamicFormBundle\Service;

use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
use LoginCidadao\DynamicFormBundle\DynamicFormEvents;
use LoginCidadao\CoreBundle\Entity\City;
use LoginCidadao\CoreBundle\Entity\Country;
use LoginCidadao\CoreBundle\Entity\PersonAddress;
use LoginCidadao\CoreBundle\Entity\State;
use LoginCidadao\CoreBundle\Entity\StateRepository;
use LoginCidadao\CoreBundle\Model\IdCardInterface;
use LoginCidadao\CoreBundle\Model\PersonInterface;
use LoginCidadao\CoreBundle\Model\LocationSelectData;
use LoginCidadao\DynamicFormBundle\Form\DynamicFormBuilder;
use LoginCidadao\DynamicFormBundle\Model\DynamicFormData;
use LoginCidadao\OAuthBundle\Model\ClientInterface;
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTask;
use LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;

class DynamicFormService implements DynamicFormServiceInterface
{
    /** @var EntityManagerInterface */
    private $em;

    /** @var EventDispatcherInterface */
    private $dispatcher;

    /** @var UserManagerInterface */
    private $userManager;

    /** @var TaskStackManagerInterface */
    private $taskStackManager;

    /** @var DynamicFormBuilder */
    private $dynamicFormBuilder;

    /** @var RouterInterface */
    private $router;

    /**
     * DynamicFormService constructor.
     * @param EntityManagerInterface $em
     * @param EventDispatcherInterface $dispatcher
     * @param UserManagerInterface $userManager
     * @param TaskStackManagerInterface $taskStackManager
     * @param DynamicFormBuilder $dynamicFormBuilder
     * @param RouterInterface $router
     */
    public function __construct(
        EntityManagerInterface $em,
        EventDispatcherInterface $dispatcher,
        UserManagerInterface $userManager,
        TaskStackManagerInterface $taskStackManager,
        DynamicFormBuilder $dynamicFormBuilder,
        RouterInterface $router
    ) {
        $this->em = $em;
        $this->dispatcher = $dispatcher;
        $this->userManager = $userManager;
        $this->taskStackManager = $taskStackManager;
        $this->dynamicFormBuilder = $dynamicFormBuilder;
        $this->router = $router;
    }

    public function getDynamicFormData(PersonInterface $person, Request $request, $scope)
    {
        $nextTask = $this->taskStackManager->getNextTask();
        $redirectUrl = $request->get('redirect_url');
        if ($nextTask) {
            $redirectUrl = $this->taskStackManager->getTargetUrl($nextTask->getTarget());
        }
        if (!$redirectUrl) {
            $redirectUrl = $this->router->generate('lc_dashboard');
        }

        $placeOfBirth = new LocationSelectData();
        $placeOfBirth->getFromObject($person);

        $data = new DynamicFormData();
        $data->setPerson($person)
            ->setRedirectUrl($redirectUrl)
            ->setScope($scope)
            ->setPlaceOfBirth($placeOfBirth)
            ->setIdCardState($this->getStateFromRequest($request));

        $this->dispatchProfileEditInitialize($request, $person);

        return $data;
    }

    public function buildForm(FormInterface $form, DynamicFormData $data, array $scopes)
    {
        foreach ($scopes as $scope) {
            $this->dynamicFormBuilder->addFieldFromScope($form, $scope, $data);
        }
        $form->add('redirect_url', HiddenType::class)
            ->add('scope', HiddenType::class);

        return $form;
    }

    public function processForm(FormInterface $form, Request $request)
    {
        $dynamicFormResponse = [
            'response' => null,
            'form' => $form,
        ];
        $form->handleRequest($request);
        if (!$form->isValid()) {
            return $dynamicFormResponse;
        }

        $this->dispatchFormEvent($form, $request, DynamicFormEvents::POST_FORM_VALIDATION);
        $event = null;
        if ($form->has('person')) {
            $event = $this->dispatchFormEvent($form->get('person'), $request, FOSUserEvents::PROFILE_EDIT_SUCCESS);
        }

        /** @var DynamicFormData $data */
        $data = $form->getData();
        $person = $data->getPerson();
        $address = $data->getAddress();
        $idCard = $data->getIdCard();
        $placeOfBirth = $data->getPlaceOfBirth();
{'fraction_non_alphanumeric': 0.11179039301310044, 'fraction_numerical': 0.0002911208151382824, 'mean_word_length': 3.550110375275938, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4264103', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2834, 'n_words': 531}