src/EventSubscriber/Metier/ObjectSubscriber.php line 351

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Metier;
  3. use App\AppEvents;
  4. use App\Constant\EmailConfiguration;
  5. use App\Entity\Metier\Actualite;
  6. use App\Entity\Metier\Agence;
  7. use App\Entity\Metier\AssureSante;
  8. use App\Entity\Metier\AvisPublic;
  9. use App\Entity\Metier\Contrat;
  10. use App\Entity\Metier\Fichier;
  11. use App\Entity\Metier\ImportData;
  12. use App\Entity\Metier\Message;
  13. use App\Entity\Metier\Produit;
  14. use App\Entity\Metier\Service;
  15. use App\Entity\Metier\Sinistre;
  16. use App\Entity\Metier\SinistreSante;
  17. use App\Entity\User\Admin;
  18. use App\Entity\User\Partenaire;
  19. use App\Helpers\ArrayUtils;
  20. use App\Helpers\UserTypeConst;
  21. use App\ObjectManager\Event\ObjectEvent;
  22. use App\Service\Firebase\DoPush;
  23. use App\Service\User\ExpertUserManager;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  26. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  27. use Symfony\Component\HttpKernel\KernelEvents;
  28. use Symfony\Component\Mailer\MailerInterface;
  29. use Symfony\Component\Mime\Address;
  30. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  31. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  33. class ObjectSubscriber implements EventSubscriberInterface
  34. {
  35.     private $em;
  36.     private $token;
  37.     private $doPush;
  38.     private $usermanager;
  39.     private $mailer;
  40.     private $dispatcher;
  41.     public function __construct(
  42.         EntityManagerInterface $em,
  43.         TokenStorageInterface $token,
  44.         ExpertUserManager $usermanager,
  45.         MailerInterface $mailer,
  46.         EventDispatcherInterface $dispatcher,
  47.         DoPush $doPush)
  48.     {
  49.         $this->em $em;
  50.         $this->token $token;
  51.         $this->doPush $doPush;
  52.         $this->usermanager $usermanager;
  53.         $this->mailer $mailer;
  54.         $this->dispatcher $dispatcher;
  55.     }
  56.     public function onObjectValidation(ObjectEvent $event)
  57.     {
  58.         $object $event->getObj();
  59.         $request $event->getRequest();
  60.         $extra $d array_map('trim'$event->getExtra());
  61.         $timezone = new \DateTimeZone('UTC');
  62.         $class get_class($object);
  63.         switch ($class) {
  64.             case Agence::class:
  65.                 $type $this->em->getRepository('Metier:TypeAgence')->find((int)$request->get('type_agence'));
  66.                 $object->setTypeAgence($type);
  67.                 break;
  68.             case Produit::class:
  69.                 $type $this->em->getRepository('Metier:TypeProduit')->find((int)$request->get('type_produit'));
  70.                 $dateLancement = new \DateTime($request->get('dateLancement'));
  71.                 $object->setDateLancement($dateLancement)
  72.                     ->setLibelle($request->get('libelle'))
  73.                     ->setDescription($request->get('description'))
  74.                     ->setTypeProduit($type)
  75.                     ->setUploadFile($request->files->get('image'));
  76.                 break;
  77.             case Actualite::class:
  78.                 $dateLancement = new \DateTime($request->get('dateLancement'));
  79.                 $object->setDateLancement($dateLancement)
  80.                     ->setTitre($request->get('titre'))
  81.                     ->setContenu($request->get('contenu'))
  82.                     ->setFlash(($request->get('flash') === 'on'))
  83.                     ->setLaune(($request->get('laune') === 'on'))
  84.                     ->setUploadFile($request->files->get('image'));
  85.                 break;
  86.             case AvisPublic::class:
  87.                 /** @var AvisPublic $object */
  88.                 if($request->files->has('uploadFile')){
  89.                     $object->setUploadFile($request->files->get('uploadFile'));
  90.                 }
  91.                 break;
  92.             case Contrat::class:
  93.                 $client $this->setClient($extra);
  94.                 if (!$client['tmp']) {
  95.                     $object->setClient($client['client']);
  96.                 } else {
  97.                     $object->setTmpClient($client['client']);
  98.                 }
  99.                 break;
  100.             case Sinistre::class:
  101.                 $contrat $this->em->getRepository('Metier:Contrat')->findOneBy(['numPolice' => $extra['numPolice']]);
  102.                 $dateDeclaration ArrayUtils::niceDateTimeFromString(array_key_exists('dateDeclaration'$extra) ? $extra['dateDeclaration'] : null);
  103.                 $dateEcheance ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance'$extra) ? $extra['dateEcheance'] : null);
  104.                 $dateEffet ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet'$extra) ? $extra['dateEffet'] : null);
  105.                 $dateEmission ArrayUtils::niceDateTimeFromString(array_key_exists('dateEmission'$extra) ? $extra['dateEmission'] : null);
  106.                 $dateSurvenance ArrayUtils::niceDateTimeFromString(array_key_exists('dateSurvenance'$extra) ? $extra['dateSurvenance'] : null);
  107.                 $object->setContrat($contrat)
  108.                     ->setDateDeclaration($dateDeclaration $dateDeclaration null)
  109.                     ->setDateEcheance($dateEcheance $dateEcheance null)
  110.                     ->setDateEffet($dateEffet $dateEffet null)
  111.                     ->setDateEmission($dateEmission $dateEmission null)
  112.                     ->setDateSurvenance($dateSurvenance $dateSurvenance null);
  113.                 break;
  114.             case SinistreSante::class:
  115.                 /** @var SinistreSante $object */
  116.                 $contrat $this->em->getRepository('Metier:Contrat')->findOneBy(['numPolice' => $extra['numPolice']]);
  117.                 $dateEcheance ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance'$extra) ? $extra['dateEcheance'] : null);
  118.                 $dateEffet ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet'$extra) ? $extra['dateEffet'] : null);
  119.                 $object->setIdentifiant($extra['numPolice'].$extra['dateEffet'].$extra['dateEcheance'])
  120.                     ->setContrat($contrat)
  121.                     ->setDateEcheance($dateEcheance)
  122.                     ->setDateEffet($dateEffet);
  123.                 break;
  124.             case AssureSante::class:
  125.                 /** @var AssureSante $object */
  126.                 $dateEcheance ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance'$extra) ? $extra['dateEcheance'] : null);
  127.                 $dateEffet ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet'$extra) ? $extra['dateEffet'] : null);
  128.                 $dateNaissance ArrayUtils::niceDateTimeFromString(array_key_exists('dateNaissance'$extra) ? $extra['dateNaissance'] : null);
  129.                 $object->setDateNaissance($dateNaissance)->setDateEffet($dateEffet)->setDateEcheance($dateEcheance);
  130.                 break;
  131.             case Fichier::class:
  132.                 $public $request->get('public');
  133.                 $object->setUploadFile($request->files->get('uploadFile'))
  134.                     ->setLibelle($request->get('libelle'))
  135.                     ->setPublic($public);
  136.                 if (!$public) {
  137.                     $client $this->em->getRepository('User:Client')->find((int)$request->get('client'));
  138.                     $object->setClient($client);
  139.                 }
  140.                 break;
  141.             case Service::class:
  142.                 $object->setUploadFile($request->files->get('uploadFile'))
  143.                     ->setDateLancement(new \DateTime($request->get('dateLancement')));
  144.                 break;
  145.             case \App\Entity\Metier\Institution::class:
  146.                 $last $this->em->getRepository('Metier:Institution')->getLastInfo();
  147.                 if (is_null($last))
  148.                     $object->setOrdre(1);
  149.                 else
  150.                     $object->setOrdre($last->getOrdre() + 1);
  151.                 break;
  152.             case Message::class:
  153.                 $user $this->usermanager->getConnectedUser();
  154.                 if ($this->usermanager->isConnected()) {
  155.                     $object->setExpediteur($user);
  156.                 }
  157.                 if (!is_null($user) && $user instanceof \App\Entity\User\User) {
  158.                     $object->setNom($user->getFullname())
  159.                         ->setEmail($user->getEmail())
  160.                         ->setTelephone($user->getTel());
  161.                 }
  162.                 if ($user instanceof \App\Entity\User\Commercial OR $user instanceof \App\Entity\User\Admin) {
  163.                     // le message constitue une reponse
  164.                     $object->setReponse(TRUE);
  165.                 }
  166.                 break;
  167.             case ImportData::class:
  168.                 /** @var ImportData $object */
  169.                 $date = new \DateTime();
  170.                 $startAt =  $date->setTimestamp($request->get('startAt'));
  171.                 $endAt $date->setTimestamp($request->get('endAt'));
  172.                 $user $this->usermanager->getConnectedUser();
  173.                 dump($user);
  174.                 $object->setStartAt($startAt)->setEndAt($endAt)->setCreateBy($user);
  175.                 dump($object);
  176.                 break;
  177.         }
  178.     }
  179.     public function onObjectCreated(ObjectEvent $event)
  180.     {
  181.         $object $event->getObj();
  182.         $request $event->getRequest();
  183.         $class get_class($object);
  184.         switch ($class) {
  185.             case \App\Entity\Metier\Message::class:
  186.                 $extra = ['type' => UserTypeConst::TYPE_MESSAGERIE'method' => UserTypeConst::METHOD_ADD'id' => $object->getId(), 'date' => $object->getCreateAtTimeStamp()];
  187.                 $this->doPush->setTitle('myFidelia')->setMessage($object->getMessage());
  188.                 if ($request->get('diffusion')) {
  189.                     $extra['diffusion'] = TRUE;
  190.                     $this->doPush->setExtra($extra)->doDiffusion();
  191.                 }
  192.                 if ($object->getRecepteur() instanceof \App\Entity\User\Client && !is_null($object->getRecepteur())) {
  193.                     // message destine a un client en particulier
  194.                     $client $object->getRecepteur();
  195.                     $expediteur $object->getExpediteur();
  196.                     $extra['diffusion'] = FALSE;
  197.                     $this->doPush->setNumClient($client->getNumClient())->setTelepohne($client->getNumClient())->setExtra($extra)->doPush();
  198.                     $oldmessages $this->em->getRepository('Metier:Message')->findBy(['expediteur' => $client->getId(), 'repondu' => null]);
  199.                     foreach ($oldmessages as $message) {
  200.                         $message->setRepondu($expediteur);
  201.                         $this->em->flush();
  202.                     }
  203.                 }
  204.                 //dump($extra);
  205.                 break;
  206.             case \App\Entity\Metier\Avenant::class:
  207.                 $contrat $object->getContrat();
  208.                 $extra = ['type' => UserTypeConst::TYPE_CONTRAT'method' => UserTypeConst::METHOD_UPDATE'id' => $contrat->getId()];
  209.                 $client $contrat->getClient();
  210.                 if (!is_null($client)) {
  211.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(TRUE)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  212.                 }
  213.                 break;
  214.             case AvisPublic::class:
  215.                 /** @var AvisPublic $object */
  216.                 $email = (new TemplatedEmail())
  217.                     ->from(new Address(EmailConfiguration::SENDER_EMAILEmailConfiguration::SENDER_NAME))
  218.                     ->to(EmailConfiguration::SUPPORT_MAIL)
  219.                     ->subject($object->getObjet())
  220.                     ->context([
  221.                         'fullname' => $object->getNom(),
  222.                         'mail' => $object->getEmail(),
  223.                         'telephone' => $object->getTelephone(),
  224.                         'objet' => $object->getObjet(),
  225.                         'message' => $object->getMessage()
  226.                     ])
  227.                     ->htmlTemplate(EmailConfiguration::AVIS_EMAIL);
  228.                 if(!is_null($object->getFichier())) $email->attachFromPath($object->getFichier()->getWebPath());
  229.                 $autoResponseMail =  null;
  230.                 if(!is_null($object->getEmail())){
  231.                     $autoResponseMail = (new TemplatedEmail())
  232.                         ->from(new Address(EmailConfiguration::SENDER_EMAILEmailConfiguration::SENDER_NAME))
  233.                         ->to($object->getEmail())
  234.                         ->subject('RE: '.$object->getObjet())
  235.                         ->context([
  236.                             'fullname' => $object->getNom(),
  237.                         ])
  238.                         ->htmlTemplate(EmailConfiguration::AVIS_RESPONSE_EMAIL);
  239.                 }
  240.                 $this->dispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) use ($email$autoResponseMail) {
  241.                     $this->mailer->send($email);
  242.                     if(!is_null($autoResponseMail)) $this->mailer->send($autoResponseMail);
  243.                 });
  244.                 break;
  245.         }
  246.     }
  247.     public function onObjectUpdated(ObjectEvent $event)
  248.     {
  249.         $object $event->getObj();
  250.         $class get_class($object);
  251.         $extra = [];
  252.         $notify false;
  253.         $diffusion true;
  254.         switch ($class) {
  255.             case \App\Entity\Metier\Actualite::class:
  256.                 $extra['type'] = UserTypeConst::TYPE_ACTUALITE;
  257.                 $notify true;
  258.                 break;
  259.             case \App\Entity\Metier\Agence::class:
  260.                 $extra['type'] = UserTypeConst::TYPE_AGENCE;
  261.                 $notify true;
  262.                 break;
  263.             case \App\Entity\Metier\Institution::class:
  264.                 $extra['type'] = UserTypeConst::TYPE_COMPAGNIE;
  265.                 $notify true;
  266.                 break;
  267.             case \App\Entity\Metier\Produit::class:
  268.                 $extra['type'] = UserTypeConst::TYPE_PRODUIT;
  269.                 $notify true;
  270.                 break;
  271.             case \App\Entity\Metier\Service::class:
  272.                 $extra['type'] = UserTypeConst::TYPE_SERVICE;
  273.                 $notify true;
  274.                 break;
  275.             case \App\Entity\Metier\Faq::class:
  276.                 $extra['type'] = UserTypeConst::TYPE_FAQ;
  277.                 $notify true;
  278.                 break;
  279.             case \App\Entity\Admin\Parametre::class:
  280.                 if ($object->getCle() === UserTypeConst::PARAMS_CONFIDENTIAL) {
  281.                     $extra['type'] = UserTypeConst::TYPE_CONFIDENTIALITE;
  282.                     $notify true;
  283.                 }
  284.                 break;
  285.             case Contrat::class:
  286.                 $extra['type'] = UserTypeConst::TYPE_CONTRAT;
  287.                 $extra['method'] = UserTypeConst::METHOD_UPDATE;
  288.                 $extra['id'] = $object->getId();
  289.                 $notify true;
  290.                 $diffusion false;
  291.                 $client $object->getClient();
  292.                 if (!is_null($client)) {
  293.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  294.                 }
  295.                 break;
  296.             case Sinistre::class:
  297.                 /** @var Sinistre $object */
  298.                 $extra['type'] = UserTypeConst::TYPE_SINISTRE;
  299.                 $extra['method'] = UserTypeConst::METHOD_UPDATE;
  300.                 $extra['id'] = $object->getId();
  301.                 $notify true;
  302.                 $diffusion false;
  303.                 $client $object->getContrat()->getClient();
  304.                 if (!is_null($client)) {
  305.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  306.                 }
  307.                 break;
  308.             case SinistreSante::class:
  309.                 /** @var SinistreSante $object */
  310.                 $extra['type'] = UserTypeConst::TYPE_SINISTRE_SANTE;
  311.                 $extra['method'] = UserTypeConst::METHOD_UPDATE;
  312.                 $extra['id'] = $object->getId();
  313.                 $notify true;
  314.                 $diffusion false;
  315.                 $client $object->getContrat()->getClient();
  316.                 if (!is_null($client)) {
  317.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  318.                 }
  319.                 break;
  320.                 case AssureSante::class:
  321.                     /** @var AssureSante $object */
  322.                 $extra['type'] = UserTypeConst::TYPE_ASSURE_SANTE;
  323.                 $extra['method'] = UserTypeConst::METHOD_UPDATE;
  324.                 $extra['id'] = $object->getId();
  325.                 $this->doPush->setBackend(TRUE)->setExtra($extra)->pushClassUser(Partenaire::class);
  326.                 break;
  327.         }
  328.         if ($notify && $diffusion) {
  329.             $extra['method'] = UserTypeConst::METHOD_UPDATE;
  330.             $extra['id'] = $object->getId();
  331.             $this->doPush->setTitle('Title')->setMessage('Message')->setBackend(true)->setExtra($extra)->doDiffusion();
  332.         }
  333.     }
  334.     public function onObjectRemoved(ObjectEvent $event)
  335.     {
  336.         $object $event->getObj();
  337.         $class get_class($object);
  338.         $extra = [];
  339.         $notify false;
  340.         $diffusion true;
  341.         switch ($class) {
  342.             case \App\Entity\Metier\Actualite::class:
  343.                 $extra['type'] = UserTypeConst::TYPE_ACTUALITE;
  344.                 $notify true;
  345.                 break;
  346.             case \App\Entity\Metier\Agence::class:
  347.                 $extra['type'] = UserTypeConst::TYPE_AGENCE;
  348.                 $notify true;
  349.                 break;
  350.             case \App\Entity\Metier\Institution::class:
  351.                 $extra['type'] = UserTypeConst::TYPE_COMPAGNIE;
  352.                 $notify true;
  353.                 break;
  354.             case \App\Entity\Metier\Produit::class:
  355.                 $extra['type'] = UserTypeConst::TYPE_PRODUIT;
  356.                 $notify true;
  357.                 break;
  358.             case \App\Entity\Metier\Service::class:
  359.                 $extra['type'] = UserTypeConst::TYPE_SERVICE;
  360.                 $notify true;
  361.                 break;
  362.             case \App\Entity\Metier\Faq::class:
  363.                 $extra['type'] = UserTypeConst::TYPE_FAQ;
  364.                 $notify true;
  365.                 break;
  366.             case \App\Entity\Metier\Fichier::class:
  367.                 $extra['type'] = UserTypeConst::TYPE_FICHIER;
  368.                 $notify true;
  369.                 break;
  370.             case Contrat::class:
  371.                 $extra['type'] = UserTypeConst::TYPE_CONTRAT;
  372.                 $notify true;
  373.                 $diffusion false;
  374.                 $client $object->getClient();
  375.                 if (!is_null($client)) {
  376.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  377.                 }
  378.                 break;
  379.             case Sinistre::class:
  380.                 $extra['type'] = UserTypeConst::TYPE_SINISTRE;
  381.                 $notify true;
  382.                 $diffusion false;
  383.                 $client $object->getContrat()->getClient();
  384.                 if (!is_null($client)) {
  385.                     $this->doPush->setNumClient($client->getNumClient())->setBackend(true)->setExtra($extra)->setTelepohne($client->getNumClient())->doPush();
  386.                 }
  387.                 break;
  388.             case AssureSante::class:
  389.                 $extra['type'] = UserTypeConst::TYPE_ASSURE_SANTE;
  390.                 $extra['method'] = UserTypeConst::METHOD_DELETE;
  391.                 $extra['id'] = $object->getId();
  392.                 return $this->doPush->setBackend(TRUE)->setExtra($extra)->pushClassUser(Partenaire::class);
  393.         }
  394.         if ($notify && $diffusion) {
  395.             $extra['method'] = UserTypeConst::METHOD_DELETE;
  396.             $extra['id'] = $object->getId();
  397.             $this->doPush->setBackend(true)->setExtra($extra)->doDiffusion();
  398.         }
  399.     }
  400.     public function onObjectUpdating(ObjectEvent $event)
  401.     {
  402.         $object $event->getObj();
  403.         $request $event->getRequest();
  404.         $class get_class($object);
  405.         switch ($class) {
  406.             case Actualite::class:
  407.                 $maquette $request->files->get('maquette');
  408.                 $object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
  409.                 break;
  410.             case Produit::class:
  411.                 $maquette $request->files->get('maquette');
  412.                 $object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
  413.                 break;
  414.             case Service::class:
  415.                 $maquette $request->files->get('maquette');
  416.                 $object->setUpdateAt(new \DateTime())->setUploadFile($maquette);
  417.                 break;
  418.             case Contrat::class :
  419.                 //dump($object);
  420.                 break;
  421.             case SinistreSante::class :
  422.                 $extra $request->request->all();
  423.                 $dateEcheance ArrayUtils::niceDateTimeFromString(array_key_exists('dateEcheance'$extra) ? $extra['dateEcheance'] : null);
  424.                 $dateEffet ArrayUtils::niceDateTimeFromString(array_key_exists('dateEffet'$extra) ? $extra['dateEffet'] : null);
  425.                 $object->setUpdateAt(new \DateTime())->setDateEffet($dateEffet)->setDateEcheance($dateEcheance);
  426.                 break;
  427.             case Sinistre::class:
  428.                 /** @var Sinistre $object */
  429.                 $extra $request->request->all();
  430.                 break;
  431.                 case AssureSante::class:
  432.                 /** @var AssureSante $object */
  433.                 $extra $request->request->all();
  434.                 $dateNaissance ArrayUtils::dateTimeFromTimeStamp(array_key_exists('date'$extra) ? $extra['date'] : null);
  435.                 $object->setDateNaissance($dateNaissance);
  436.                 break;
  437.         }
  438.         //dump($object);
  439.     }
  440.     public static function getSubscribedEvents()
  441.     {
  442.         return [
  443.             AppEvents::OBJECT_VALIDATING => 'onObjectValidation',
  444.             AppEvents::OBJECT_CREATED => 'onObjectCreated',
  445.             AppEvents::OBJECT_UPDATED => 'onObjectUpdated',
  446.             AppEvents::OBJECT_UPDATING => 'onObjectUpdating',
  447.             AppEvents::OBJECT_REMOVED => 'onObjectRemoved',
  448.         ];
  449.     }
  450.     private function setClient($extra)
  451.     {
  452.         // Check if client already exist
  453.         $client $this->em->getRepository('User:Client')->findOneBy(['numClient' => $extra['numClient']]);
  454.         if (!is_null($client))
  455.             return ['tmp' => false'client' => $client];
  456.         // check if client exist in tmp table
  457.         $client $this->em->getRepository('Metier:TmpClient')->findOneBy(['numClient' => $extra['numClient']]);
  458.         if (!is_null($client))
  459.             return ['tmp' => true'client' => $client];
  460.         // Set Tel to numClient when tel is null
  461.         if (array_key_exists('numTel'$extra) && $extra['numTel'] !== '') {
  462.             $array_tel explode('/'$extra['numTel']);
  463.         } else {
  464.             $array_tel = [$extra['numClient']];
  465.         }
  466.         $client = new \App\Entity\Metier\TmpClient();
  467.         $client->setNumClient($extra['numClient'])
  468.             ->setNom($extra['souscripteur'])
  469.             ->setTelephone(str_replace([' ''-'], [''''], $array_tel[0]));
  470.         if (sizeof($array_tel) > 1) {
  471.             $client->setTelephone2(str_replace([' ''-'], [''''], $array_tel[1]));
  472.         }
  473.         $this->em->persist($client);
  474.         $this->em->flush();
  475.         return ['tmp' => true'client' => $client];
  476.     }
  477. }