src/Controller/RegistrationController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use App\Security\AppAuthenticator;
  7. use App\Security\EmailVerifier;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Mime\Address;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. class RegistrationController extends AbstractController
  20. {
  21.     private EmailVerifier $emailVerifier;
  22.     public function __construct(EmailVerifier $emailVerifier)
  23.     {
  24.         $this->emailVerifier $emailVerifier;
  25.     }
  26.     /**
  27.      * @Route("/register", name="app_register")
  28.      */
  29.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppAuthenticator $authenticatorEntityManagerInterface $entityManager): Response
  30.     {
  31.         $user = new Customer();
  32.         $form $this->createForm(RegistrationFormType::class, $user);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             // encode the plain password
  36.             $user->setPassword(
  37.                 $userPasswordHasher->hashPassword(
  38.                     $user,
  39.                     $form->get('plainPassword')->getData()
  40.                 )
  41.             );
  42.             $entityManager->persist($user);
  43.             $entityManager->flush();
  44.             // generate a signed url and email it to the user
  45. //            $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
  46. //                (new TemplatedEmail())
  47. //                    ->from(new Address($this->getParameter('mailer_sender'), $this->getParameter('default_app_name')))
  48. //                    ->to($user->getUserIdentifier())
  49. //                    ->subject("Votre nouveau compte sur {$this->getParameter('default_app_name')}")
  50. //                    ->htmlTemplate('registration/new_account_email.html.twig')
  51. //                    ->context([
  52. //                        "password" => $form->get('plainPassword')->getData(),
  53. //                        "user" => $user,
  54. //                    ])
  55. //            );
  56.             // do anything else you need here, like send an email
  57.             return $userAuthenticator->authenticateUser(
  58.                 $user,
  59.                 $authenticator,
  60.                 $request
  61.             );
  62.         }
  63.         $isAttached $request->get('attached');
  64.         if ($isAttached) {
  65.             $template 'registration/register.html.twig';
  66.         } else {
  67.             $template 'registration/_form.html.twig';
  68.         }
  69.         return $this->render($template, [
  70.             'registrationForm' => $form->createView(),
  71.         ]);
  72.     }
  73.     /**
  74.      * @Route("/verify/email", name="app_verify_email")
  75.      */
  76.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  77.     {
  78.         $id $request->get('id');
  79.         if (null === $id) {
  80.             return $this->redirectToRoute('app_register');
  81.         }
  82.         $user $userRepository->find($id);
  83.         if (null === $user) {
  84.             return $this->redirectToRoute('app_register');
  85.         }
  86.         // validate email confirmation link, sets User::isVerified=true and persists
  87.         try {
  88.             $this->emailVerifier->handleEmailConfirmation($request$user);
  89.         } catch (VerifyEmailExceptionInterface $exception) {
  90.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  91.             return $this->redirectToRoute('app_register');
  92.         }
  93.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  94.         $this->addFlash('success''Your email address has been verified.');
  95.         return $this->redirectToRoute('index');
  96.     }
  97. }