src/EventListener/WorkflowDossierSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3.  
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Twig\Extension\EscaperExtension;
  8. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  9. use App\Utils\MailerService;
  10. use App\Entity\Notification;
  11. use App\Entity\DocumentControle;
  12. use App\Event\WorkflowDossierEvent;
  13. use App\Event\NotifyEvent;
  14. class WorkflowDossierSubscriber implements EventSubscriberInterface
  15. {
  16.   private $mailer;
  17.   private $parameters;
  18.   private $em;
  19.   private $dispatcher;
  20.   private $templating;
  21.   
  22.   public function __construct(
  23.     EntityManagerInterface  $em
  24.     ParameterBagInterface $parameters
  25.     MailerService $mailer
  26.     EventDispatcherInterface $dispatcher
  27.     \Twig\Environment $templating
  28.   ) {
  29.       $this->mailer $mailer;
  30.       $this->parameters $parameters;
  31.       $this->em $em;
  32.       $this->dispatcher $dispatcher;
  33.       $this->templating $templating;
  34.   }
  35.   public static function getSubscribedEvents(): array
  36.   {
  37.       return [
  38.         WorkflowDossierEvent::NAME => 'notify'
  39.       ];
  40.   }
  41.  
  42.   public function notify(WorkflowDossierEvent $event) {
  43.     $dossier $event->getDossier();
  44.     $statut $event->getStatut();
  45.     $controles $dossier->getControles();
  46.     $motifs = [];
  47.     $motif_id null;
  48.     /* iteration sur les points de controles enregistrés dans le champs controle du dossier */
  49.     foreach($controles as $motif_id => $val) {
  50.       if($val) { // si le point de controle est coché
  51.         $controle $this->em->getRepository(DocumentControle::class)->find($motif_id);
  52.         $motif $this->templating->createTemplate($controle->getRaison());
  53.         $variables $dossier->getVars();
  54.         
  55.         // Variables template de motifs liés à document non remplissable
  56.         $variables['date_effet_contrat'] = $dossier->getDateEffet()->format('d/m/Y');
  57.         if(array_key_exists($motif_id$variables)) {
  58.           $variables $variables[$motif_id];
  59.         }
  60.         $motifs[] = $motif->render($variables);
  61.       }
  62.     }
  63.     
  64.     $vars = [
  65.       'motif_id' => $motif_id,
  66.       'offre' => $dossier->getOffre(),
  67.       'raison_sociale' => $dossier->getEntreprise(),
  68.       'motifs' => $motifs,
  69.       'dateEffet' => $dossier->getDateEffet()->format('d/m/Y'),
  70.     ];
  71.     $object = [
  72.       'dossier' => $dossier,
  73.       'event' => 'workflow.dossier.' $statut,
  74.       'vars' => $vars,
  75.     ];
  76.     $this->dispatcher->dispatch(new NotifyEvent($object), NotifyEvent::NAME);
  77.   }
  78. }