src/Controller/RegistrationController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\EmailVerifier;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Mime\Address;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  15. use App\Entity\Prescriber;
  16. use App\Form\PrescriberRegistrationType;
  17. use App\Service\PrescriberCodeGeneratorService;
  18. use App\Service\RPPSValidatorService;
  19. use App\Service\SIRENValidatorService;
  20. use Symfony\Component\Form\FormError;
  21. use App\Service\PrescriberService;
  22. use App\Service\EmailService;
  23. use App\Repository\PrescriberActivityStatusRepository;
  24. use App\Repository\BillingCenterRepository;
  25. use Symfony\Component\Mailer\MailerInterface;
  26. class RegistrationController extends AbstractController
  27. {
  28.     private const DEFAULT_BILLING_SLUG 'BIONOPS-SA';
  29.     private EmailVerifier $emailVerifier;
  30.     public function __construct(
  31.         EmailVerifier $emailVerifier,
  32.         private readonly PrescriberCodeGeneratorService $prescriberCodeGenerator,
  33.         private readonly PrescriberService              $prescriberService,
  34.         private readonly RPPSValidatorService $rppsValidator,
  35.         private readonly SIRENValidatorService $sirenValidator,
  36.         private readonly UserPasswordHasherInterface $userPasswordHasher,
  37.         private readonly EntityManagerInterface $entityManager,
  38.         private readonly PrescriberActivityStatusRepository $activityStatusRepository,
  39.         private readonly BillingCenterRepository $billingCenterRepository,
  40.         private readonly EmailService $emailService,
  41.         private readonly MailerInterface $mailer
  42.     ) {
  43.         $this->emailVerifier $emailVerifier;
  44.     }
  45.     /**
  46.      * Action pour l'inscription complète multi-étapes (Originale)
  47.      */
  48.     #[Route('/registration/prescriber'name'app_prescriber_register'methods: ['GET''POST'])]
  49.     public function registerPrescriber(Request $request): Response
  50.     {
  51.         $prescriber = new Prescriber();
  52.         
  53.         // Définir le centre de profit par défaut
  54.         $billingCenter $this->billingCenterRepository->findOneBy(['slug' => self::DEFAULT_BILLING_SLUG]);
  55.         if ($billingCenter) {
  56.             $prescriber->setBillingCenter($billingCenter);
  57.         }
  58.         $form $this->createForm(PrescriberRegistrationType::class, $prescriber);
  59.         $form->handleRequest($request);
  60.         $activeTab 'personal'// Onglet par défaut
  61.         if ($form->isSubmitted()) {
  62.             if ($form->isValid()) {
  63.                  // Vérifier SIREN/RPPS ici mais ne pas bloquer
  64.                 $rppsNumberField $form->get('rppsNumber');
  65.                 $sirenNumberField $form->get('sirenNumber');
  66.                 $rppsNumber $rppsNumberField->getData();
  67.                 $sirenNumber $sirenNumberField->getData();
  68.                 // $hasServiceError = false; // Plus utile pour bloquer
  69.                 if ($rppsNumber && !$this->rppsValidator->isValid($rppsNumber)) {
  70.                     // Commenté : Ne pas ajouter d'erreur bloquante ici
  71.                     // $rppsNumberField->addError(new FormError('Le numéro RPPS fourni n\'est pas valide.'));
  72.                     // $hasServiceError = true;
  73.                      $this->addFlash('warning''Le numéro RPPS fourni semble invalide mais l\'inscription peut continuer.'); // Optionnel
  74.                 }
  75.                 if ($sirenNumber && !$this->sirenValidator->isValid($sirenNumber)) {
  76.                      // Commenté : Ne pas ajouter d'erreur bloquante ici
  77.                      // $sirenNumberField->addError(new FormError('Le numéro SIREN fourni n\'est pas valide.'));
  78.                      // $hasServiceError = true;
  79.                      $this->addFlash('warning''Le numéro SIREN fourni semble invalide mais l\'inscription peut continuer.'); // Optionnel
  80.                 }
  81.                 // On continue la création même si $hasServiceError serait true
  82.                 // if (!$hasServiceError) { // Condition retirée
  83.                     // ... (logique de création de compte inchangée)
  84.                     $baseUsername strtolower($prescriber->getFirstname() . '.' $prescriber->getLastname());
  85.                     $username $baseUsername;
  86.                     if ($this->entityManager->getRepository(Prescriber::class)->findOneBy(['username' => $username])) {
  87.                         do {
  88.                             $randomNumber str_pad(random_int(0999), 3'0'STR_PAD_LEFT);
  89.                             $username $baseUsername '.' $randomNumber;
  90.                         } while ($this->entityManager->getRepository(Prescriber::class)->findOneBy(['username' => $username]));
  91.                     }
  92.                     $prescriber->setUsername($username);
  93.                     $prescriber->setPassword($this->userPasswordHasher->hashPassword($prescriber$form->get('plainPassword')->getData()));
  94.                     $prescriber->setApproved(false);
  95.                     $prescriber->setPointCoefficient(0.15);
  96.                     $prescriberCode $this->prescriberCodeGenerator->generatePrescriberCode($prescriber);
  97.                     $prescriber->setPrescriberCode($prescriberCode);
  98.                     // Définir la date de création
  99.                     $prescriber->setCreatedAt(new \DateTimeImmutable());
  100.                     // Récupérer et définir le statut d'activité 'new'
  101.                     $newStatus $this->activityStatusRepository->findOneBy(['slug' => 'new']);
  102.                     if (!$newStatus) {
  103.                         throw new \RuntimeException('Statut \'new\' introuvable pour PrescriberActivityStatus.');
  104.                     }
  105.                     $prescriber->setActivityStatus($newStatus);
  106.                     // Utiliser PrescriberService pour générer l'extranetCode unique
  107.                     $prescriber->setExtranetCode($this->prescriberService->getUniquePrescriberCode()); 
  108.                     // Définir le créateur comme étant le prescripteur lui-même
  109.                     $prescriber->setCreatedBy($prescriber);
  110.                     $this->entityManager->persist($prescriber);
  111.                     $this->entityManager->flush();
  112.                     // Envoi email informant que la demande va être traitée
  113.                     if ($this->emailService->isEmailSendingAllowedTo('app.allow_send_emails_prospect_new')) {
  114.                         $data = [
  115.                             'prescriber' => $prescriber,
  116.                         ];
  117.                         $email $this->emailService->sendEmail(
  118.                             $this->emailService->getEmailSender(),
  119.                             $prescriber->getEmail(),
  120.                             'Demande d\'inscription reçue',
  121.                             'emails/prescriber_registration_received.html.twig',
  122.                             $data,
  123.                         );
  124.                         if ($email) {
  125.                             $this->mailer->send($email);
  126.                         }
  127.                     }
  128.                     
  129.                     $this->addFlash('success''Votre demande d\'inscription a été enregistrée avec succès. Un email de confirmation vous a été envoyé.');
  130.                     return $this->redirectToRoute('app_login');
  131.                 // } else { // Partie else retirée
  132.                     // Erreur service détectée après form->isValid(), définir l'onglet actif sur pro
  133.                     // $activeTab = 'professional'; // Plus nécessaire de changer d'onglet ici
  134.                 // }
  135.             } else {
  136.                 // Le formulaire est invalide (contraintes Symfony), déterminer l'onglet
  137.                 $activeTab $this->determineActiveTabOnErrorMultiStep($form);
  138.             }
  139.         }
  140.         return $this->render('registration/prescriber_register.html.twig', [
  141.             'registrationForm' => $form->createView(),
  142.             'activeTab' => $activeTab,
  143.         ]);
  144.     }
  145.     /**
  146.      * Helper pour déterminer l'onglet actif en cas d'erreur sur le formulaire multi-étapes
  147.      */
  148.     private function determineActiveTabOnErrorMultiStep($form): string
  149.     {
  150.         // Ordre des onglets: personal -> professional -> address
  151.         // Vérifier erreurs étape 1 (personal)
  152.         if (!$form->get('firstname')->isValid() || !$form->get('lastname')->isValid() || !$form->get('email')->isValid() || !$form->get('phone')->isValid() || !$form->get('plainPassword')->isValid()) {
  153.             return 'personal';
  154.         }
  155.         // Vérifier erreurs étape 2 (professional)
  156.         if (!$form->get('rppsNumber')->isValid() || !$form->get('sirenNumber')->isValid() || !$form->get('mainSpeciality')->isValid()) {
  157.             return 'professional';
  158.         }
  159.         // Vérifier erreurs étape 3 (address)
  160.         if (!$form->get('address')->isValid() || !$form->get('postalCode')->isValid() || !$form->get('city')->isValid() || !$form->get('country')->isValid() || ($form->has('agreeTerms') && !$form->get('agreeTerms')->isValid())) {
  161.              return 'address';
  162.         }
  163.         // Défaut
  164.         return 'personal';
  165.     }
  166.     #[Route('/verify/email'name'app_verify_email')]
  167.     public function verifyUserEmail(Request $request): Response
  168.     {
  169.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  170.         // validate email confirmation link, sets User::isVerified=true and persists
  171.         try {
  172.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  173.         } catch (VerifyEmailExceptionInterface $exception) {
  174.             $this->addFlash('verify_email_error'$exception->getReason());
  175.             return $this->redirectToRoute('app_register');
  176.         }
  177.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  178.         $this->addFlash('success''Your email address has been verified.');
  179.         return $this->redirectToRoute('app_register');
  180.     }
  181. }