src/EventSubscriber/EmployeeSubscriber.php line 80
<?phpnamespace App\EventSubscriber;use App\Entity\Employee;use App\Entity\Invitation;use App\Entity\User;use App\Event\Employee\EmployeeCreatedEvent;use App\Event\Employee\EmployeeDeletedEvent;use App\Event\Employee\EmployeeUpdatedEvent;use App\Event\Employee\InvitationAcceptedEvent;use App\Event\Employee\InvitationRefusedEvent;use App\Kimia;use App\Repository\EmployeeRepository;use App\Service\InvitationService;use App\Service\NotificationService;use DateTimeImmutable;use Symfony\Bundle\SecurityBundle\Security;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class EmployeeSubscriber implements EventSubscriberInterface{public function __construct(private readonly EmployeeRepository $repository,private readonly NotificationService $notificationService,private readonly InvitationService $invitationService,private readonly Security $security){}public static function getSubscribedEvents(): array{return [EmployeeCreatedEvent::class => 'onCreated',EmployeeUpdatedEvent::class => 'onUpdated',EmployeeDeletedEvent::class => 'onDeleted',InvitationAcceptedEvent::class => 'onAccepted',InvitationRefusedEvent::class => 'onRefused'];}public function onCreated(EmployeeCreatedEvent $event): void{$employee = $event->getEmployee();$invitation = $this->invitationService->create($employee->getEmail(),"Vous êtes invité à rejoindre l'établissement {$employee->getDepartment()->getEstablishment()->getName()} au département {$employee->getDepartment()->getName()}");$employee->setInvitation($invitation);$invitation->setEstablishment($employee->getDepartment()->getEstablishment());$this->repository->save($employee, true);$this->notificationService->notifyUser($employee->getDepartment()->getEstablishment()->getUser(),'Invitation envoyé',sprintf("Vous venez d'ajouter l'employé <strong>%s</strong>",$employee->getName()),$employee);}public function onUpdated(EmployeeUpdatedEvent $event): void{$employee = $event->getEmployee();$this->notificationService->notifyUser($employee->getDepartment()->getEstablishment()->getUser(),'Employé modifié',sprintf("Vous venez de modifier l'employé <strong>%s</strong>",$employee->getName()),$employee);}public function onDeleted(EmployeeDeletedEvent $event): void{$employee = $event->getEmployee();$establishment = $employee->getDepartment()->getEstablishment();$title = 'Employé supprimé';$message = sprintf("L'employé <strong>%s</strong> a été supprimé",$employee->getName());$this->notificationService->notifyUser($establishment->getUser(),$title,$message,$employee);$this->notificationService->sendMail(to: $establishment->getEmail(), subject: $title, body: $message);}public function onAccepted(InvitationAcceptedEvent $event): void{/** @var User $user */$user = $this->security->getUser();$title = 'Invitation acceptée';$invitation = $event->getInvitation();$invitation->setStatus(Kimia::STATUS_ACCEPTED);$employee = $this->repository->findOneBy(['invitation' => $invitation]);($employee)->setUser($user)->setStatus(Kimia::STATUS_ACCEPTED)->setIsActive(1)->setActivatedAt(new DateTimeImmutable());$this->repository->save($employee, true);$message = sprintf("L'employé <strong>%s</strong> a accepté l'invitation de votre établissement",$employee->getName());$this->notificationService->sendMail(to: $invitation->getEstablishment()->getEmail(), subject: $title, body: $message);}public function onRefused(InvitationRefusedEvent $event): void{$employee = $event->getEmployee();$establishment = $employee->getDepartment()->getEstablishment();$title = 'Invitation refusée';$message = sprintf("L'employé <strong>%s</strong> a refusé l'invitation de votre établissement",$employee->getName());$invitation = $event->getInvitation();$invitation->setStatus(Kimia::STATUS_DECLINED);$employee = $this->repository->findOneBy(['invitation' => $invitation]);$employee->setStatus(Kimia::STATUS_DECLINED);$this->repository->save($employee, true);$this->notificationService->sendMail(to: $establishment->getEmail(), subject: $title, body: $message);}}