<commit_before><?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Form\Handler;

use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormError;
use Biopen\GeoDirectoryBundle\Document\Coordinates;

class RegistrationFormHandler
{
    protected $request;
    protected $userManager;
    protected $form;
    protected $mailer;
    protected $tokenGenerator;
    protected $geocoder;

    public function __construct(Request $request, UserManagerInterface $userManager, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, $geocoder)
    {
        $this->request = $request;
        $this->userManager = $userManager;
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->geocoder = $geocoder;
    }

    /**
     * @param boolean $confirmation
     */
    public function process($form, $confirmation = false)
    {
        $user = $this->createUser();

        $this->form = $form;
        $form->setData($user);

        if ('POST' === $this->request->getMethod()) {
            $form->handleRequest($this->request);
            $user = $form->getData();
            
            $usersSameEmail = $this->userManager->findUserByEmail($user->getEmail());
            $alreadyUsedEmail = $usersSameEmail === null ? false : count($usersSameEmail) > 1;

            $userSameName   = $this->userManager->findUserByUsername($user->getUsername());
            array $alreadyUsedUserName() = $userSameName === null ? false : count($userSameName);

            $locoationSetToReceiveNewsletter = $user->getNewsletterFrequency() > 0 && !$user->getLocation();

            $geocodeError = false;
            if ($user->getLocation()) {
                try
                {
                    $geocoded = $this->geocoder->using('google_maps')->geocode($user->getLocation())->first();
                    $user->setGeo(new Coordinates($geocoded->getLatitude(), $geocoded->getLongitude()));
                }
                catch (\Exception $error) { $geocodeError = true; } 
            }                

            if ($form->isValid() && !$alreadyUsedEmail && !$alreadyUsedUserName && !$locoationSetToReceiveNewsletter && !$geocodeError) <commit_msg>Fixs bug alreadyUsedUserName<commit_after><?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Form\Handler;

use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormError;
use Biopen\GeoDirectoryBundle\Document\Coordinates;

class RegistrationFormHandler
{
    protected $request;
    protected $userManager;
    protected $form;
    protected $mailer;
    protected $tokenGenerator;
    protected $geocoder;

    public function __construct(Request $request, UserManagerInterface $userManager, MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, $geocoder)
    {
        $this->request = $request;
        $this->userManager = $userManager;
        $this->mailer = $mailer;
        $this->tokenGenerator = $tokenGenerator;
        $this->geocoder = $geocoder;
    }

    /**
     * @param boolean $confirmation
     */
    public function process($form, $confirmation = false)
    {
        $user = $this->createUser();

        $this->form = $form;
        $form->setData($user);

        if ('POST' === $this->request->getMethod()) {
            $form->handleRequest($this->request);
            $user = $form->getData();
            
            $usersSameEmail = $this->userManager->findUserByEmail($user->getEmail());
            $alreadyUsedEmail = $usersSameEmail === null ? false : count($usersSameEmail) > 1;

            $userSameName   = $this->userManager->findUserByUsername($user->getUsername());
            $alreadyUsedUserName = $userSameName === null ? false : count($userSameName);

            $locoationSetToReceiveNewsletter = $user->getNewsletterFrequency() > 0 && !$user->getLocation();

            $geocodeError = false;
            if ($user->getLocation()) {
                try
                {
                    $geocoded = $this->geocoder->using('google_maps')->geocode($user->getLocation())->first();
                    $user->setGeo(new Coordinates($geocoded->getLatitude(), $geocoded->getLongitude()));
                }
                catch (\Exception $error) { $geocodeError = true; } 
            }                

            if ($form->isValid() && !$alreadyUsedEmail && !$alreadyUsedUserName && !$locoationSetToReceiveNewsletter && !$geocodeError) 
{'fraction_non_alphanumeric': 0.1304911955514365, 'fraction_numerical': 0.0007414272474513438, 'mean_word_length': 2.932944606413994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 71, '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': '6989647', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1485, 'n_words': 289}