You are here

public function BusinessRulesProcessor::processItems in Business Rules 2.x

Same name and namespace in other branches
  1. 8 src/Util/BusinessRulesProcessor.php \Drupal\business_rules\Util\BusinessRulesProcessor::processItems()

Process the items.

Parameters

array $items: Array of items to pe processed. Each item must be a instance of BusinessRulesItemObject.

\Drupal\business_rules\Events\BusinessRulesEvent $event: The event.

string $parent_id: The Item parent Id. It can be the Business Rule or other item.

1 call to BusinessRulesProcessor::processItems()
BusinessRulesProcessor::processTriggeredRules in src/Util/BusinessRulesProcessor.php
Process the triggered rules.

File

src/Util/BusinessRulesProcessor.php, line 335

Class

BusinessRulesProcessor
Class BusinessRulesProcessor.

Namespace

Drupal\business_rules\Util

Code

public function processItems(array $items, BusinessRulesEvent $event, $parent_id) {

  // Dispatch a event before process business rule items.
  $this->eventDispatcher
    ->dispatch('business_rules.before_process_items', $event);

  /** @var \Drupal\business_rules\BusinessRulesItemObject $item */
  foreach ($items as $item) {
    if ($item
      ->getType() == BusinessRulesItemObject::ACTION) {
      $action = Action::load($item
        ->getId());
      if (!empty($action)) {
        $this
          ->executeAction($action, $event);
        $this->debugArray['actions'][$this->ruleBeingExecuted
          ->id()][] = [
          'item' => $action,
          'parent' => $parent_id,
        ];
      }
      else {
        $this->util->logger
          ->error('Action id: %id not found', [
          '%id' => $item
            ->getId(),
        ]);
        $this->messenger
          ->addError($this
          ->t('Business Rules - Action id: %id not found.', [
          '%id' => $item
            ->getId(),
        ]));
      }
    }
    elseif ($item
      ->getType() == BusinessRulesItemObject::CONDITION) {
      $condition = Condition::load($item
        ->getId());
      if (empty($condition)) {
        $this->util->logger
          ->error('Condition id: %id not found', [
          '%id' => $item
            ->getId(),
        ]);
        $this->messenger
          ->addError($this
          ->t('Business Rules Condition id: %id not found.', [
          '%id' => $item
            ->getId(),
        ]));
      }
      else {
        $success = $this
          ->isConditionValid($condition, $event);
        if ($success) {
          $condition_items = $condition
            ->getSuccessItems();
          $this->debugArray['conditions'][$this->ruleBeingExecuted
            ->id()]['success'][] = [
            'item' => $condition,
            'parent' => $parent_id,
          ];
        }
        else {
          $condition_items = $condition
            ->getFailItems();
          $this->debugArray['conditions'][$this->ruleBeingExecuted
            ->id()]['fail'][] = [
            'item' => $condition,
            'parent' => $parent_id,
          ];
        }
        if (is_array($condition_items)) {
          $this
            ->processItems($condition_items, $event, $condition
            ->id());
        }
      }
    }
  }

  // Dispatch a event after process business rule items.
  $this->eventDispatcher
    ->dispatch('business_rules.after_process_items', $event);
}