You are here

public function MaestroIfTask::execute in Maestro 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/EngineTasks/MaestroIfTask.php \Drupal\maestro\Plugin\EngineTasks\MaestroIfTask::execute()

Part of the ExecutableInterface Execution of the Batch Function task will use the handler for this task as the executable function. .

Overrides ExecutableInterface::execute

File

src/Plugin/EngineTasks/MaestroIfTask.php, line 82

Class

MaestroIfTask
Maestro If Task Plugin.

Namespace

Drupal\maestro\Plugin\EngineTasks

Code

public function execute() {
  $templateMachineName = MaestroEngine::getTemplateIdFromProcessId($this->processID);
  $taskMachineName = MaestroEngine::getTaskIdFromQueueId($this->queueID);
  $task = MaestroEngine::getTemplateTaskByID($templateMachineName, $taskMachineName);
  $statusFlag = NULL;
  $ifData = $task['data']['if'];
  $variable = $ifData['variable'];
  $variableValue = $ifData['variable_value'];
  $method = $ifData['method'];

  // This is one of our constants defined in the engine.
  $status = $ifData['status'];

  // = , !=, < , >
  $operator = $ifData['operator'];
  switch ($method) {
    case 'byvariable':
      $processVariableValue = MaestroEngine::getProcessVariable($variable, $this->processID);

      // we're being optimistic here in that we're going to have a status match.
      $statusFlag = TRUE;

      // We need to determine if the variable chosen contains the value we are testing against based on the condition.
      switch ($operator) {
        case '=':

          // Not equal!
          if (strcmp($variableValue, $processVariableValue) != 0) {
            $statusFlag = FALSE;
          }
          break;
        case '!=':

          // equal!
          if (strcmp($variableValue, $processVariableValue) == 0) {
            $statusFlag = FALSE;
          }
          break;
        case '<':
          if (floatval($processVariableValue) > floatval($variableValue)) {
            $statusFlag = FALSE;
          }
          break;
        case '>':
          if (floatval($processVariableValue) < floatval($variableValue)) {
            $statusFlag = FALSE;
          }
          break;
      }
      break;
    case 'bylasttaskstatus':

      // Need to find out who points to this task.  If there is more than one pointer to this task,
      // we have no real way to know what to do other than if any other task that DOESN'T have the
      // status, we return false.
      $pointers = MaestroEngine::getTaskPointersFromTemplate($templateMachineName, $taskMachineName);

      // Pointers now holds the task machine names (taskIDs).  we fetch these from the queue now.
      $query = \Drupal::entityQuery('maestro_queue');
      $andMainConditions = $query
        ->andConditionGroup()
        ->condition('process_id', $this->processID)
        ->condition('status', '0', '<>')
        ->condition('archived', '1');
      $orConditionGroup = $query
        ->orConditionGroup();
      foreach ($pointers as $taskID) {
        $orConditionGroup
          ->condition('task_id', $taskID);
      }
      $andMainConditions
        ->condition($orConditionGroup);
      $query
        ->condition($andMainConditions);
      $entity_ids = $query
        ->execute();
      foreach ($entity_ids as $entityID) {
        $queueRecord = \Drupal::entityTypeManager()
          ->getStorage('maestro_queue')
          ->load($entityID);
        if (strcmp($status, $queueRecord->status
          ->getString()) != 0) {
          $statusFlag = FALSE;
        }
      }

      // At this point, if the statusFlag is not false, it must be OK as the default is NULL.
      if ($statusFlag === NULL) {
        $statusFlag = TRUE;
      }
      break;
  }

  // At this point, we have a statusFlag variable that denotes whether the pointed-from tasks in the queue have
  // a status that is equal to the status that was provided in the template.  If it's false, then we complete this
  // IF task with the execution status as success with a completion status of use the false branch.
  if ($statusFlag !== NULL) {
    if ($statusFlag == TRUE) {

      // Normal condition here. we've not aborted or done anything different.
      $this->executionStatus = TASK_STATUS_SUCCESS;

      // This will follow the true branch.
      $this->completionStatus = MAESTRO_TASK_COMPLETION_NORMAL;
    }
    else {

      // again, nothing unusual.  just set the task status.
      $this->executionStatus = TASK_STATUS_SUCCESS;

      // ahh.. last status doesn't equal what we tested for.  Use the false branch for nextstep.
      $this->completionStatus = MAESTRO_TASK_COMPLETION_USE_FALSE_BRANCH;
    }

    // Nothing really stopping us from always completing this task in the engine.
    return TRUE;
  }
  \Drupal::logger('maestro')
    ->error('If task does not have a statusFlag set and is unable to complete');

  // Problem here - we have a situation where the IF statement is stuck because the statusFlag was never set.
  return FALSE;

  // This will hold the engine at the IF task forever.
}