Rename the Plan bundle to Program across config, routes, menus, and controllers (Plan -> Initiative), bump version to 0.16.0, and introduce new audience and UTM taxonomy routes and permissions to support the expanded Programs feature set.
85 lines
2.8 KiB
PHP
85 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\ProgramBundle\Validator\Constraints;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use MauticPlugin\ProgramBundle\Entity\Task;
|
|
use MauticPlugin\ProgramBundle\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) {
|
|
return;
|
|
}
|
|
|
|
foreach ($task->getDependencies() as $dependency) {
|
|
$this->validateEdge($task, $dependency, $constraint);
|
|
}
|
|
}
|
|
|
|
private function validateEdge(Task $task, TaskDependency $dependency, Constraint $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->getInitiative() && null !== $dependsOn->getInitiative() && $task->getInitiative()->getId() !== $dependsOn->getInitiative()->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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|