src/EventSubscriber/EmployeeSubscriber.php line 66

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Employee;
  4. use App\Entity\Invitation;
  5. use App\Entity\User;
  6. use App\Event\Employee\EmployeeCreatedEvent;
  7. use App\Event\Employee\EmployeeDeletedEvent;
  8. use App\Event\Employee\EmployeeUpdatedEvent;
  9. use App\Event\Employee\InvitationAcceptedEvent;
  10. use App\Event\Employee\InvitationRefusedEvent;
  11. use App\Kimia;
  12. use App\Repository\EmployeeRepository;
  13. use App\Service\InvitationService;
  14. use App\Service\NotificationService;
  15. use DateTimeImmutable;
  16. use Symfony\Bundle\SecurityBundle\Security;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class EmployeeSubscriber implements EventSubscriberInterface
  19. {
  20.     public function __construct(
  21.         private readonly EmployeeRepository  $repository,
  22.         private readonly NotificationService $notificationService,
  23.         private readonly InvitationService   $invitationService,
  24.         private readonly Security $security
  25.     )
  26.     {
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             EmployeeCreatedEvent::class => 'onCreated',
  32.             EmployeeUpdatedEvent::class => 'onUpdated',
  33.             EmployeeDeletedEvent::class => 'onDeleted',
  34.             InvitationAcceptedEvent::class => 'onAccepted',
  35.             InvitationRefusedEvent::class => 'onRefused'
  36.         ];
  37.     }
  38.     public function onCreated(EmployeeCreatedEvent $event): void
  39.     {
  40.         $employee $event->getEmployee();
  41.         $invitation $this->invitationService->create(
  42.             $employee->getEmail(),
  43.             "Vous êtes invité à rejoindre l'établissement {$employee->getDepartment()->getEstablishment()->getName()} au département {$employee->getDepartment()->getName()}"
  44.         );
  45.         $employee->setInvitation($invitation);
  46.         $invitation->setEstablishment($employee->getDepartment()->getEstablishment());
  47.         $this->repository->save($employeetrue);
  48.         $this->notificationService->notifyUser(
  49.             $employee->getDepartment()->getEstablishment()->getUser(),
  50.             'Invitation envoyé',
  51.             sprintf(
  52.                 "Vous venez d'ajouter l'employé <strong>%s</strong>",
  53.                 $employee->getName()
  54.             ),
  55.             $employee
  56.         );
  57.     }
  58.     public function onUpdated(EmployeeUpdatedEvent $event): void
  59.     {
  60.         $employee $event->getEmployee();
  61.         $this->notificationService->notifyUser(
  62.             $employee->getDepartment()->getEstablishment()->getUser(),
  63.             'Employé modifié',
  64.             sprintf(
  65.                 "Vous venez de modifier l'employé <strong>%s</strong>",
  66.                 $employee->getName()
  67.             ),
  68.             $employee
  69.         );
  70.     }
  71.     public function onDeleted(EmployeeDeletedEvent $event): void
  72.     {
  73.         $employee $event->getEmployee();
  74.         $establishment $employee->getDepartment()->getEstablishment();
  75.         $title 'Employé supprimé';
  76.         $message sprintf(
  77.             "L'employé <strong>%s</strong> a été supprimé",
  78.             $employee->getName()
  79.         );
  80.         $this->notificationService->notifyUser(
  81.             $establishment->getUser(),
  82.             $title,
  83.             $message,
  84.             $employee
  85.         );
  86.         $this->notificationService->sendMail(to$establishment->getEmail(), subject$titlebody$message);
  87.     }
  88.     public function onAccepted(InvitationAcceptedEvent $event): void
  89.     {
  90.         /** @var User $user */
  91.         $user $this->security->getUser();
  92.         $title 'Invitation acceptée';
  93.         $invitation $event->getInvitation();
  94.         $invitation->setStatus(Kimia::STATUS_ACCEPTED);
  95.         $employee $this->repository->findOneBy(['invitation' => $invitation]);
  96.         ($employee)
  97.             ->setUser($user)
  98.             ->setStatus(Kimia::STATUS_ACCEPTED)
  99.             ->setIsActive(1)
  100.             ->setActivatedAt(new DateTimeImmutable());
  101.         $this->repository->save($employeetrue);
  102.         $message sprintf(
  103.             "L'employé <strong>%s</strong> a accepté l'invitation de votre établissement",
  104.             $employee->getName()
  105.         );
  106.         $this->notificationService->sendMail(to$invitation->getEstablishment()->getEmail(), subject$titlebody$message);
  107.     }
  108.     public function onRefused(InvitationRefusedEvent $event): void
  109.     {
  110.         $employee $event->getEmployee();
  111.         $establishment $employee->getDepartment()->getEstablishment();
  112.         $title 'Invitation refusée';
  113.         $message sprintf(
  114.             "L'employé <strong>%s</strong> a refusé l'invitation de votre établissement",
  115.             $employee->getName()
  116.         );
  117.         $invitation $event->getInvitation();
  118.         $invitation->setStatus(Kimia::STATUS_DECLINED);
  119.         $employee $this->repository->findOneBy(['invitation' => $invitation]);
  120.         $employee->setStatus(Kimia::STATUS_DECLINED);
  121.         $this->repository->save($employeetrue);
  122.         $this->notificationService->sendMail(to$establishment->getEmail(), subject$titlebody$message);
  123.     }
  124. }