src/Controller/InscriptionController.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use App\Form\UserPartnerInscriptionType;
  7. use App\Service\DynamicHostService;
  8. use App\Service\ContractService;
  9. use App\Entity\User;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use App\Event\SubContractor\SubContractorCompletedProfileEvent;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use App\Event\SubContractorUpdatedEvent;
  17. use App\Service\ConfidentialityService;
  18. use App\Form\UserAgencyType;
  19. use App\Event\ClientUpdatedEvent;
  20. class InscriptionController extends AbstractController
  21. {
  22.     public function __construct(
  23.         private ParameterBagInterface $parameter,private ConfidentialityService $confidentialityService
  24.     ){}
  25.     #[Route('/inscription/partner'name'app_inscription_partner')]
  26.     public function inscription(DynamicHostService $host,ContractService $contractService,Request $request,EntityManagerInterface $entityManagerInterface,UserPasswordHasherInterface $hash,EventDispatcherInterface $dispatcher): Response
  27.     {
  28.         $company $host->getCompany();
  29.         //redirection si MYFlow
  30.         if ($company == null) {
  31.              return $this->redirectToRoute('app_login');
  32.         }
  33.         //fin redirection
  34.         $user = new User();
  35.         $inscriptionForm =$this->createForm(UserPartnerInscriptionType::class,$user);
  36.         $inscriptionForm->handleRequest($request);
  37.         $errorMessages = [];
  38.         if ($inscriptionForm->isSubmitted() && !$inscriptionForm->isValid()) {
  39.             foreach ($inscriptionForm->getErrors(true) as $error) {
  40.                 $errorMessages[] = $error->getMessage();
  41.             }
  42.         }
  43.        
  44.         if($inscriptionForm->isSubmitted() && $inscriptionForm->isValid()){
  45.             $userData $inscriptionForm->get('userData')->getData();
  46.             $vigilenceCertificate $inscriptionForm->get('userData')->get('VigilanceCertificateFile')->getData();
  47.             $this->saveVigilenceCertificate($vigilenceCertificate,$user);
  48.             $user->setPassword($hash->hashPassword($user$user->getPassword()));
  49.             $user->setRoles(['ROLE_SUBCONTRACTOR']);
  50.             $user->setEnabled(true);
  51.             $user->setPartner(true);
  52.             $user->setCompany($company);
  53.             $user->setShareMyEmail(true);
  54.             $user->setShareMyPhone(true);
  55.             $resaleRate $user->getDailyRate() + ($user->getDailyRate() * 0.03);
  56.             $user->setResaleRate($resaleRate);
  57.             $entityManagerInterface->persist($user);
  58.             $entityManagerInterface->flush();
  59.             //ajout de resaleRate
  60.             //send all email and create contract
  61.             $event = new SubContractorCompletedProfileEvent($usertruetrue);
  62.             $dispatcher->dispatch($eventSubContractorCompletedProfileEvent::NAME);
  63.             $event = new SubContractorUpdatedEvent($userfalsetruefalse);
  64.             $dispatcher->dispatch($eventSubContractorUpdatedEvent::NAME);
  65.             $this->confidentialityService->addSignedContractSubcontractor($usertruetruetrue);
  66.             $this->addFlash('success'"Inscription fait avec succès");
  67.             return $this->render('inscription/confirmation.html.twig', [
  68.                 'company' => $company
  69.             ]);
  70.         }
  71.         //var_dump($error);
  72.         return $this->render('inscription/partner.html.twig', [
  73.             'inscriptionForm' => $inscriptionForm->createView(),
  74.             'cgv' => $contractService->getCGV(null$companyfalse),
  75.             'cgu' => $contractService->getCGU(null,$company,'subcontractor'false),
  76.             'company' => $company,
  77.             'errors' => $errorMessages
  78.         ]);
  79.     }
  80.     private function saveVigilenceCertificate($file,$user){
  81.         $path "file_user_directory";
  82.         if (!is_null($file)) {
  83.             $destination =  $this->parameter->get($path);
  84.             if (!is_dir($destination)) {
  85.                 mkdir($destination0755true);
  86.             }
  87.             $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  88.             $newFilename $originalFilename '-' uniqid() . '.' $file->guessExtension();
  89.             $file->move(
  90.                 $destination,
  91.                 $newFilename
  92.             );
  93.             //save file vigilence in data (name of file / date insertion)
  94.             $allUserFile  =  $user->getUserData()->getVigilanceCertificate();
  95.             $allUserFile [] = [
  96.                 'name' => $newFilename,
  97.                 'shortName' => mb_substr($newFilename040)."...",
  98.                 'dateInsert' => new \Datetime()
  99.             ];
  100.             if($user->getUserData()->getStateCompany() === 'fr'){
  101.                 $user->getUserData()->setVigilanceCertificate($allUserFile);
  102.                 $dateAddCertificate = new \DateTime();
  103.                 if(is_null($user->getUserData()->getVigilanceCertificate())){
  104.                     $user->getUserData()->setDateAddCertificate($dateAddCertificate);
  105.                     $user->getUserData()->setDateUpdateCertificate($dateAddCertificate);
  106.                 }else{
  107.                     $user->getUserData()->setDateUpdateCertificate($dateAddCertificate);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.     
  113.     #[Route('/creez-votre-compte-client-agence-{id}'name'dsfdsfqcreate_client')]
  114.     public function finalStepInscription(User $user,DynamicHostService $host,ContractService $contractService,Request $request,EntityManagerInterface $entityManagerInterface,UserPasswordHasherInterface $hash,EventDispatcherInterface $dispatcher): Response
  115.     {
  116.         if ($user->isEnabled()) {
  117.             return $this->redirectToRoute('app_login');
  118.         }
  119.         $company $host->getCompany();
  120.         $formInscription $this->createForm(UserAgencyType::class,$user);
  121.         $formInscription->handleRequest($request);
  122.         if($formInscription->isSubmitted() && $formInscription->isValid()){
  123.             $user->setEnabled(true);
  124.             $hashedPassword $hash->hashPassword($user$user->getPassword());
  125.             $user->setPassword($hashedPassword);    
  126.             $user->setShareMyEmail(true);
  127.             $user->setShareMyPhone(true);   
  128.             $entityManagerInterface->persist($user);
  129.             $entityManagerInterface->flush();
  130.             $event = new ClientUpdatedEvent($userfalse$user->getPassword(), true);
  131.             $dispatcher->dispatch($eventClientUpdatedEvent::NAME);
  132.             $this->confidentialityService->addSignedContractForClient($usertruefalsetrue);
  133.             return $this->render('inscription/confirmation_client.html.twig', [
  134.                 'company' => $company
  135.             ]);
  136.         }
  137.         return $this->render('inscription/finalisation_inscription.html.twig', [
  138.             'cgu' => $contractService->getCGU(null,$company,'subcontractor'false),
  139.             'company' => $company,
  140.             'inscriptionForm' => $formInscription->createView(),
  141.             'user' => $user
  142.         ]);
  143.     }
  144. }