You are here

public function ScheduleController::execute in Business Rules 8

Same name and namespace in other branches
  1. 2.x src/Controller/ScheduleController.php \Drupal\business_rules\Controller\ScheduleController::execute()

Manual execution of a scheduled item.

Parameters

string $business_rules_schedule: The business_rules_schedule id.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse The redirect response.

1 string reference to 'ScheduleController::execute'
business_rules.routing.yml in ./business_rules.routing.yml
business_rules.routing.yml

File

src/Controller/ScheduleController.php, line 111

Class

ScheduleController
Class ScheduleController.

Namespace

Drupal\business_rules\Controller

Code

public function execute($business_rules_schedule) {
  $task = Schedule::load($business_rules_schedule);

  /** @var \Drupal\business_rules\Entity\Action $action */
  $action = $task
    ->getTriggeredBy();
  $items = $action
    ->getSettings('items');
  $container = \Drupal::getContainer();
  $util = new BusinessRulesUtil($container);
  $reacts_on_definition = \Drupal::getContainer()
    ->get('plugin.manager.business_rules.reacts_on')
    ->getDefinition('cron_runs');
  $task_event = $task
    ->getEvent();
  $loop_control = time();
  $dummy = new \stdClass();
  $variables = new VariablesSet();
  $dummy_event = new BusinessRulesEvent($dummy, [
    'entity_type_id' => '',
    'bundle' => NULL,
    'entity' => NULL,
    'entity_unchanged' => NULL,
    'variables' => $variables,
    'reacts_on' => $reacts_on_definition,
    'loop_control' => $loop_control,
  ]);
  $event = $task_event instanceof BusinessRulesEvent ? $task_event : $dummy_event;

  /** @var \Drupal\Core\Entity\Entity $entity */
  $entity = $task_event ? $task_event
    ->getSubject() instanceof Entity ? $task_event
    ->getSubject() : FALSE : FALSE;
  if ($entity) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity
      ->getEntityTypeId())
      ->load($entity
      ->id());
    $task_event
      ->setArgument('entity', $entity);
    $event = new BusinessRulesEvent($entity, $task_event
      ->getArguments());
  }
  try {
    foreach ($items as $item) {
      $action_item = Action::load($item['id']);
      $action_item
        ->execute($event);
    }
    $entity = $event
      ->getSubject() instanceof Entity ? $event
      ->getSubject() : FALSE;
    if ($entity && $task
      ->getUpdateEntity()) {
      $entity_exists = \Drupal::entityTypeManager()
        ->getStorage($entity
        ->getEntityTypeId())
        ->load($entity
        ->id());
      if ($entity_exists instanceof EntityInterface) {
        $entity
          ->save();
      }
    }
    $task
      ->setExecuted(1);
    $task
      ->save();
    $util->logger
      ->notice(t('Scheduled task id: @id, name: "@name", triggered by: "@by" has been executed at: @time', [
      '@id' => $task
        ->id(),
      '@name' => $task
        ->getName(),
      '@by' => $task
        ->getTriggeredBy()
        ->id(),
      '@time' => $container
        ->get('date.formatter')
        ->format(time(), 'medium'),
    ]));
  } catch (\Exception $e) {
    $util->logger
      ->error($e
      ->getMessage());
  }
  return new RedirectResponse('/admin/config/workflow/business_rules/schedule/collection');
}