You are here

public static function MaestroEngine::performTemplateValidityCheck in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 src/Engine/MaestroEngine.php \Drupal\maestro\Engine\MaestroEngine::performTemplateValidityCheck()

Checks the validity of the template in question.

Parameters

string $templateMachineName: The machine name of the template.

Return value

array Will return an array with keys 'failures' and 'information' with each array contains an array in the form of ['taskID', 'taskLabel', 'reason'] that fail or create information during the validity check. Empty 'failures' key array if no issues.

2 calls to MaestroEngine::performTemplateValidityCheck()
MaestroValidityCheck::buildForm in modules/maestro_template_builder/src/Form/MaestroValidityCheck.php
Ajax callback for add-new-form button click.
MaestroValidityCheck::cancelForm in modules/maestro_template_builder/src/Form/MaestroValidityCheck.php

File

src/Engine/MaestroEngine.php, line 837

Class

MaestroEngine
Class MaestroEngine.

Namespace

Drupal\maestro\Engine

Code

public static function performTemplateValidityCheck($templateMachineName) {
  $template = MaestroEngine::getTemplate($templateMachineName);
  $pointers = [];
  $endTaskExists = FALSE;
  $validation_failure_tasks = [];
  $validation_information_tasks = [];
  foreach ($template->tasks as $task) {

    // Now determine who points to this task.  If this is an AND or an OR task, you can have multiple pointers.
    // if you're any other type of task, you've broken the validity of the template.
    $pointers = MaestroEngine::getTaskPointersFromTemplate($templateMachineName, $task['id']);

    // $task['tasktype'] holds the task type.
    // this is validation information, not an error.
    if ($task['tasktype'] !== 'MaestroOr' && $task['tasktype'] !== 'MaestroAnd') {

      // We have a non-logical validation failure here.
      if (count($pointers) > 1) {
        $validation_information_tasks[] = [
          'taskID' => $task['id'],
          'taskLabel' => $task['label'],
          'reason' => t('This task, with two pointers to it, will cause a regeneration to happen.  Please see documentation for more information.'),
        ];
      }
    }

    // Now check to see if the task has at least ONE pointer to it OTHER THAN THE START TASK!
    if ($task['tasktype'] !== 'MaestroStart') {

      // No pointers to this task.
      if (count($pointers) == 0) {
        $validation_failure_tasks[] = [
          'taskID' => $task['id'],
          'taskLabel' => $task['label'],
          'reason' => t('Task has no other tasks pointing to it. Only the Start Task is allowed to have no tasks pointing to it.'),
        ];
      }
    }
    if ($task['tasktype'] === 'MaestroEnd') {
      $endTaskExists = TRUE;
    }

    // Now let the task itself determine if it should fail the validation.
    $executable_task = NULL;
    $executable_task = MaestroEngine::getPluginTask($task['tasktype']);
    if ($executable_task != NULL) {
      $executable_task
        ->performValidityCheck($validation_failure_tasks, $validation_information_tasks, $task);
    }
  }

  // Now we check to see if an end task exists...
  if (!$endTaskExists) {
    $validation_failure_tasks[] = [
      'taskID' => t('No Task ID'),
      'taskLabel' => t('No Task Label'),
      'reason' => t('This template is missing an END Task.  Without an END Task, the process will never be flagged as complete.'),
    ];
  }

  // Anyone else would like to add to or remove from the validation failure list here?  by all means:
  \Drupal::moduleHandler()
    ->invokeAll('maestro_template_validation_check', [
    $templateMachineName,
    &$validation_failure_tasks,
    &$validation_information_tasks,
  ]);

  // If we have no validation issues, lets set the template to have its validity set to true.
  if (count($validation_failure_tasks) == 0) {
    $template->validated = TRUE;
  }
  else {
    $template->validated = FALSE;
  }
  $template
    ->save();
  return [
    'failures' => $validation_failure_tasks,
    'information' => $validation_information_tasks,
  ];
}