You are here

public static function MaestroEngine::setProcessVariable in Maestro 8.2

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

Set a process variable's value. Does a post-variable-set call to re-create any production assignments that may rely on this variable's value.

Parameters

string $variableName: The variable machine name you wish to set.

string $variableValue: The variable's value you wish to set.

int $processID: The Maestro Process ID.

2 calls to MaestroEngine::setProcessVariable()
MaestroEngine::createProductionTask in src/Engine/MaestroEngine.php
Creates a task in the Maestro Queue table.
MaestroSetProcessVariableTask::execute in src/Plugin/EngineTasks/MaestroSetProcessVariableTask.php
Part of the ExecutableInterface Execution of the set process variable task. We will read the data in the template for what we should do with the process variable .

File

src/Engine/MaestroEngine.php, line 354

Class

MaestroEngine
Class MaestroEngine.

Namespace

Drupal\maestro\Engine

Code

public static function setProcessVariable($variableName, $variableValue, $processID) {
  $query = \Drupal::entityTypeManager()
    ->getStorage('maestro_process_variables')
    ->getQuery();
  $query
    ->condition('process_id', $processID)
    ->condition('variable_name', $variableName);
  $varID = $query
    ->execute();
  if (count($varID) > 0) {
    $varRecord = \Drupal::entityTypeManager()
      ->getStorage('maestro_process_variables')
      ->load(current($varID));
    $varRecord
      ->set('variable_value', $variableValue);
    $varRecord
      ->save();

    // TODO: handle an error condition on save here.
    // most likely trap the error and return false.
    // now we determine if there's any production assignments done on this variable
    // by_variable = 1, process_variable needs to match our variable.
    $query = \Drupal::entityTypeManager()
      ->getStorage('maestro_production_assignments')
      ->getQuery();
    $query
      ->condition('process_variable', $varID)
      ->condition('by_variable', '1')
      ->condition('task_completed', '0');
    $entries = $query
      ->execute();

    // we're going to remove these entries and actually do a production assignment.
    $engine = new MaestroEngine();
    $storeAssignmentInfo = [];
    foreach ($entries as $assignmentID) {
      $assignRecord = \Drupal::entityTypeManager()
        ->getStorage('maestro_production_assignments')
        ->load($assignmentID);
      $queueID = $assignRecord->queue_id
        ->getString();
      $processID = MaestroEngine::getProcessIdFromQueueId($queueID);
      $taskID = MaestroEngine::getTaskIdFromQueueId($queueID);
      $templateMachineName = MaestroEngine::getTemplateIdFromProcessId($processID);

      // We store the assignment keyed on queueID because an assignment may have multiple asignees listed in the production assignments.
      $storeAssignmentInfo[$queueID] = [
        'templateMachineName' => $templateMachineName,
        'taskID' => $taskID,
      ];

      // Now remove this assignment.
      $assignRecord
        ->delete();
    }
    foreach ($storeAssignmentInfo as $queueID => $assingmentInfo) {
      $engine
        ->productionAssignments($assingmentInfo['templateMachineName'], $assingmentInfo['taskID'], $queueID);
    }

    // Now lets call our hooks on a post-save variable change
    // our built-in hook will update the production assignments if they exist.
    \Drupal::moduleHandler()
      ->invokeAll('maestro_post_variable_save', [
      $variableName,
      $variableValue,
      $processID,
    ]);
  }
}