- Correct getChoices() docblock to reflect translated label => enum backing value order instead of value => label - Re-indent Smart Links menu config to match actual array nesting - Align assignment operators in RefreshStatsCommand - Add missing newline at end of RefreshStatsCommand.php
85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Validator\Constraints;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskDependency;
|
|
use Symfony\Component\Validator\Constraint;
|
|
use Symfony\Component\Validator\ConstraintValidator;
|
|
|
|
/**
|
|
* Walks the task's dependency edges and refuses cycles, self-edges and edges to another plan's
|
|
* tasks. The walk follows the edges through the database (edges of other tasks are not on this
|
|
* form), so a cycle crossing other tasks is caught too.
|
|
*/
|
|
class ValidTaskDependencyValidator extends ConstraintValidator
|
|
{
|
|
public function __construct(
|
|
private EntityManagerInterface $em,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @param Task $task
|
|
*/
|
|
public function validate($task, Constraint $constraint): void
|
|
{
|
|
if (!$task instanceof Task || !$constraint instanceof ValidTaskDependency) {
|
|
return;
|
|
}
|
|
|
|
foreach ($task->getDependencies() as $dependency) {
|
|
$this->validateEdge($task, $dependency, $constraint);
|
|
}
|
|
}
|
|
|
|
private function validateEdge(Task $task, TaskDependency $dependency, ValidTaskDependency $constraint): void
|
|
{
|
|
$dependsOn = $dependency->getDependsOn();
|
|
|
|
if (null === $dependsOn) {
|
|
return;
|
|
}
|
|
|
|
if ($dependsOn === $task || $dependsOn->getId() === $task->getId()) {
|
|
$this->context->buildViolation($constraint::SELF_REFERENCE)->atPath('dependencies')->addViolation();
|
|
|
|
return;
|
|
}
|
|
|
|
if (null !== $task->getProgram() && null !== $dependsOn->getProgram() && $task->getProgram()->getId() !== $dependsOn->getProgram()->getId()) {
|
|
$this->context->buildViolation($constraint::OTHER_PLAN)->atPath('dependencies')->addViolation();
|
|
|
|
return;
|
|
}
|
|
|
|
// Cycle check: walk the edges from the depends-on task; if we come back to this task, it's a cycle.
|
|
$seen = [];
|
|
$queue = [$dependsOn];
|
|
|
|
while (null !== ($current = array_shift($queue))) {
|
|
$key = $current->getId() ?? spl_object_id($current);
|
|
if ($key === ($task->getId() ?? spl_object_id($task))) {
|
|
$this->context->buildViolation($constraint::CYCLE)->atPath('dependencies')->addViolation();
|
|
|
|
return;
|
|
}
|
|
if (isset($seen[$key])) {
|
|
continue;
|
|
}
|
|
$seen[$key] = true;
|
|
|
|
// Follow the edges of the current task, from the database (they may belong to other tasks).
|
|
$edges = $this->em->getRepository(TaskDependency::class)->findBy(['task' => $current]);
|
|
foreach ($edges as $edge) {
|
|
if (null !== $edge->getDependsOn()) {
|
|
$queue[] = $edge->getDependsOn();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|