mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-22 02:04:24 +00:00
58 lines
2.2 KiB
PHP
58 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Chill\MainBundle\Service\Workflow;
|
|
|
|
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
|
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
|
use Symfony\Component\Workflow\Registry;
|
|
|
|
#[AsMessageHandler]
|
|
class CancelStaleWorkflowHandler
|
|
{
|
|
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger)
|
|
{
|
|
}
|
|
|
|
public function __invoke(CancelStaleWorkflow $message): void
|
|
{
|
|
$workflowId = $message->getWorkflowId();
|
|
|
|
$workflow = $this->workflowRepository->find($workflowId);
|
|
$workflowComponent = $this->registry->get($workflow, $workflow->getWorkflowName());
|
|
$metadataStore = $workflowComponent->getMetadataStore();
|
|
$transitions = $workflowComponent->getEnabledTransitions($workflow);
|
|
$steps = $workflow->getSteps();
|
|
|
|
$transitionApplied = false;
|
|
|
|
if (count($steps) === 1) {
|
|
$this->em->remove($workflow->getCurrentStep());
|
|
$this->em->remove($workflow);
|
|
} else {
|
|
foreach ($transitions as $transition) {
|
|
$isFinal = $metadataStore->getMetadata('isFinal', $transition);
|
|
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
|
|
|
|
if ($isFinal && !$isFinalPositive) {
|
|
$workflowComponent->apply($workflow, 'annule');
|
|
$this->logger->info(sprintf('EntityWorkflow %d has been transitioned.', $workflowId));
|
|
$transitionApplied = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$transitionApplied) {
|
|
$this->logger->error(sprintf('No valid transition found for EntityWorkflow %d.', $workflowId));
|
|
throw new UnrecoverableMessageHandlingException(
|
|
sprintf('No valid transition found for EntityWorkflow %d.', $workflowId)
|
|
);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|