<?php
namespace App\EventSubscriber\Metier;
use App\AppEvents;
use App\Constant\EmailConfiguration;
use App\Entity\Metier\Actualite;
use App\Entity\Metier\Agence;
use App\Entity\Metier\AssureSante;
use App\Entity\Metier\AvisPublic;
use App\Entity\Metier\Contrat;
use App\Entity\Metier\Fichier;
use App\Entity\Metier\ImportData;
use App\Entity\Metier\Message;
use App\Entity\Metier\Produit;
use App\Entity\Metier\Service;
use App\Entity\Metier\Sinistre;
use App\Entity\Metier\SinistreSante;
use App\Entity\User\Admin;
use App\Entity\User\Partenaire;
use App\Helpers\ArrayUtils;
use App\Helpers\UserTypeConst;
use App\ObjectManager\Event\ObjectEvent;
use App\Service\Firebase\DoPush;
use App\Service\User\ExpertUserManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
class ObjectSubscriber implements EventSubscriberInterface
{
private $em;
private $token;
private $doPush;
private $usermanager;
private $mailer;
private $dispatcher;
public function __construct(
EntityManagerInterface $em,
TokenStorageInterface $token,
ExpertUserManager $usermanager,
MailerInterface $mailer,
EventDispatcherInterface $dispatcher,
DoPush $doPush)
{
$this->em = $em;
$this->token = $token;
$this->doPush = $doPush;
$this->usermanager = $usermanager;
$this->mailer = $mailer;
$this->dispatcher = $dispatcher;
}
public function onObjectValidation(ObjectEvent $event)
{
$object = $event->getObj();
$request = $event->getRequest();
$extra = $d = array_map('trim', $event->getExtra());
$timezone = new \DateTimeZone('UTC');
$class = get_class($object);
switch ($class) {
case Agence::class:
$type = $this->em->getRepository('Metier:TypeAgence')->find((int)$request->get('type_agence'));
$object->setTypeAgence($type);
break;
case Produit::class:
$type = $this->em->getRepository('Metier:TypeProduit')->find((int)$request->get('type_produit'));
$dateLancement = new \DateTime($request->get('dateLancement'));
$object->setDateLancement($dateLancement)
->setLibelle($request->get('libelle'))
->setDescription($request->get('description'))
->setTypeProduit($type)
->setUploadFile($request->files->get('image'));
break;
case Actualite::class:
$dateLancement = new \DateTime($request->get('dateLancement'));
$object->setDateLancement($dateLancement)
->setTitre($request->get('titre'))
->setContenu($request->get('contenu'))
->setFlash(($request->get('flash') === 'on'))
->setLaune(($request->get('laune') === 'on'))
->setUploadFile($request->files->get('image'));
break;
case AvisPublic::class:
/** @var AvisPublic $object */
if($request->files->has('uploadFile')){
$object->setUploadFile($request->files->get('uploadFile'));
}
break;
case Contrat::class:
$client = $this->setClient($extra);
if (!$client['tmp']) {
$object->setClient($client['client']);
} else {
$object->setTmpClient($client['client']);
}
break;
case Sinistre::class:
$contrat = $this->em->getRepository('Metier:Contrat')->findOneBy(['numPolice' => $extra['numPolice']]);
$dateDeclaration = ArrayUtils::niceDateTimeFromString(array_key_exists('dateDeclaration', $extra) ? $extra['dateDeclaration'] : null);
$dateEcheance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance', $extra) ? $extra['dateEcheance'] : null);
$dateEffet = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet', $extra) ? $extra['dateEffet'] : null);
$dateEmission = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEmission', $extra) ? $extra['dateEmission'] : null);
$dateSurvenance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateSurvenance', $extra) ? $extra['dateSurvenance'] : null);
$object->setContrat($contrat)
->setDateDeclaration($dateDeclaration ? $dateDeclaration : null)
->setDateEcheance($dateEcheance ? $dateEcheance : null)
->setDateEffet($dateEffet ? $dateEffet : null)
->setDateEmission($dateEmission ? $dateEmission : null)
->setDateSurvenance($dateSurvenance ? $dateSurvenance : null);
break;
case SinistreSante::class:
/** @var SinistreSante $object */
$contrat = $this->em->getRepository('Metier:Contrat')->findOneBy(['numPolice' => $extra['numPolice']]);
$dateEcheance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance', $extra) ? $extra['dateEcheance'] : null);
$dateEffet = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet', $extra) ? $extra['dateEffet'] : null);
$object->setIdentifiant($extra['numPolice'].$extra['dateEffet'].$extra['dateEcheance'])
->setContrat($contrat)
->setDateEcheance($dateEcheance)
->setDateEffet($dateEffet);
break;
case AssureSante::class:
/** @var AssureSante $object */
$dateEcheance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance', $extra) ? $extra['dateEcheance'] : null);
$dateEffet = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet', $extra) ? $extra['dateEffet'] : null);
$dateNaissance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateNaissance', $extra) ? $extra['dateNaissance'] : null);
$object->setDateNaissance($dateNaissance)->setDateEffet($dateEffet)->setDateEcheance($dateEcheance);
break;
case Fichier::class:
$public = $request->get('public');
$object->setUploadFile($request->files->get('uploadFile'))
->setLibelle($request->get('libelle'))
->setPublic($public);
if (!$public) {
$client = $this->em->getRepository('User:Client')->find((int)$request->get('client'));
$object->setClient($client);
}
break;
case Service::class:
$object->setUploadFile($request->files->get('uploadFile'))
->setDateLancement(new \DateTime($request->get('dateLancement')));
break;
case \App\Entity\Metier\Institution::class:
$last = $this->em->getRepository('Metier:Institution')->getLastInfo();
if (is_null($last))
$object->setOrdre(1);
else
$object->setOrdre($last->getOrdre() + 1);
break;
case Message::class:
$user = $this->usermanager->getConnectedUser();
if ($this->usermanager->isConnected()) {
$object->setExpediteur($user);
}
if (!is_null($user) && $user instanceof \App\Entity\User\User) {
$object->setNom($user->getFullname())
->setEmail($user->getEmail())
->setTelephone($user->getTel());
}
if ($user instanceof \App\Entity\User\Commercial OR $user instanceof \App\Entity\User\Admin) {
// le message constitue une reponse
$object->setReponse(TRUE);
}
break;
case ImportData::class:
/** @var ImportData $object */
$date = new \DateTime();
$startAt = $date->setTimestamp($request->get('startAt'));
$endAt = $date->setTimestamp($request->get('endAt'));
$user = $this->usermanager->getConnectedUser();
dump($user);
$object->setStartAt($startAt)->setEndAt($endAt)->setCreateBy($user);
dump($object);
break;
}
}
public function onObjectCreated(ObjectEvent $event)
{
$object = $event->getObj();
$request = $event->getRequest();
$class = get_class($object);
switch ($class) {
case \App\Entity\Metier\Message::class:
$extra = ['type' => UserTypeConst::TYPE_MESSAGERIE, 'method' => UserTypeConst::METHOD_ADD, 'id' => $object->getId(), 'date' => $object->getCreateAtTimeStamp()];
$this->doPush->setTitle('myFidelia')->setMessage($object->getMessage());
if ($request->get('diffusion')) {
$extra['diffusion'] = TRUE;
$this->doPush->setExtra($extra)->doDiffusion();
}
if ($object->getRecepteur() instanceof \App\Entity\User\Client && !is_null($object->getRecepteur())) {
// message destine a un client en particulier
$client = $object->getRecepteur();
$expediteur = $object->getExpediteur();
$extra['diffusion'] = FALSE;
$this->doPush->setNumClient($client->getNumClient())->setTelepohne($client->getNumClient())->setExtra($extra)->doPush();
$oldmessages = $this->em->getRepository('Metier:Message')->findBy(['expediteur' => $client->getId(), 'repondu' => null]);
foreach ($oldmessages as $message) {
$message->setRepondu($expediteur);
$this->em->flush();
}
}
//dump($extra);
break;
case \App\Entity\Metier\Avenant::class:
$contrat = $object->getContrat();
$extra = ['type' => UserTypeConst::TYPE_CONTRAT, 'method' => UserTypeConst::METHOD_UPDATE, 'id' => $contrat->getId()];
$client = $contrat->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(TRUE)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case AvisPublic::class:
/** @var AvisPublic $object */
$email = (new TemplatedEmail())
->from(new Address(EmailConfiguration::SENDER_EMAIL, EmailConfiguration::SENDER_NAME))
->to(EmailConfiguration::SUPPORT_MAIL)
->subject($object->getObjet())
->context([
'fullname' => $object->getNom(),
'mail' => $object->getEmail(),
'telephone' => $object->getTelephone(),
'objet' => $object->getObjet(),
'message' => $object->getMessage()
])
->htmlTemplate(EmailConfiguration::AVIS_EMAIL);
if(!is_null($object->getFichier())) $email->attachFromPath($object->getFichier()->getWebPath());
$autoResponseMail = null;
if(!is_null($object->getEmail())){
$autoResponseMail = (new TemplatedEmail())
->from(new Address(EmailConfiguration::SENDER_EMAIL, EmailConfiguration::SENDER_NAME))
->to($object->getEmail())
->subject('RE: '.$object->getObjet())
->context([
'fullname' => $object->getNom(),
])
->htmlTemplate(EmailConfiguration::AVIS_RESPONSE_EMAIL);
}
$this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email, $autoResponseMail) {
$this->mailer->send($email);
if(!is_null($autoResponseMail)) $this->mailer->send($autoResponseMail);
});
break;
}
}
public function onObjectUpdated(ObjectEvent $event)
{
$object = $event->getObj();
$class = get_class($object);
$extra = [];
$notify = false;
$diffusion = true;
switch ($class) {
case \App\Entity\Metier\Actualite::class:
$extra['type'] = UserTypeConst::TYPE_ACTUALITE;
$notify = true;
break;
case \App\Entity\Metier\Agence::class:
$extra['type'] = UserTypeConst::TYPE_AGENCE;
$notify = true;
break;
case \App\Entity\Metier\Institution::class:
$extra['type'] = UserTypeConst::TYPE_COMPAGNIE;
$notify = true;
break;
case \App\Entity\Metier\Produit::class:
$extra['type'] = UserTypeConst::TYPE_PRODUIT;
$notify = true;
break;
case \App\Entity\Metier\Service::class:
$extra['type'] = UserTypeConst::TYPE_SERVICE;
$notify = true;
break;
case \App\Entity\Metier\Faq::class:
$extra['type'] = UserTypeConst::TYPE_FAQ;
$notify = true;
break;
case \App\Entity\Admin\Parametre::class:
if ($object->getCle() === UserTypeConst::PARAMS_CONFIDENTIAL) {
$extra['type'] = UserTypeConst::TYPE_CONFIDENTIALITE;
$notify = true;
}
break;
case Contrat::class:
$extra['type'] = UserTypeConst::TYPE_CONTRAT;
$extra['method'] = UserTypeConst::METHOD_UPDATE;
$extra['id'] = $object->getId();
$notify = true;
$diffusion = false;
$client = $object->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case Sinistre::class:
/** @var Sinistre $object */
$extra['type'] = UserTypeConst::TYPE_SINISTRE;
$extra['method'] = UserTypeConst::METHOD_UPDATE;
$extra['id'] = $object->getId();
$notify = true;
$diffusion = false;
$client = $object->getContrat()->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case SinistreSante::class:
/** @var SinistreSante $object */
$extra['type'] = UserTypeConst::TYPE_SINISTRE_SANTE;
$extra['method'] = UserTypeConst::METHOD_UPDATE;
$extra['id'] = $object->getId();
$notify = true;
$diffusion = false;
$client = $object->getContrat()->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case AssureSante::class:
/** @var AssureSante $object */
$extra['type'] = UserTypeConst::TYPE_ASSURE_SANTE;
$extra['method'] = UserTypeConst::METHOD_UPDATE;
$extra['id'] = $object->getId();
$this->doPush->setBackend(TRUE)->setExtra($extra)->pushClassUser(Partenaire::class);
break;
}
if ($notify && $diffusion) {
$extra['method'] = UserTypeConst::METHOD_UPDATE;
$extra['id'] = $object->getId();
$this->doPush->setTitle('Title')->setMessage('Message')->setBackend(true)->setExtra($extra)->doDiffusion();
}
}
public function onObjectRemoved(ObjectEvent $event)
{
$object = $event->getObj();
$class = get_class($object);
$extra = [];
$notify = false;
$diffusion = true;
switch ($class) {
case \App\Entity\Metier\Actualite::class:
$extra['type'] = UserTypeConst::TYPE_ACTUALITE;
$notify = true;
break;
case \App\Entity\Metier\Agence::class:
$extra['type'] = UserTypeConst::TYPE_AGENCE;
$notify = true;
break;
case \App\Entity\Metier\Institution::class:
$extra['type'] = UserTypeConst::TYPE_COMPAGNIE;
$notify = true;
break;
case \App\Entity\Metier\Produit::class:
$extra['type'] = UserTypeConst::TYPE_PRODUIT;
$notify = true;
break;
case \App\Entity\Metier\Service::class:
$extra['type'] = UserTypeConst::TYPE_SERVICE;
$notify = true;
break;
case \App\Entity\Metier\Faq::class:
$extra['type'] = UserTypeConst::TYPE_FAQ;
$notify = true;
break;
case \App\Entity\Metier\Fichier::class:
$extra['type'] = UserTypeConst::TYPE_FICHIER;
$notify = true;
break;
case Contrat::class:
$extra['type'] = UserTypeConst::TYPE_CONTRAT;
$notify = true;
$diffusion = false;
$client = $object->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case Sinistre::class:
$extra['type'] = UserTypeConst::TYPE_SINISTRE;
$notify = true;
$diffusion = false;
$client = $object->getContrat()->getClient();
if (!is_null($client)) {
$this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
}
break;
case AssureSante::class:
$extra['type'] = UserTypeConst::TYPE_ASSURE_SANTE;
$extra['method'] = UserTypeConst::METHOD_DELETE;
$extra['id'] = $object->getId();
return $this->doPush->setBackend(TRUE)->setExtra($extra)->pushClassUser(Partenaire::class);
}
if ($notify && $diffusion) {
$extra['method'] = UserTypeConst::METHOD_DELETE;
$extra['id'] = $object->getId();
$this->doPush->setBackend(true)->setExtra($extra)->doDiffusion();
}
}
public function onObjectUpdating(ObjectEvent $event)
{
$object = $event->getObj();
$request = $event->getRequest();
$class = get_class($object);
switch ($class) {
case Actualite::class:
$maquette = $request->files->get('maquette');
$object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
break;
case Produit::class:
$maquette = $request->files->get('maquette');
$object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
break;
case Service::class:
$maquette = $request->files->get('maquette');
$object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
break;
case Contrat::class :
//dump($object);
break;
case SinistreSante::class :
$extra = $request->request->all();
$dateEcheance = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance', $extra) ? $extra['dateEcheance'] : null);
$dateEffet = ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet', $extra) ? $extra['dateEffet'] : null);
$object->setUpdateAt(new \DateTime())->setDateEffet($dateEffet)->setDateEcheance($dateEcheance);
break;
case Sinistre::class:
/** @var Sinistre $object */
$extra = $request->request->all();
break;
case AssureSante::class:
/** @var AssureSante $object */
$extra = $request->request->all();
$dateNaissance = ArrayUtils::dateTimeFromTimeStamp(array_key_exists('date', $extra) ? $extra['date'] : null);
$object->setDateNaissance($dateNaissance);
break;
}
//dump($object);
}
public static function getSubscribedEvents()
{
return [
AppEvents::OBJECT_VALIDATING => 'onObjectValidation',
AppEvents::OBJECT_CREATED => 'onObjectCreated',
AppEvents::OBJECT_UPDATED => 'onObjectUpdated',
AppEvents::OBJECT_UPDATING => 'onObjectUpdating',
AppEvents::OBJECT_REMOVED => 'onObjectRemoved',
];
}
private function setClient($extra)
{
// Check if client already exist
$client = $this->em->getRepository('User:Client')->findOneBy(['numClient' => $extra['numClient']]);
if (!is_null($client))
return ['tmp' => false, 'client' => $client];
// check if client exist in tmp table
$client = $this->em->getRepository('Metier:TmpClient')->findOneBy(['numClient' => $extra['numClient']]);
if (!is_null($client))
return ['tmp' => true, 'client' => $client];
// Set Tel to numClient when tel is null
if (array_key_exists('numTel', $extra) && $extra['numTel'] !== '') {
$array_tel = explode('/', $extra['numTel']);
} else {
$array_tel = [$extra['numClient']];
}
$client = new \App\Entity\Metier\TmpClient();
$client->setNumClient($extra['numClient'])
->setNom($extra['souscripteur'])
->setTelephone(str_replace([' ', '-'], ['', ''], $array_tel[0]));
if (sizeof($array_tel) > 1) {
$client->setTelephone2(str_replace([' ', '-'], ['', ''], $array_tel[1]));
}
$this->em->persist($client);
$this->em->flush();
return ['tmp' => true, 'client' => $client];
}
}