<?php
namespace App\Events;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Repository\ApplicationRepository;
use App\Repository\ParametrageRepository;
use App\Repository\RegionRepository;
use App\Service\AccountService;
use App\Entity\Utilisateur;
use App\Entity\Entreprise;
use App\Repository\CommuneRepository;
use App\Repository\EntrepriseRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Uid\UuidV4;
class InscriptionSubscriber implements EventSubscriberInterface
{
/**
* @var UserPasswordHasherInterface
*/
private $encoder;
/** @var EntityManagerInterface */
private $manager;
private $entrepriseRepository;
private $communeRepository;
private $accountService;
private $serializer;
public function __construct(UserPasswordHasherInterface $encoder,EntityManagerInterface $manager,EntrepriseRepository $entrepriseRepository, CommuneRepository $communeRepository, RegionRepository $regionRepository,ParametrageRepository $paramRepository, AccountService $accountService, ApplicationRepository $applicationRepository, SerializerInterface $serializer)
{
$this->encoder=$encoder;
$this->manager = $manager;
$this->entrepriseRepository = $entrepriseRepository;
$this->communeRepository = $communeRepository;
$this->regionRepository = $regionRepository;
$this->paramRepository = $paramRepository;
$this->applicationRepository = $applicationRepository;
$this->accountService = $accountService;
$this->serializer = $serializer;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['encodePassword',EventPriorities::PRE_WRITE]
];
}
public function encodePassword(ViewEvent $event)
{
$result = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if($result instanceof Utilisateur && $method == 'POST'){
$hash=$this->encoder->hashPassword($result,$result->getPassword());
$result->setPassword($hash);
//lire l'entreprise par rapport au siret
$entreprise = $this->entrepriseRepository->findOneBy(array('siret' => $result->getSiretEntreprise(), 'actif' => true ));
if(!$entreprise){
//Créer l'entreprise
$entreprise = new Entreprise();
//lire la commune
$commune = $this->communeRepository->findOneBy(array('id' => $result->getUuidCommune(), 'actif' => true));
if(!$commune){
$error['@context']="/api/contexts/ConstraintViolationList";
$error['@type']="ConstraintViolationList";
$error['hydra:title']="An error occurred";
$error['hydra:description']="La commune n'existe pas";
$error['violations'][0]['propertyPath']="uuidCommune";
$error['violations'][0]['message']="La commune n'existe pas";
$error['violations'][0]['code']=new UuidV4();
//$event->setController(return() => new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
$event->stopPropagation();
//return new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
}else {
//Recherche code_insee region dans la bdd
$param = $this->paramRepository->findOneBy(array('code' => 'APP_CODE_REGION'));
$regionId=$param->getValeur();
$region = $this->regionRepository->findOneBy(array('codeInsee' => $regionId, 'actif' => true));
if(!$region){
$error['@context']="/api/contexts/ConstraintViolationList";
$error['@type']="ConstraintViolationList";
$error['hydra:title']="An error occurred";
$error['hydra:description']="La région n'existe pas";
$error['violations'][0]['propertyPath']="configInterneRegion";
$error['violations'][0]['message']="La région n'existe pas";
$error['violations'][0]['code']=new UuidV4();
$event->stopPropagation();
//return new JsonResponse($this->serializer->serialize($error, 'jsonld'), 422, [], true);
}else {
$entreprise->setActif(1)
->setSiret($result->getSiretEntreprise())
->setCommune($commune)
->setTypeEntreprise($result->getTypeEntreprise())
->setNom($result->getNomEntreprise())
->setRegion($region);
if($result->getUuidSolution() != ''){
$app = $this->applicationRepository->findOneBy(array('id' => $result->getUuidSolution(), 'actif' => true));
if($app){
$entreprise->setApplication($app);
}
}
$this->manager->persist($entreprise);
//Envoyer l'email d'activation de compte
$this->accountService->reinitialisePassword($result);
}
}
}else{
//Envoyer l'email d'avertissement qu'un compte vient d'être créé avec lien d'activation
$this->accountService->avertirFirstAccountEntreprise($entreprise, $result);
}
$result->setEntreprise($entreprise);
}
}
}