<commit_before><?php

/*
 * This file is part of the BenGorUser package.
 *
 * (c) <NAME> <<EMAIL>>
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace BenGorUser\UserBundle\Controller;

use BenGorUser\User\Domain\Model\UserGuest;
use BenGorUser\User\Domain\Model\UserToken;
use BenGorUser\UserBundle\Form\Type\SignUpByInvitationType;
use BenGorUser\UserBundle\Form\Type\SignUpType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Sign up user controller.
 *
 * @author <NAME> <<EMAIL>>
 */
class SignUpController extends Controller
{
    /**
     * Default action, that it can executes the "default"
     * or "with_confirmation" specifications.
     *
     * @param Request $request   The request
     * @param string  $userClass Extra parameter that contains the user type
     * @param string  $firewall  Extra parameter that contains the firewall name
     * @param string  $command   Extra parameter that contains the fully qualified class name of command
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function defaultAction(Request $request, $userClass, $firewall, $command)
    {
        $roles = $this->getParameter('bengor_user.' . $userClass . '_default_roles');
        $form = $this->createForm(SignUpType::class, null, ['roles' => $roles, 'command' => $command]);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                try {
                    $this->get('bengor_user.' . $userClass . '_command_bus')->handle($form->getData());
                    $this->addFlash('notice', $this->get('translator')->trans('sign_up.success_flash'));

                    return $this
                        ->get('security.authentication.guard_handler')
                        ->authenticateUserAndHandleSuccess(
                            $this->get('bengor_user.' . $userClass . '_provider')->loadUserByUsername(
                                $form->get('email')->getData()
                            ),
                            $request,
                            $this->get('bengor_user.form_login_' . $userClass . '_authenticator'),
                            $firewall
                        );
                } catch (\Exception $exception) {
                    $this->get('logger')->addError($exception->getMessage());
                    $this->addFlash('error', $this->get('translator')->trans('sign_up.error_flash_generic'));
                }
            }
        }

        return $this->render('@BenGorUser/sign_up/default.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    /**
     * By invitation action, that it can executes the "by_invitation"
     * or "by_invitation_with_confirmation" specifications.
     *
     * @param Request $request         The request
     * @param string  $invitationToken The invitation token
     * @param string  $userClass       Extra parameter that contains the user type
     * @param string  $firewall        Extra parameter that contains the firewall name
     * @param string  $command         Extra parameter that contains the fully qualified class name of command
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function byInvitationAction(Request $request, $invitationToken, $userClass, $firewall, $command)
    {
        $userGuest = $this->getUserGuestByToken($userClass, $invitationToken);
        $form = $this->createForm(SignUpByInvitationType::class, null, [
            'roles'            => $this->getParameter('bengor_user.' . $userClass . '_default_roles'),
            'invitation_token' => $invitationToken,
            'command'          => $command,
        ]);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                try {
                    $this->get('bengor_user.' . $userClass . '_command_bus')->handle($form->getData());
                    $this->addFlash('notice', $this->get('translator')->trans('sign_up.success_flash'));

//                    return $this
//                        ->get('security.authentication.guard_handler')
//                        ->authenticateUserAndHandleSuccess(
//                         TODO: execute UserOfIdHandler with the id that is generated by command inside form
//                            $request,
//                            $this->get('bengor_user.form_login_' . $userClass . '_authenticator'),
//                            $firewall
//                        );
                } catch (\Exception $exception) {
                    $this->get('logger')->addError($exception->getMessage());
                    $this->addFlash('error', $this->get('translator')->trans('sign_up.error_flash_generic'));
                }
            }
        }

        return $this->render('@BenGorUser/sign_up/by_invitation.html.twig', [
            'email' => $userGuest->email()->email(),
            'form'  => $form->createView(),
        ]);
    }

    /**
     * This extra query is a trade off related with the flow of application service.
     *
     * In "GET" requests we need to know if the invitation
     * token given exists in database, in case that
     * it isn't, it throws 404.
     *
     * @param string $userClass       The user type
     * @param string $invitationToken The invitation token
     *
     * @return UserGuest
     */
    private function getUserGuestByToken($userClass, $invitationToken)
    {
        $userGuest = $this->get('bengor_user.' . $userClass . '_guest_repository')->userGuestOfInvitationToken(
            new UserToken($invitationToken)
        );
        if (!$userGuest instanceof UserGuest) {
            throw $this->createNotFoundException();
        }

        return $userGuest;
    }<commit_msg>Fixed SignUp controller making compatible with queries<commit_after><?php

/*
 * This file is part of the BenGorUser package.
 *
 * (c) <NAME> <<EMAIL>>
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace BenGorUser\UserBundle\Controller;

use BenGorUser\User\Application\Query\UserOfInvitationTokenQuery;
use BenGorUser\User\Domain\Model\Exception\UserDoesNotExistException;
use BenGorUser\UserBundle\Form\Type\SignUpByInvitationType;
use BenGorUser\UserBundle\Form\Type\SignUpType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Sign up user controller.
 *
 * @author <NAME> <<EMAIL>>
 */
class SignUpController extends Controller
{
    /**
     * Default action, that it can executes the "default"
     * or "with_confirmation" specifications.
     *
     * @param Request $request   The request
     * @param string  $userClass Extra parameter that contains the user type
     * @param string  $firewall  Extra parameter that contains the firewall name
     * @param string  $command   Extra parameter that contains the fully qualified class name of command
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function defaultAction(Request $request, $userClass, $firewall, $command)
    {
        $roles = $this->getParameter('bengor_user.' . $userClass . '_default_roles');
        $form = $this->createForm(SignUpType::class, null, ['roles' => $roles, 'command' => $command]);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                try {
                    $this->get('bengor_user.' . $userClass . '_command_bus')->handle($form->getData());
                    $this->addFlash('notice', $this->get('translator')->trans('sign_up.success_flash'));

                    return $this
                        ->get('security.authentication.guard_handler')
                        ->authenticateUserAndHandleSuccess(
                            $this->get('bengor_user.' . $userClass . '_provider')->loadUserByUsername(
                                $form->get('email')->getData()
                            ),
                            $request,
                            $this->get('bengor_user.form_login_' . $userClass . '_authenticator'),
                            $firewall
                        );
                } catch (\Exception $exception) {
                    $this->get('logger')->addError($exception->getMessage());
                    $this->addFlash('error', $this->get('translator')->trans('sign_up.error_flash_generic'));
                }
            }
        }

        return $this->render('@BenGorUser/sign_up/default.html.twig', [
            'form' => $form->createView(),
        ]);
    }

    /**
     * By invitation action, that it can executes the "by_invitation"
     * or "by_invitation_with_confirmation" specifications.
     *
     * @param Request $request         The request
     * @param string  $invitationToken The invitation token
     * @param string  $userClass       Extra parameter that contains the user type
     * @param string  $firewall        Extra parameter that contains the firewall name
     * @param string  $command         Extra parameter that contains the fully qualified class name of command
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function byInvitationAction(Request $request, $invitationToken, $userClass, $firewall, $command)
    {
        try {
            // we need to know if the invitation token given exists in
            //database, in case that it isn't, it throws 404.
            $user = $this->get('bengor_user.' . $userClass . '_invitation_token_query')->__invoke(
                new UserOfInvitationTokenQuery($invitationToken)
            );
            $dataTransformer = $this->get('bengor_user.user_symfony_data_transformer');
            $dataTransformer->write($user);
            $user = $dataTransformer->read();
        } catch (UserDoesNotExistException $exception) {
            throw $this->createNotFoundException();
        }

        $form = $this->createForm(SignUpByInvitationType::class, null, [
            'roles'            => $this->getParameter('bengor_user.' . $userClass . '_default_roles'),
            'invitation_token' => $invitationToken,
            'command'          => $command,
        ]);
        if ($request->isMethod('POST')) {
            $form->handleRequest($request);
            if ($form->isValid()) {
                try {
                    $this->get('bengor_user.' . $userClass . '_command_bus')->handle($form->getData());
                    $this->addFlash('notice', $this->get('translator')->trans('sign_up.success_flash'));

                    return $this
                        ->get('security.authentication.guard_handler')
                        ->authenticateUserAndHandleSuccess(
                            $user,
                            $request,
                            $this->get('bengor_user.form_login_' . $userClass . '_authenticator'),
                            $firewall
                        );
                } catch (\Exception $exception) {
                    $this->get('logger')->addError($exception->getMessage());
                    $this->addFlash('error', $this->get('translator')->trans('sign_up.error_flash_generic'));
                }
            }
        }

        return $this->render('@BenGorUser/sign_up/by_invitation.html.twig', [
            'email' => $user->getUsername(),
            'form'  => $form->createView(),
        ]);
    }
{'fraction_non_alphanumeric': 0.1260895320301261, 'fraction_numerical': 0.0005077430820005078, 'mean_word_length': 2.0656290531776915, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 146, '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': '2139491', 'n_tokens_mistral': 3147, 'n_tokens_neox': 3055, 'n_words': 675}