Files
mautic-marketing-program-bu…/Validator/Constraints/ValidTaskDependencyValidator.php
T
Alexandre Gain ea88b7a772 refactor(plugin): rebrand as MT3SMarketingProgram, release 0.17.0
Rename the plugin's vendor/namespace from experiential/Program to mt3s/MT3SMarketingProgram (composer: mt3s/mautic-marketing-program-bundle):

- ProgramBundle -> MT3SMarketingProgramBundle; extension, integration,
  config support, permissions, events and twig extension renamed likewise
- Initiative* -> Program*: controller (InitiativeController ->
  ProgramController), model, entities, repositories, form types and views
  (new _tabs/_task_table/_tab_table_filter partials, details page)
- Settings config form theme -> _config_mt3smarketingprogramconfig_widget;
  routes and translations re-prefixed (mt3s_marketing_program_*)
- Assets: icon.svg -> mt3s-mp-icon.svg; program.js -> mt3s-mp-program.js
  (OnLoad hooks renamed mt3sMpProgram*)
- Add Command/RefreshStatsCommand; Migrations/Version_0_17_0.php renames
  the tables to mt3s_mp_program_*
2026-11-15 17:48:01 +01:00

85 lines
2.8 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) {
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->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();
}
}
}
}
}