src/EventSubscriber/WorkflowNotificationSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3.  use App\Entity\ResNotification;
  4. use App\Entity\ResRoleConfig;
  5.  use App\Entity\Vacation;
  6.  use App\Utils\Consts;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Workflow\Event\Event;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. class WorkflowNotificationSubscriber implements EventSubscriberInterface
  12. {
  13.     private $entityManager;
  14.     private $router;
  15.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $router)
  16.     {
  17.         $this->entityManager $entityManager;
  18.         $this->router $router;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             'workflow.vacation_validation.leave' => 'onLeave',
  24.         ];
  25.     }
  26.     public function onLeave(Event $event)
  27.     {
  28.         $vacation $event->getSubject();
  29.         if ($vacation instanceof Vacation) {
  30.             $transitionName $event->getTransition()->getName();
  31.             $notificationHiearchie = new ResNotification();
  32.             $notificationHiearchie->setTypeNotification(Consts::NOTIFICATION_DEMANDE_VALIDATION_VACATION);
  33.             $notificationHiearchie->setDateNotification(new \DateTime());
  34.             $notificationHiearchie->setDestination($vacation->getPoste()->getResponsable()->getPartner()->getUserAccount());
  35.             $vacationDemandeShowPath $this->router->generate('app_vacation_show', [
  36.                 'id' => $vacation->getId(),
  37.             ]);
  38.             $notificationHiearchie->setActionName($vacationDemandeShowPath);
  39.             if ($transitionName == Consts::TRANSIION_to_accepted) {
  40.                 $notificationHiearchie->setMessage(
  41.                     sprintf(Consts::NOTIFICATION_VACATION_MESSAGE['accepted'])
  42.                 );
  43.                 $this->entityManager->persist($notificationHiearchie);
  44.                 $this->entityManager->persist($vacation);
  45.                 $this->entityManager->flush();
  46.             } elseif ($transitionName == Consts::TRANSIION_to_rejected) {
  47.                 $notificationHiearchie->setMessage(
  48.                     sprintf(Consts::NOTIFICATION_VACATION_MESSAGE['rejected'])
  49.                 );
  50.                 $this->entityManager->persist($notificationHiearchie);
  51.                 $this->entityManager->persist($vacation);
  52.                 $this->entityManager->flush();
  53.             }
  54.         }
  55.     }
  56. }