src/Controller/RegistrationController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\CategoryRepository;
  6. use App\Repository\ProductRepository;
  7. use App\Repository\UserRepository;
  8. use App\Security\EmailVerifier;
  9. use App\Services\Cart;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. class RegistrationController extends AbstractController
  22. {
  23.     private EmailVerifier $emailVerifier;
  24.     public function __construct(EmailVerifier $emailVerifier)
  25.     {
  26.         $this->emailVerifier $emailVerifier;
  27.     }
  28.     #[Route('/account'name'account')]
  29.     public function account(Request $requestProductRepository $productRepoUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManagerAuthenticationUtils $authenticationUtilsCategoryRepository $catRepo): Response
  30.     {
  31.         $categories $catRepo->findAll();
  32.         //TO COPY TO EVERY ROUTE THAT DISPLAY CART
  33.         $session $request->getSession();
  34.         $cart = new Cart($session);
  35.         $totalQuantity $cart->getCartTotalQuantity();
  36.         $maxQuantity $cart->getMaxQuantityAllowed();
  37.         $cartProducts $cart->getCartProducts($productRepo);
  38.         $totalPrice $cart->getTotalPrice($productRepo);        
  39.         //REGISTER SYSTEM SECTION
  40.         $user = new User();
  41.         $form $this->createForm(RegistrationFormType::class, $user);
  42.         $form->handleRequest($request);
  43.         if ($form->isSubmitted() && $form->isValid()) {
  44.             
  45.             // encode the plain password
  46.             $user->setPassword(
  47.                 $userPasswordHasher->hashPassword(
  48.                     $user,
  49.                     $form->get('plainPassword')->getData()
  50.                 )
  51.             );
  52.             $entityManager->persist($user);
  53.             $entityManager->flush();
  54.             // generate a signed url and email it to the user
  55.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  56.                 (new TemplatedEmail())
  57.                     ->from(new Address('no-reply@lestresorsdeluna.fr''Luna Mail Bot'))
  58.                     ->to($user->getEmail())
  59.                     ->subject('Veuillez confirmer votre email')
  60.                     ->htmlTemplate('emails/confirmation_email.html.twig')
  61.             );
  62.             // do anything else you need here, like send an email
  63.             $this->addFlash('message''Création de compte réussie, un mail de confirmation vous a été envoyé');
  64.             return $this->redirectToRoute('app_home');
  65.         }
  66.         
  67.         // get the login error if there is one
  68.         $error $authenticationUtils->getLastAuthenticationError();
  69.         // last username entered by the user
  70.         $lastUsername $authenticationUtils->getLastUsername();
  71.         return $this->render('account/account.html.twig', [
  72.             'categories' => $categories,
  73.             'registrationForm' => $form->createView(),
  74.             'last_username' => $lastUsername,
  75.             'error' => $error,
  76.             //TO COPY TO EVERY ROUTE THAT DISPLAY CART
  77.             'totalQuantity' => $totalQuantity,
  78.             'maxQuantity' => $maxQuantity,
  79.             'cartProducts' => $cartProducts,
  80.             'totalPrice' => $totalPrice
  81.         ]);
  82.     }
  83.     #[Route('/register-test'name'app_register_base')]
  84.     public function registerTest(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManager): Response
  85.     {
  86.         $user = new User();
  87.         $form $this->createForm(RegistrationFormType::class, $user);
  88.         $form->handleRequest($request);
  89.         if ($form->isSubmitted() && $form->isValid()) {
  90.             // encode the plain password
  91.             $user->setPassword(
  92.                 $userPasswordHasher->hashPassword(
  93.                     $user,
  94.                     $form->get('plainPassword')->getData()
  95.                 )
  96.             );
  97.             $entityManager->persist($user);
  98.             $entityManager->flush();
  99.             // generate a signed url and email it to the user
  100.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  101.                 (new TemplatedEmail())
  102.                     ->from(new Address('no-reply@lestresorsdeluna.fr''Luna Mail Bot'))
  103.                     ->to($user->getEmail())
  104.                     ->subject('Please Confirm your Email')
  105.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  106.             );
  107.             // do anything else you need here, like send an email
  108.             return $this->redirectToRoute('app_home');
  109.         }
  110.         return $this->render('registration/register_base.html.twig', [
  111.             'registrationForm' => $form->createView(),
  112.         ]);
  113.     }
  114.     #[Route('/verify/email'name'app_verify_email')]
  115.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  116.     {
  117.         $id $request->query->get('id');
  118.         if (null === $id) {
  119.             return $this->redirectToRoute('account');
  120.         }
  121.         $user $userRepository->find($id);
  122.         if (null === $user) {
  123.             return $this->redirectToRoute('account');
  124.         }
  125.         // validate email confirmation link, sets User::isVerified=true and persists
  126.         try {
  127.             $this->emailVerifier->handleEmailConfirmation($request$user);
  128.         } catch (VerifyEmailExceptionInterface $exception) {
  129.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  130.             return $this->redirectToRoute('account');
  131.         }
  132.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  133.         $this->addFlash('success''Votre email à été vérifié.');
  134.         return $this->redirectToRoute('app_home');
  135.     }
  136.     #[Route('generate-confirmation-link'name'generate_confirmation_list')]
  137.     public function generateConfirmationLink()
  138.     {
  139.         $user $this->getUser();
  140.         $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  141.         (new TemplatedEmail())
  142.             ->from(new Address('no-reply@lestresorsdeluna.fr''Luna Mail Bot'))
  143.             ->to($user->getEmail())
  144.             ->subject('Veuillez confirmer votre email')
  145.             ->htmlTemplate('emails/confirmation_email.html.twig')
  146.         );
  147.         return $this->redirectToRoute('app_home');
  148.     }
  149. }