src/EventSubscriber/LeaveRequestStatusWorkflowSubscriber.php line 26
<?phpnamespace App\EventSubscriber;use Generator;use App\Entity\Leave;use App\Entity\LeaveRequest;use App\Notifier\NotificationType;use App\Notifier\NotificationFactory;use Doctrine\ORM\EntityManagerInterface;use App\LeaveManagement\LeaveAllowanceBooker;use Symfony\Component\Workflow\Event\CompletedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class LeaveRequestStatusWorkflowSubscriber implements EventSubscriberInterface{use LeaveAllowanceModifying;public function __construct(private readonly EntityManagerInterface $entityManager,private readonly LeaveAllowanceBooker $leaveAllowanceBooker,private readonly NotificationFactory $notificationFactory,) {}public function completedRequest(CompletedEvent $event){$leaveRequest = $event->getSubject();if ($leaveRequest instanceof LeaveRequest) {$notification = $this->notificationFactory->getNotification(NotificationType::LEAVE_REQUESTED__HR,['leaveRequest' => $leaveRequest]);$this->entityManager->persist($notification);}}public function completedDecline(CompletedEvent $event){$leaveRequest = $event->getSubject();if ($leaveRequest instanceof LeaveRequest) {$this->releaseAllowanceDays($event);$notification = $this->notificationFactory->getNotification(NotificationType::LEAVE_REQUEST_DECLINED__REQUESTER,['leaveRequest' => $leaveRequest]);$this->entityManager->persist($notification);}}public function completedCancel(CompletedEvent $event){$leaveRequest = $event->getSubject();if ($leaveRequest instanceof LeaveRequest) {$this->releaseAllowanceDays($event);$notification = $this->notificationFactory->getNotification(NotificationType::LEAVE_REQUEST_CANCELLED_BY_REQUESTER__HR,['leaveRequest' => $leaveRequest]);$this->entityManager->persist($notification);}}public function completedAccept(CompletedEvent $event){$leaveRequest = $event->getSubject();if ($leaveRequest instanceof LeaveRequest) {$leave = new Leave();$leave->setEmployee($leaveRequest->getEmployee());$leave->setStartDate($leaveRequest->getStartDate());$leave->setFirstDayHours($leaveRequest->getFirstDayHours());$leave->setLastDayHours($leaveRequest->getLastDayHours());$leave->setEndDate($leaveRequest->getEndDate());$leave->setType($leaveRequest->getType());$leave->setDetails($leaveRequest->getDetails());$leave->setRequest($leaveRequest);$leave->setDuration($leaveRequest->getDuration());$this->entityManager->persist($leave);$notification = $this->notificationFactory->getNotification(NotificationType::LEAVE_REQUEST_ACCEPTED__REQUESTER,['leaveRequest' => $leaveRequest]);$this->entityManager->persist($notification);}}public static function getSubscribedEvents(): Generator{$workflowName = 'leave_request';// Do not create HR notification on instant_request transition// The notification is handled post persist of LeaveRequest 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;}}}