<?php
namespace App\Controller\Adherent\Registration;
use App\Entity\ContactEmail;
use App\Entity\User;
use App\Form\Adherent\Registration\UserPasswordType;
use App\Form\Adherent\Registration\UserType;
use App\Security\AdherentLoginAuthenticator;
use App\Service\Registration\Session;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticatorManager;
#[Route(path: '/inscription', name: 'registration_user')]
class UserController extends AbstractRegistrationController
{
protected static int $step = 1;
public function __construct(protected Session $registrationSession, EntityManagerInterface $em, private readonly AuthenticatorManager $authenticatorManager)
{
parent::__construct($registrationSession, $em);
}
#[Route(path: '/nouvel-espace', name: '')]
public function user(Request $request, UserPasswordHasherInterface $passwordHasher, AdherentLoginAuthenticator $authenticator)
{
if ($this->getUser()) {
$this->registrationSession->setStep(static::$step + 1);
return $this->redirectToRoute('registration_family');
}
$user = new User();
$user->setIsTemporary(true);
$user->setPassword($passwordHasher->hashPassword($user, uniqid('', true)));
$form = $this->createForm(UserType::class, $user)->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contactEmail = (new ContactEmail())
->setEmail($user->getEmail());
$user->getPerson()->setFirstEmail($contactEmail);
$this->em->persist($user);
$this->em->flush();
$type = $form->get('type')->getData();
if ($type === 'self') {
$this->registrationSession->setPerson($user->getPerson());
}
$this->registrationSession->setRegistrationType($type);
$this->authenticatorManager->authenticateUser($user, $authenticator, $request);
return $this->redirectToRoute('registration_family');
}
return $this->render('adherent/registration/user.html.twig', ['form' => $form->createView()]);
}
#[Route(path: '/utilisateur', name: '_validation')]
public function validation(Request $request, AdherentLoginAuthenticator $authenticator)
{
$step = 8;
if ($step > $this->registrationSession->getStep()) {
return $this->redirectToRoute('registration_user');
}
$season = $this->registrationSession->getSeason();
$person = $this->registrationSession->getPerson();
$user = $this->getUser();
$data = ['email' => $user->getEmail()];
$form = $this->createForm(UserPasswordType::class, $data, ['temporary_user' => $user->isIsTemporary()])
->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->has('plainPassword')) {
$plainPassword = $form->get('plainPassword')->getData();
$user->setPlainPassword($plainPassword);
}
$this->registrationSession->setStep($step + 1);
$this->em->flush();
return $this->redirectToRoute('registration_finalize');
}
return $this->render('adherent/registration/user_validation.html.twig', [
'form' => $form->createView(),
'step' => $step,
'season' => $season,
'person' => $person,
'email' => $user->getEmail(),
]);
}
}