src/Controller/Front/RegistrationController.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * Unauthorized copying of this file, via any medium is strictly prohibited
  4.  * Proprietary and confidential.
  5.  *
  6.  * @author Bilel AZRI          <azri.bilel@gmail.com>
  7.  * @author Assma BEN SASSI     <bensassiasma.bws@gmail.com>
  8.  *
  9.  * Bicking man (c) 2019-present.
  10.  */
  11. declare(strict_types=1);
  12. namespace App\Controller\Front;
  13. use App\Entity\User\Participant;
  14. use App\Form\Front\User\Registration\RegistrationFormType;
  15. use App\Repository\User\ParticipantRepository;
  16. use App\Security\EmailVerifier;
  17. use App\Security\FrontAuthenticator;
  18. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Mime\Address;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  26. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  27. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  28. use Twig\Environment;
  29. class RegistrationController extends AbstractController
  30. {
  31.     private $emailVerifier;
  32.     private Environment $twig;
  33.     private UrlGeneratorInterface $urlGenerator;
  34.     public function __construct(Environment $twigEmailVerifier $emailVerifierUrlGeneratorInterface $urlGenerator)
  35.     {
  36.         $this->twig $twig;
  37.         $this->emailVerifier $emailVerifier;
  38.         $this->urlGenerator $urlGenerator;
  39.     }
  40.     /**
  41.      * @Route("/register", name="app_register")
  42.      */
  43.     public function register(Request $requestUserPasswordEncoderInterface $passwordEncoderGuardAuthenticatorHandler $guardHandlerFrontAuthenticator $authenticator): Response
  44.     {
  45.         $user = new Participant();
  46.         $form $this->createForm(RegistrationFormType::class, $user, [
  47.             'action' => $this->urlGenerator->generate('front.app_register'),
  48.         ]);
  49.         $form->handleRequest($request);
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             // encode the plain password
  52.             $user->setPassword(
  53.                 $passwordEncoder->encodePassword(
  54.                     $user,
  55.                     $form->get('plainPassword')->getData()
  56.                 )
  57.             );
  58.             $entityManager $this->getDoctrine()->getManager();
  59.             $user->setProgressiveStatus(0);
  60.             $entityManager->persist($user);
  61.             $entityManager->flush();
  62.             // generate a signed url and email it to the user
  63.             // ASMAAAA disable Email send
  64.             $emailContent $this->twig->render('emails/user/registration_confirmation_email.html.twig', [
  65.                 'user' => $user,
  66.             ]);
  67.             $this->emailVerifier->sendEmailConfirmation('front.app_verify_email'$user,
  68.                 (new TemplatedEmail())
  69.                     ->from(new Address('app@bikingman.com''BikingMan'))
  70.                     ->to($user->getEmail())
  71.                     ->subject('Votre inscription au dashboard BikingMan')
  72.                     ->html($emailContent)
  73.             ); 
  74.             // do anything else you need here, like send an email
  75.             return $guardHandler->authenticateUserAndHandleSuccess(
  76.                 $user,
  77.                 $request,
  78.                 $authenticator,
  79.                 'main' // firewall name in security.yaml
  80.             );
  81.         }
  82.         return $this->render('front/security/register.html.twig', [
  83.             'registrationForm' => $form->createView(),
  84.         ]);
  85.     }
  86.     /**
  87.      * @Route("/verify/email", name="app_verify_email")
  88.      */
  89.     public function verifyUserEmail(Request $requestParticipantRepository $participantRepository): Response
  90.     {
  91.         $id $request->get('id');
  92.         if (null === $id) {
  93.             return $this->redirectToRoute('front.app_register');
  94.         }
  95.         $user $participantRepository->find($id);
  96.         if (null === $user) {
  97.             return $this->redirectToRoute('front.app_register');
  98.         }
  99.         // validate email confirmation link, sets User::isVerified=true and persists
  100.         try {
  101.             $this->emailVerifier->handleEmailConfirmation($request$user);
  102.         } catch (VerifyEmailExceptionInterface $exception) {
  103.             $this->addFlash('verify_email_error'$exception->getReason());
  104.             return $this->redirectToRoute('front.app_register');
  105.         }
  106.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  107.         $this->addFlash('success''Your email address has been verified.');
  108.         return $this->redirectToRoute('front.app_register');
  109.     }
  110. }