mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-22 02:04:24 +00:00
Create message and handler for canceling stale workflows
This commit is contained in:
parent
860ae5cedf
commit
34edb02cd0
@ -4,12 +4,12 @@ namespace Chill\MainBundle\Service\Workflow;
|
||||
|
||||
class CancelStaleWorkflow
|
||||
{
|
||||
public function __construct(public array $workflowIds)
|
||||
public function __construct(public int $workflowId)
|
||||
{
|
||||
}
|
||||
|
||||
public function getWorkflowIds(): array
|
||||
public function getWorkflowId(): int
|
||||
{
|
||||
return $this->workflowIds;
|
||||
return $this->workflowId;
|
||||
}
|
||||
}
|
||||
|
@ -5,26 +5,53 @@ namespace Chill\MainBundle\Service\Workflow;
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
|
||||
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
|
||||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
class CancelStaleWorkflowHandler implements MessageHandlerInterface
|
||||
#[AsMessageHandler]
|
||||
class CancelStaleWorkflowHandler
|
||||
{
|
||||
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private EntityManagerInterface $em, private LoggerInterface $logger)
|
||||
public function __construct(private readonly EntityWorkflowRepository $workflowRepository, private readonly Registry $registry, private EntityManagerInterface $em, private LoggerInterface $logger)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function __invoke(CancelStaleWorkflow $message): void
|
||||
{
|
||||
$workflowIds = $message->getWorkflowIds();
|
||||
$workflowId = $message->getWorkflowId();
|
||||
|
||||
foreach ($workflowIds as $workflowId) {
|
||||
$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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user