<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\contact;
use App\Form\RegistrationFormType;
use App\Message\SendEmailMessage;
use App\Security\LoginFormAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
class RegistrationController extends AbstractController
{
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, LoginFormAuthenticator $authenticator, EntityManagerInterface $entityManager, MessageBusInterface $bus): Response
{
$user = new User();
$csrfToken = $this->get('security.csrf.token_manager')->getToken('register')->getValue();
$form = $this->createForm(RegistrationFormType::class, $user, [
'csrf_token' => $csrfToken,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = (new Contact())
->setEmail($user->getEMail())
->setUser($user);
$user->setContact($contact);
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
$bus->dispatch(new SendEmailMessage('validation_registration', '', $user->getId(),$user->getEmail()));
return $userAuthenticator->authenticateUser(
$user,
$authenticator,
$request
);
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
}