src/Events/InscriptionSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\Events;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Repository\ApplicationRepository;
  5. use App\Repository\ParametrageRepository;
  6. use App\Repository\RegionRepository;
  7. use App\Service\AccountService;
  8. use App\Entity\Utilisateur;
  9. use App\Entity\Entreprise;
  10. use App\Repository\CommuneRepository;
  11. use App\Repository\EntrepriseRepository;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  18. use Symfony\Component\Serializer\SerializerInterface;
  19. use Symfony\Component\Uid\UuidV4;
  20. class InscriptionSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var UserPasswordHasherInterface
  24.      */
  25.     private $encoder;
  26.     /** @var EntityManagerInterface */
  27.     private $manager;
  28.     private $entrepriseRepository;
  29.     private $communeRepository;
  30.     private $accountService;
  31.     private $serializer;
  32.     public function __construct(UserPasswordHasherInterface $encoder,EntityManagerInterface $manager,EntrepriseRepository $entrepriseRepositoryCommuneRepository $communeRepositoryRegionRepository $regionRepository,ParametrageRepository $paramRepositoryAccountService $accountServiceApplicationRepository $applicationRepositorySerializerInterface $serializer)
  33.     {
  34.         $this->encoder=$encoder;
  35.         $this->manager $manager;
  36.         $this->entrepriseRepository $entrepriseRepository;
  37.         $this->communeRepository $communeRepository;
  38.         $this->regionRepository $regionRepository;
  39.         $this->paramRepository $paramRepository;
  40.         $this->applicationRepository $applicationRepository;
  41.         $this->accountService $accountService;
  42.         $this->serializer $serializer;
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.           KernelEvents::VIEW => ['encodePassword',EventPriorities::PRE_WRITE]
  48.         ];
  49.     }
  50.     public function encodePassword(ViewEvent $event)
  51.     {
  52.         $result $event->getControllerResult();
  53.         $method $event->getRequest()->getMethod();
  54.         if($result instanceof Utilisateur && $method == 'POST'){
  55.             $hash=$this->encoder->hashPassword($result,$result->getPassword());
  56.             $result->setPassword($hash);
  57.             //lire l'entreprise par rapport au siret
  58.             $entreprise $this->entrepriseRepository->findOneBy(array('siret' => $result->getSiretEntreprise(), 'actif' => true ));
  59.             if(!$entreprise){
  60.                 //Créer l'entreprise
  61.                 $entreprise = new Entreprise();
  62.                 //lire la commune
  63.                 $commune $this->communeRepository->findOneBy(array('id' => $result->getUuidCommune(), 'actif' => true));
  64.                 if(!$commune){
  65.                     $error['@context']="/api/contexts/ConstraintViolationList";
  66.                     $error['@type']="ConstraintViolationList";
  67.                     $error['hydra:title']="An error occurred";
  68.                     $error['hydra:description']="La commune n'existe pas";
  69.                     $error['violations'][0]['propertyPath']="uuidCommune";
  70.                     $error['violations'][0]['message']="La commune n'existe pas";
  71.                     $error['violations'][0]['code']=new UuidV4();
  72.                     //$event->setController(return() => new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
  73.                     $event->stopPropagation();
  74.                     //return new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
  75.                 }else {
  76.                     //Recherche code_insee region dans la bdd
  77.                     $param $this->paramRepository->findOneBy(array('code' => 'APP_CODE_REGION'));
  78.                     $regionId=$param->getValeur();
  79.                     $region $this->regionRepository->findOneBy(array('codeInsee' => $regionId'actif' => true));
  80.                     if(!$region){
  81.                         $error['@context']="/api/contexts/ConstraintViolationList";
  82.                         $error['@type']="ConstraintViolationList";
  83.                         $error['hydra:title']="An error occurred";
  84.                         $error['hydra:description']="La région n'existe pas";
  85.                         $error['violations'][0]['propertyPath']="configInterneRegion";
  86.                         $error['violations'][0]['message']="La région n'existe pas";
  87.                         $error['violations'][0]['code']=new UuidV4();
  88.                         $event->stopPropagation();
  89.                         //return new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
  90.                     }else {
  91.                         $entreprise->setActif(1)
  92.                             ->setSiret($result->getSiretEntreprise())
  93.                             ->setCommune($commune)
  94.                             ->setTypeEntreprise($result->getTypeEntreprise())
  95.                             ->setNom($result->getNomEntreprise())
  96.                             ->setRegion($region);
  97.                         if($result->getUuidSolution() != ''){
  98.                             $app $this->applicationRepository->findOneBy(array('id' => $result->getUuidSolution(), 'actif' => true));
  99.                             if($app){
  100.                                 $entreprise->setApplication($app);
  101.                             }
  102.                         }
  103.                         $this->manager->persist($entreprise);
  104.                         //Envoyer l'email d'activation de compte
  105.                         $this->accountService->reinitialisePassword($result);
  106.                     }
  107.                 }
  108.             }else{
  109.                 //Envoyer l'email d'avertissement qu'un compte vient d'être créé avec lien d'activation
  110.                 $this->accountService->avertirFirstAccountEntreprise($entreprise$result);
  111.             }
  112.             $result->setEntreprise($entreprise);
  113.         }
  114.     }
  115. }