src/Controller/RegistrationController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\RequestModification;
  4. use App\Entity\Users;
  5. use App\Repository\RequestModificationStatusRepository;
  6. use App\Service\MailerService;
  7. use App\Repository\RequestModificationRepository;
  8. use App\Repository\TVARepository;
  9. use App\Repository\UsersRepository;
  10. use App\Form\RegistrationFormType;
  11. use App\Security\AppCustomAuthenticator;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. class RegistrationController extends AbstractController
  22. {
  23.     private $mailerService;
  24.     /**
  25.      * @var Status|MailerService
  26.      */
  27.     public function __construct(
  28.         RequestModificationRepository $requestModificationRepository,
  29.         MailerService $mailerService,
  30.         TVARepository $TVARepository,
  31.         RequestModificationStatusRepository $requestModificationStatusRepository,
  32.         UsersRepository $usersRepository,
  33.         EntityManagerInterface $entityManager
  34.     )
  35.     {
  36.         $this->requestModificationRepository $requestModificationRepository;
  37.         $this->mailerService $mailerService;
  38.         $this->TVARepository $TVARepository;
  39.         $this->requestModificationStatusRepository $requestModificationStatusRepository;
  40.         $this->usersRepository $usersRepository;
  41.         $this->entityManager $entityManager;
  42.     }
  43.     /**
  44.      * @Route("/register", name="app_register")
  45.      */
  46.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppCustomAuthenticator $authenticator): Response
  47.     {
  48.         $user = new Users();
  49.         $form $this->createForm(RegistrationFormType::class, $user);
  50.         $form->handleRequest($request);
  51.         if ($form->isSubmitted() && $form->isValid()) {
  52.             $foundUser $this->usersRepository->findOneBy(['deleted' => false'email' => $user->getEmail()]);
  53.             if($foundUser){
  54.                 $this->addFlash('error''Cet e-mail est déjà utilisé. Veuillez en choisir un autre.');
  55.                 // Rediriger ou rendre la vue du formulaire avec l'erreur
  56.                 return $this->render('registration/register.html.twig', [
  57.                     'registrationForm' => $form->createView(),
  58.                 ]);
  59.             }
  60.             $tvaStatus $form->get('tvaStatus')->getData();
  61.             $user->setPassword(
  62.                 $userPasswordHasher->hashPassword(
  63.                     $user,
  64.                     $form->get('plainPassword')->getData()
  65.                 )
  66.             );
  67.             $randomHex bin2hex(random_bytes(16));
  68.             $timestamp time();
  69.             $token $randomHex $timestamp;
  70.             $shuffledToken str_shuffle($token);
  71.             $token substr($shuffledToken032);
  72.             $user->setToken($token);
  73.             $user->setValidated(true);
  74.             $this->entityManager->persist($user);
  75.             $this->entityManager->flush();
  76.             $user->setEmailValidationToken($this->sendEmailVerification($user));
  77.             $this->sendRequest($request->get('isExistingEstablishment'),
  78.                 $request->get('establishments'),
  79.                 $user,
  80.                 $request->get('establishmentsAddress'),
  81.                 $request->get('description'),
  82.                 $tvaStatus);
  83.             return $this->render('registration/register-success.html.twig');
  84.         }
  85.         else if($form->isSubmitted() && !$form->isValid()){
  86.             $this->addFlash('error''Veuillez vérifier les informations saisies.');
  87.             return $this->render('registration/register.html.twig', [
  88.                 'registrationForm' => $form->createView(),
  89.                 'registrationFormSubmitted' => 1,
  90.                 'existing' => $request->get('isExistingEstablishment')
  91.             ]);
  92.         }
  93.         return $this->render('registration/register.html.twig', [
  94.             'registrationForm' => $form->createView(),
  95.             'registrationFormSubmitted' => 0,
  96.             'existing' => 0,
  97.         ]);
  98.     }
  99.     public function sendEmailVerification(Users $user){
  100.         $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  101.         $charactersLength strlen($characters);
  102.         $token '';
  103.         for ($i 0$i 50$i++) {
  104.             $token .= $characters[rand(0$charactersLength 1)];
  105.         }
  106.         $params = [
  107.             'link' => $_ENV['DOMAIN_EMAIL'].'email-verification/'.$token
  108.         ];
  109.         $this->mailerService->load(
  110.             $params,
  111.             'd-27c384389d1a47169fc54a18f87199b3',
  112.             $user
  113.         );
  114.         return $token;
  115.     }
  116.     public function sendRequest(string $newstring $establishmentUsers $user, ?string $address, ?string $descriptionstring $tva)
  117.     {
  118.         $form = new RequestModification();
  119.         $form->setCreatedBy($user);
  120.         $form->setType(2);
  121.         $form->setResponsable($user->getMyResponsable());
  122.         $form->setTvaNumber($user->getTvaNumber());
  123.         $form->setCompanyName($establishment);
  124.         $form->setTvaSubject($this->TVARepository->find($tva));
  125.         $form->setRequestModificationStatus($this->requestModificationStatusRepository->find(2));
  126.         if ($new === "false") {
  127.             $form->setTitle('Demande de création');
  128.             $form->setText(
  129.                 'Pouvez-vous créer mon établissement : ' $establishment .
  130.                 '<br>Adresse : ' $address .
  131.                 ',<br>Numéro de TVA : ' $user->getTvaNumber() .
  132.                 '<br>Description : ' $description
  133.             );
  134.         } else {
  135.             $form->setTitle('Demande de liaison');
  136.             $form->setText('Pouvez-vous me lier à mon établissement : ' $establishment);
  137.         }
  138.         // Si tu veux persister ce formulaire, ajoute ici le persist/flush avec l'EntityManager
  139.          $this->entityManager->persist($form);
  140.         $this->entityManager->flush();
  141.     }
  142. }