getSubject(); $this->em->persist($counter); $this->em->flush(); } #[AsCompletedListener('countdown', Counter::TRANSITION_TO_STARTED)] public function started(Event $event): void { /** @var Counter $counter */ $counter = $event->getSubject(); $counter ->setStartTime(new \DateTimeImmutable('now')) ->setEndTime($counter->getStartTime()->add(new \DateInterval($counter->getTimeTolive()))) ; $this->em->flush(); } #[AsCompletedListener('countdown', Counter::TRANSITION_TO_COMPLETED)] public function completed(Event $event): void { return; } #[AsCompletedListener('countdown', Counter::TRANSITION_TO_READY)] public function toReady(Event $event): void { /** @var Counter $counter */ $counter = $event->getSubject(); $counter ->setStartTime(null) ->setEndTime(null) ; //$this->em->flush(); } #[AsEventListener(KernelEvents::CONTROLLER)] public function onKernelController(ControllerEvent $event) { if (!$event->isMainRequest()) { return; } $counters = $this->counterRepository->findBy(['state' => Counter::STATE_STARTED]); foreach($counters as $counter) { if ($counter->getEndTime() <= new \DateTime('now')) { $this->countdownStateMachine->apply($counter, Counter::TRANSITION_TO_COMPLETED); } } $this->em->flush(); } }