src/Controller/RegistrationController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\Customer;
  5. use App\Form\RegistrationFormType;
  6. use App\Repository\UserRepository;
  7. use Symfony\Component\Mime\Address;
  8. use App\Repository\CustomerRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use App\Repository\Variables\UserTypeRepository;
  17. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  20. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  21. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  22. #[Route('/register')]
  23. class RegistrationController extends AbstractController
  24. {
  25. public function __construct(private VerifyEmailHelperInterface $verifyEmailHelper, private MailerInterface $mailer)
  26. {
  27. }
  28. #[Route('/', name: 'customer_register')]
  29. public function register(Request $request, UserPasswordHasherInterface $passwordHasher): Response
  30. {
  31. if ($this->isGranted('IS_AUTHENTICATED')) {
  32. $this->addFlash('warning', 'Vous êtes déja connecté');
  33. return $this->redirectToRoute('app_login');
  34. }
  35. $customer = new Customer();
  36. $form = $this->createForm(RegistrationFormType::class, $customer);
  37. $form->handleRequest($request);
  38. if ($form->isSubmitted() && $form->isValid()) {
  39. /** @var Customer $customer */
  40. $customer = $form->getData();
  41. /** @var string $password */
  42. $password = $form->get('plainPassword')->getData();
  43. $hashedPassword = $passwordHasher->hashPassword($customer, $password);
  44. $customer->setPassword($hashedPassword);
  45. $session = $request->getSession();
  46. $session->set('customer', $customer);
  47. return $this->redirectToRoute('customer_civility');
  48. }
  49. return $this->render('registration/register.html.twig', [
  50. 'form' => $form->createView(),
  51. ]);
  52. }
  53. #[Route('/customer/civility', name: 'customer_civility')]
  54. public function civility(Request $request): Response
  55. {
  56. $session = $request->getSession();
  57. $customer = $session->get('customer');
  58. if (!$customer) {
  59. return $this->redirectToRoute('customer_register');
  60. }
  61. $form = $this->createForm(RegistrationFormType::class, $customer);
  62. $form->handleRequest($request);
  63. if ($form->isSubmitted() && $form->isValid()) {
  64. $customer = $form->getData();
  65. $session->set('customer', $customer);
  66. return $this->redirectToRoute('customer_address');
  67. }
  68. return $this->render('registration/civility.html.twig', [
  69. 'form' => $form->createView(),
  70. ]);
  71. }
  72. #[Route('/customer/address', name: 'customer_address')]
  73. public function address(Request $request, UserTypeRepository $userTypeRepository, CustomerRepository $customerRepository): Response
  74. {
  75. $session = $request->getSession();
  76. /** @var ?Customer $customer */
  77. $customer = $session->get('customer');
  78. if (!$customer) {
  79. return $this->redirectToRoute('customer_register');
  80. }
  81. $customer->removeAllAddress();
  82. $form = $this->createForm(RegistrationFormType::class, $customer);
  83. $form->handleRequest($request);
  84. if ($form->isSubmitted() && $form->isValid()) {
  85. /** @var Customer $customer */
  86. $customer = $form->getData();
  87. $userType = $userTypeRepository->findOneByName("Client particulier");
  88. $customer->setUserType($userType);
  89. $customerRepository->add($customer, true);
  90. $session->set('customer', $customer);
  91. return $this->redirectToRoute('customer_know', ['id' => $customer->getId()]);
  92. }
  93. return $this->render('registration/address.html.twig', [
  94. 'form' => $form->createView(),
  95. ]);
  96. }
  97. #[Route('/customer/know/{id}', name: 'customer_know')]
  98. public function know(Request $request, ManagerRegistry $doctrine, Customer $customer): Response
  99. {
  100. $entityManager = $doctrine->getManager();
  101. $form = $this->createForm(RegistrationFormType::class, $customer);
  102. $form->handleRequest($request);
  103. if ($form->isSubmitted() && $form->isValid()) {
  104. /** @var Customer $customer */
  105. $customer = $form->getData();
  106. /** @var string $know */
  107. $know = $form->get('know')->getData();
  108. if ('Autre' == $know) {
  109. /** @var string $other */
  110. $other = $form->get('other')->getData();
  111. $know = '' != $other ? $other : $know;
  112. $customer->setKnow($know);
  113. }
  114. $entityManager->flush();
  115. return $this->redirectToRoute('app_confirmation', ['id' => $customer->getId()]);
  116. }
  117. return $this->render('registration/know.html.twig', [
  118. 'form' => $form->createView(),
  119. 'id' => $customer->getId(),
  120. ]);
  121. }
  122. /**
  123. * @Route("/verify", name="registration_confirmation_route")
  124. */
  125. public function verifyUserEmail(Request $request, UserRepository $userRepository, EntityManagerInterface $entityManager): Response
  126. {
  127. $id = $request->get('id');
  128. if (null === $id) {
  129. return $this->redirectToRoute('customer_register');
  130. }
  131. $user = $userRepository->find($id);
  132. if (null === $user) {
  133. return $this->redirectToRoute('customer_register');
  134. }
  135. // Do not get the User's Id or Email Address from the Request object
  136. try {
  137. $this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), (string) $user->getId(), $user->getEmail());
  138. } catch (VerifyEmailExceptionInterface $e) {
  139. $this->addFlash('verify_email_error', $e->getReason());
  140. return $this->redirectToRoute('customer_register');
  141. }
  142. // Mark your user as verified. e.g. switch a User::verified property to true
  143. $user->setIsVerified(true);
  144. $entityManager->persist($user);
  145. $entityManager->flush();
  146. $this->addFlash('success', 'Votre email a bien été vérifié');
  147. return $this->redirectToRoute('app_login');
  148. }
  149. /**
  150. * @Route("/confirmation/{id}", name="app_confirmation")
  151. *
  152. * @throws TransportExceptionInterface
  153. */
  154. public function confirmationConfirm(User $user): Response
  155. {
  156. $userConfirm = $user->isVerified();
  157. if (true === $userConfirm) {
  158. return $this->redirectToRoute('app_login');
  159. }
  160. $signatureComponents = $this->verifyEmailHelper->generateSignature(
  161. 'registration_confirmation_route',
  162. (string) $user->getId(),
  163. $user->getEmail(),
  164. ['id' => $user->getId()] // add the user's id as an extra query param
  165. );
  166. $email = new TemplatedEmail();
  167. $email->from(new Address('contact@nibuco.fr', 'Livraison Nibuco'));
  168. $email->to($user->getEmail());
  169. $email->subject('Confirmez votre email');
  170. $email->htmlTemplate('registration/confirmation_email.html.twig');
  171. $email->context(['expiresAtMessageKey' => '1 heure', 'signedUrl' => $signatureComponents->getSignedUrl()]);
  172. $this->mailer->send($email);
  173. return $this->render('registration/check_registration.html.twig', [
  174. 'confirm' => 'confirm',
  175. 'user' => $user,
  176. ]);
  177. }
  178. }