src/EventSubscriber/OvertimeRequestStatusWorkflowSubscriber.php line 69
<?phpnamespace App\EventSubscriber;use Generator;use App\Entity\Overtime;use App\Entity\OvertimeRequest;use App\Notifier\NotificationType;use App\Notifier\NotificationFactory;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\Workflow\Event\CompletedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class OvertimeRequestStatusWorkflowSubscriber implements EventSubscriberInterface{public function __construct(private readonly EntityManagerInterface $entityManager,private readonly NotificationFactory $notificationFactory,) {}public static function getSubscribedEvents(): Generator{$workflowName = 'leave_request';// Do not create HR notification on instant_request transition// The notification is handled post persist of OvertimeRequest entity to contain ID information$transitionNames = ['request' => ['completedRequest'],'accept' => ['completedAccept'],'cancel' => ['completedCancel'],'decline' => ['completedDecline'],];foreach ($transitionNames as $transitionName => $functionNames) {yield 'workflow.' . $workflowName . '.completed.' . $transitionName => $functionNames;}}public function completedRequest(CompletedEvent $event){$overtimeRequest = $event->getSubject();if ($overtimeRequest instanceof OvertimeRequest) {$notification = $this->notificationFactory->getNotification(NotificationType::OVERTIME_REQUESTED__HR,['overtimeRequest' => $overtimeRequest]);$this->entityManager->persist($notification);}}public function completedDecline(CompletedEvent $event){$overtimeRequest = $event->getSubject();if ($overtimeRequest instanceof OvertimeRequest) {$notification = $this->notificationFactory->getNotification(NotificationType::OVERTIME_REQUEST_DECLINED__REQUESTER,['overtimeRequest' => $overtimeRequest]);$this->entityManager->persist($notification);}}public function completedCancel(CompletedEvent $event){$overtimeRequest = $event->getSubject();if ($overtimeRequest instanceof OvertimeRequest) {$notification = $this->notificationFactory->getNotification(NotificationType::OVERTIME_REQUEST_CANCELLED_BY_REQUESTER__HR,['overtimeRequest' => $overtimeRequest]);$this->entityManager->persist($notification);}}public function completedAccept(CompletedEvent $event){$overtimeRequest = $event->getSubject();if ($overtimeRequest instanceof OvertimeRequest) {$overtime = new Overtime();$overtime->setEmployee($overtimeRequest->getEmployee());$overtime->setDate($overtimeRequest->getDate());$overtime->setDescription($overtimeRequest->getDescription());$overtime->setRequest($overtimeRequest);$overtime->setDuration($overtimeRequest->getDuration());$this->entityManager->persist($overtime);$notification = $this->notificationFactory->getNotification(NotificationType::OVERTIME_REQUEST_ACCEPTED__REQUESTER,['overtimeRequest' => $overtimeRequest]);$this->entityManager->persist($notification);}}}