Fix Canceling of stale workflow cronjob

Refactor workflow cancellation logic to encapsulate transition checks in a dedicated method, and update CronJob handling to use entity workflows instead of IDs. Enhance test coverage to ensure proper handling and instantiate mocks for EntityManagerInterface.
This commit is contained in:
2024-10-21 17:39:31 +02:00
parent 1d708a481d
commit 527cf23d4f
6 changed files with 73 additions and 29 deletions

View File

@@ -18,7 +18,9 @@ use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\Transition;
#[AsMessageHandler]
final readonly class CancelStaleWorkflowHandler
@@ -57,10 +59,7 @@ final readonly class CancelStaleWorkflowHandler
$wasInInitialPosition = 'initial' === $workflow->getStep();
foreach ($transitions as $transition) {
$isFinal = $metadataStore->getMetadata('isFinal', $transition);
$isFinalPositive = $metadataStore->getMetadata('isFinalPositive', $transition);
if ($isFinal && !$isFinalPositive) {
if ($this->willTransitionLeadToFinalNegative($transition, $metadataStore)) {
$dto = new WorkflowTransitionContextDTO($workflow);
$workflowComponent->apply($workflow, $transition->getName(), [
'context' => $dto,
@@ -85,4 +84,16 @@ final readonly class CancelStaleWorkflowHandler
$this->em->flush();
}
private function willTransitionLeadToFinalNegative(Transition $transition, MetadataStoreInterface $metadataStore): bool
{
foreach ($transition->getTos() as $place) {
$metadata = $metadataStore->getPlaceMetadata($place);
if (($metadata['isFinal'] ?? true) && false === ($metadata['isFinalPositive'] ?? true)) {
return true;
}
}
return false;
}
}