src/Controller/RegistrationController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Security\EmailVerifier;
  5. use App\Form\RegistrationFormType;
  6. use App\Repository\UserRepository;
  7. use Symfony\Component\Mime\Address;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. class RegistrationController extends AbstractController
  19. {
  20.     private $emailVerifier;
  21.     private $params;
  22.     private $manager;
  23.     public function __construct(EmailVerifier $emailVerifierParameterBagInterface $paramsEntityManagerInterface $manager)
  24.     {
  25.         $this->emailVerifier $emailVerifier;
  26.         $this->params $params;
  27.         $this->manager $manager;
  28.     }
  29.     /**
  30.      * @Route("/register", name="app_register")
  31.      */
  32.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherInterfaceTranslatorInterface $translator): Response
  33.     {
  34.         $user = new User();
  35.         $form $this->createForm(RegistrationFormType::class, $user);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             // encode the plain password
  39.             $user->setPassword(
  40.             $userPasswordHasherInterface->hashPassword(
  41.                     $user,
  42.                     $form->get('plainPassword')->getData()
  43.                 )
  44.             );
  45.             //set active lang and persote user
  46.             $user->setLang($request->getLocale());
  47.             $this->manager->persist($user);
  48.             $this->manager->flush();
  49.             // generate a signed url and email it to the user
  50.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  51.                 (new TemplatedEmail())
  52.                     ->from(new Address($this->params->get('app.sendermail'), $this->params->get('app.title')))
  53.                     ->to($user->getEmail())
  54.                     ->subject($translator->trans('register.subject', [], 'email'))
  55.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  56.             );
  57.             return $this->redirectToRoute('app_register_verify');
  58.         }
  59.         return $this->render('registration/register.html.twig', [
  60.             'registrationForm' => $form->createView(),
  61.         ]);
  62.     }
  63.     /**
  64.      * @Route("/verify/email", name="app_verify_email")
  65.      */
  66.     public function verifyUserEmail(Request $requestUserRepository $userRepositoryTranslatorInterface $translator): Response
  67.     {
  68.         $id $request->get('id'); // retrieve the user id from the url
  69.         // Verify the user id exists and is not null
  70.         if (null === $id) {
  71.             return $this->redirectToRoute('app_register');
  72.         }
  73.         $user $userRepository->find($id);
  74.         // Ensure the user exists in persistence
  75.         if (null === $user) {
  76.             return $this->redirectToRoute('app_register');
  77.         }
  78.         // validate email confirmation link, sets User::isVerified=true and persists
  79.         try {
  80.             $this->emailVerifier->handleEmailConfirmation($request$user);
  81.         } catch (VerifyEmailExceptionInterface $exception) {
  82.             $this->addFlash('verify_email_error'$exception->getReason());
  83.             return $this->redirectToRoute('app_register');
  84.         }
  85.         // flash and redirect
  86.         $this->addFlash('success'$translator->trans('register.confirm'));
  87.         return $this->redirectToRoute('app_login');
  88.     }
  89.     /**
  90.      * @Route("/register/verify", name="app_register_verify")
  91.      */
  92.     public function verifyMsg(): Response
  93.     {
  94.         return $this->render('registration/confirm.html.twig');
  95.     }
  96. }