You are here

protected function QueueClearCommand::execute in Advanced Queue 8

File

src/Command/QueueClearCommand.php, line 91

Class

QueueClearCommand
Class ClearQueueCommand.

Namespace

Drupal\advancedqueue\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {
  $queue_id = $input
    ->getArgument('queue_id');
  $state = strtolower($input
    ->getOption('state'));
  $queue_storage = $this->entityTypeManager
    ->getStorage('advancedqueue_queue');

  /** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
  $queue = $queue_storage
    ->load($queue_id);
  if (!$queue) {
    $message = $this
      ->trans('commands.advancedqueue.queue.clear.messages.not-found');
    throw new \RuntimeException(sprintf($message, $queue_id));
  }
  if ($state && !in_array($state, $this->jobStates)) {
    $message = $this
      ->trans('commands.advancedqueue.queue.clear.messages.unknown-state');
    throw new \RuntimeException(sprintf($message, $state));
  }
  $query = $this->database
    ->delete('advancedqueue');
  $query
    ->condition('queue_id', $queue_id);
  if ($state) {
    $query
      ->condition('state', $state, '=');
  }
  else {

    // All job states.
    $query
      ->condition('state', $this->jobStates, 'IN');
  }
  $num_rows = $query
    ->execute();
  $io = new DrupalStyle($input, $output);
  if ($num_rows) {
    $io
      ->success(sprintf($this
      ->trans('commands.advancedqueue.queue.clear.messages.finished'), $num_rows, $queue
      ->get('label')));
  }
  else {
    $io
      ->success(sprintf($this
      ->trans('commands.advancedqueue.queue.clear.messages.nothing-to-do'), $queue
      ->get('label')));
  }
}