src/Controller/RegistrationController.php line 77

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\Category;
  5. use App\Entity\PaymentT;
  6. use App\Service\EmailService;
  7. use App\Security\EmailVerifier;
  8. use App\Form\RegistrationFormType;
  9. use App\Security\AppAuthenticator;
  10. use Symfony\Component\Mime\Address;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Mailer\MailerInterface;
  16. use Symfony\Component\Translation\Translator;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Translation\LocaleSwitcher;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. // use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  23. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  24. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  25. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  26. class RegistrationController extends BaseCommonController
  27. {
  28.     private EmailVerifier $emailVerifier;
  29.     private $emailService;
  30.     public function __construct(EmailVerifier $emailVerifierEmailService $emailService)
  31.     {
  32.         $this->emailVerifier $emailVerifier;
  33.         $this->emailService $emailService;
  34.     }
  35.     #[Route('/send-test-email'name'send_test_email')]
  36.     public function sendTestEmail(): Response
  37.     {
  38.         $this->emailService->sendEmail('georgeskayal2014@gmail.com''Test Email''This is a test email.');
  39.         
  40.         return new Response('Email sent successfully');
  41.     }
  42.     
  43.     private function generateSignedUrl(User $user): string
  44.     {
  45.         // Your logic to generate a signed URL
  46.         // Example: Generate a unique token, add it to the URL, and sign it
  47.         // (Remember to handle expiration and security considerations)
  48.         // ...
  49.         return 'https://example.com/signed-url'// Replace with your actual URL
  50.     }
  51.     #[Route('/register'name'app_register')]
  52.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppAuthenticator $authenticator,
  53.      EntityManagerInterface $entityManager,ManagerRegistry $doctrine,
  54.      EmailService $mail,LocaleSwitcher $localeSwitcher,TranslatorInterface $translator,MailerInterface $mailer ): Response
  55.     {
  56.         // $currentLocale = $localeSwitcher->getLocale();
  57.         // $locale = $request->get('_locale');
  58.         $locale =  $request->getSession()->get('_locale');
  59.         if($locale !=null){
  60.             $localeSwitcher->setLocale($locale);
  61.             $translator->setLocale($locale);    
  62.             $request->getSession()->set('_locale',$locale);
  63.         }
  64.         $user = new User();
  65.         $form $this->createForm(RegistrationFormType::class, $user);
  66.         $form->handleRequest($request);
  67.         if ($form->isSubmitted() && $form->isValid()) {
  68.             // encode the plain password
  69.             $user->setPassword(
  70.                 $userPasswordHasher->hashPassword(
  71.                     $user,
  72.                     $form->get('plainPassword')->getData()
  73.                 )
  74.             );
  75.             $pt $entityManager->getRepository(PaymentT::class)->find(1);
  76.             
  77.             $user->setPaymentT($pt);
  78.             $entityManager->persist($user);
  79.             $entityManager->flush();
  80.             // $signedUrl = $this->emailVerifier->generateSignedUrl('app_verify_email', $user);
  81. // test email
  82. // test email
  83.             // Send the email with the signed URL
  84.             // $email = (new TemplatedEmail())
  85.             //     ->from('VerifyMyAccount@julico.io')
  86.             //     ->to(new Address($user->getEmail()))
  87.             //     ->subject('Confirm your email address')
  88.             //     ->htmlTemplate('registration/confirmation_email.html.twig')
  89.             //     ->context([
  90.             //         'signedUrl' => $signedUrl,
  91.             //     ]);
  92.     
  93.                 try{
  94.                     // // generate a signed url and email it to the user
  95.                     $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  96.                     (new TemplatedEmail())
  97.                     // Verify20241
  98.                     ->from(new Address('no-reply@julico.io''Admin App'))
  99.                     ->to($user->getEmail())
  100.                     ->subject('Please Confirm your Email')
  101.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  102.                     // ->context([
  103.                         // 'resetToken' => $resetToken,
  104.                         // 'username' => $user->getPrenom(),
  105.                         // ]);
  106.                         
  107.                         // $mailer->send($email);
  108.                     // ->context([
  109.                         //             'signedUrl' => $signedUrl,
  110.                         //         ])
  111.                     );
  112.                     // var_dump("after send" . "send email");die;
  113.                     // $urlemail = 'reset_password/email'. $locale .'.html.twig';
  114.                     // // dd($urlemail);
  115.                     // $email = (new TemplatedEmail())
  116.                     // ->from(new Address('no-reply@julico.io', 'Julico Reset'))
  117.                     // ->to($user->getEmail())
  118.                     // ->subject('Reset your Julico password')
  119.                     // ->htmlTemplate($urlemail)
  120.                     // ->context([
  121.                     //     'resetToken' => $resetToken,
  122.                     //     'username' => $user->getPrenom(),
  123.                     //     ]);
  124.                         
  125.                     //     $mailer->send($email);
  126.                     // // do anything else you need here, like send an email
  127.                 }
  128.                 catch (Exception $e) {
  129.                     // Handle the exception
  130.                     // You can log the error or show a flash message
  131.                     // For example:
  132.                     // $this->addFlash('error', 'An error occurred while saving the entity.');
  133.                     var_dump("exception" $exception);die;
  134.                 }
  135.                     // $mail->sendEmail('VerifyMyAccount@julico.io',
  136.                     // $user->getEmail(),
  137.                     // 'Activation de votre compte',       
  138.                     // 'confirmation_email',
  139.                     // compact('user')
  140.                     // );
  141.                     
  142.             return $userAuthenticator->authenticateUser(
  143.                 $user,
  144.                 $authenticator,
  145.                 $request
  146.             );
  147.         }
  148.         return $this->render('registration/register.html.twig', [
  149.             'registrationForm' => $form->createView()
  150.         ]);
  151.     }
  152.     #[Route('/verify/email'name'app_verify_email')]
  153.     public function verifyUserEmail(Request $requestTranslatorInterface $translator): Response
  154.     {
  155.         // var_dump("dans app verify ");die;
  156.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  157.         // validate email confirmation link, sets User::isVerified=true and persists
  158.         try {
  159.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  160.             
  161.         } catch (VerifyEmailExceptionInterface $exception) {
  162.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  163.             return $this->redirectToRoute('app_register');
  164.         }
  165.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  166.         $this->addFlash('success''Your email address has been verified.');
  167.         return $this->redirectToRoute('app_register');
  168.     }
  169. }