src/Controller/RegistrationController.php line 27

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Mailer\MailerInterface;
  12. use Symfony\Component\Mime\Email;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  16. class RegistrationController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/register", name="app_register")
  20.      */
  21.     public function register(Request $requestUserPasswordHasherInterface $passwordEncoderMailerInterface $mailer\App\Services\CompanyDetailsService $companyDetailsServiceEntityManagerInterface $entityManagerUrlGeneratorInterface $urlGenerator): Response
  22.     {
  23.         $user = new User();
  24.         $form $this->createForm(RegistrationFormType::class, $user);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             $user->setEmailVerified(false);
  28.             $user->setPassword(
  29.                 $passwordEncoder->hashPassword(
  30.                     $user,
  31.                     $form->get('plainPassword')->getData()
  32.                 )
  33.             );
  34.             try {
  35.                 $entityManager->persist($user);
  36.                 $entityManager->flush();
  37.             } catch (UniqueConstraintViolationException $e) {
  38.                 // Add a user-friendly error on the email field
  39.                 $loginUrl $urlGenerator->generate('app_login');
  40.                 $resetUrl $urlGenerator->generate('app_forgot_password_request'); // adjust to your route
  41.                 $message sprintf(
  42.                     "An account with this email already exists. <a href='%s'>Log in</a> or <a href='%s'>reset your password</a>.",
  43.                     $loginUrl,
  44.                     $resetUrl
  45.                 );
  46.                 $form->get('email')->addError(new FormError($message));
  47.                 // re-render the form with the error, no email sent
  48.                 return $this->render('registration/register.html.twig', [
  49.                     'registrationForm' => $form->createView(),
  50.                 ]);
  51.             }
  52.             // -- send verification email only after successful flush --
  53.             $companyEmail $companyDetailsService->getCompanyDetails()->getCompanyEmail();
  54.             $companyName $companyDetailsService->getCompanyDetails()->getCompanyName();
  55.             $htmlBody $companyDetailsService->getCompanyDetails()->getRegistrationEmail();
  56.             $url $this->generateUrl('register_verify_email', ['id' => $user->getId()], UrlGeneratorInterface::ABSOLUTE_URL);
  57.             $htmlSubject $companyName ' :: Registration confirmation';
  58.             $htmlLink "Please click on the link below to verify your email address.<br> <a class='btn btn-success' href='" $url "'>Verify E-mail</a> ";
  59.             $emailMsg = (new Email())
  60.                 ->from($companyEmail)
  61.                 ->to($user->getEmail())
  62.                 ->bcc('nurse_stephen@hotmail.com')
  63.                 ->subject($htmlSubject)
  64.                 ->html($htmlBody $htmlLink);
  65.             $mailer->send($emailMsg);
  66.             return $this->redirectToRoute('app_login');
  67.         }
  68.         return $this->render('registration/register.html.twig', [
  69.             'registrationForm' => $form->createView(),
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/verify/email/{id}", name="register_verify_email")
  74.      */
  75.     public function verifyEmail(Request $requestint $idUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  76.     {
  77.         $user $userRepository->find($id);
  78.         $user->setEmailVerified(true);
  79.         $entityManager->flush();
  80.         return $this->redirectToRoute('app_login');
  81.     }
  82. }