Add duplicate workflow prevention in MetadataExtractor

Integrate DuplicateEntityWorkflowFinder to prevent creating workflows for entities with existing opened or positive final workflows. Updated EntityWorkflowVoter to implement the same check before allowing creation. Removed unnecessary blank workflow parameter from Twig template.
This commit is contained in:
2024-09-24 14:18:50 +02:00
parent 758a14366e
commit 5a5d259d18
4 changed files with 87 additions and 4 deletions

View File

@@ -19,20 +19,33 @@ use Symfony\Component\Workflow\WorkflowInterface;
class MetadataExtractor
{
public function __construct(private readonly Registry $registry, private readonly TranslatableStringHelperInterface $translatableStringHelper) {}
public function __construct(
private readonly Registry $registry,
private readonly TranslatableStringHelperInterface $translatableStringHelper,
private readonly DuplicateEntityWorkflowFinder $duplicateEntityWorkflowFinder,
) {}
public function availableWorkflowFor(string $relatedEntityClass, ?int $relatedEntityId = 0): array
{
$blankEntityWorkflow = new EntityWorkflow();
$blankEntityWorkflow
->setRelatedEntityId($relatedEntityId)
->setRelatedEntityClass($relatedEntityClass);
->setRelatedEntityClass($relatedEntityClass)
;
// build the list of available workflows, and extract their names from metadata
$workflows = $this->registry->all($blankEntityWorkflow);
$workflowsList = [];
foreach ($workflows as $workflow) {
// shortcut: we must not be able to create a new workflow if there are already created workflows,
// so, we find if there are workflows opened or final positive for the same entity
$blankEntityWorkflow->setWorkflowName($workflow->getName());
if ($this->duplicateEntityWorkflowFinder->hasDuplicateOpenedOrFinalPositiveEntityWorkflow($blankEntityWorkflow)) {
// if yes, we skip suggesting workflow
continue;
}
$metadata = $workflow->getMetadataStore()->getWorkflowMetadata();
$text = \array_key_exists('label', $metadata) ?
$this->translatableStringHelper->localize($metadata['label']) : $workflow->getName();