You are here

public function AcquiaContenthubCommands::contenthubAuditPublisher in Acquia Content Hub 8

Checks published entities and republishes them to Content Hub.

@internal param array $request_options An associative array of options whose values come from cli, aliases, config, etc.

@option publish 1 if set to republish entities, 0 (or false) if we just want to print. @option status The status of the entities to audit, defaults to EXPORTED if not given.

@command acquia:contenthub-audit-publisher @aliases ach-ap,ach-audit-publisher

Parameters

string $entity_type_id: The Entity Type.

array $options: The options array.

Throws

\Exception

File

src/Commands/AcquiaContenthubCommands.php, line 844

Class

AcquiaContenthubCommands
A Drush commandfile.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubAuditPublisher($entity_type_id = NULL, array $options = [
  'publish' => NULL,
  'status' => ContentHubEntitiesTracking::EXPORTED,
]) {

  // Obtaining the query.
  $publish = $options["publish"];
  $status = $options["status"];
  if ($publish) {
    $warning_message = dt('Are you sure you want to republish entities to Content Hub?');
    if ($this
      ->io()
      ->confirm($warning_message) == FALSE) {
      throw new \Exception(dt('Command aborted by user.'));
    }
  }

  /** @var \Drupal\acquia_contenthub\ContentHubEntitiesTracking $entities_tracking */
  $entities_tracking = \Drupal::getContainer()
    ->get('acquia_contenthub.acquia_contenthub_entities_tracking');
  switch ($status) {
    case ContentHubEntitiesTracking::EXPORTED:
      $entities = $entities_tracking
        ->getPublishedEntities($entity_type_id);
      break;
    case ContentHubEntitiesTracking::INITIATED:
      $entities = $entities_tracking
        ->getInitiatedEntities($entity_type_id);
      break;
    case ContentHubEntitiesTracking::REINDEX:
      $entities = $entities_tracking
        ->getEntitiesToReindex($entity_type_id);
      break;
    case ContentHubEntitiesTracking::QUEUED:

      // If we want to queue "queued" entities, then we have to make sure the
      // export queue is empty or we might be re-queuing entities that already
      // are in the queue.

      /** @var \Drupal\Core\Queue\QueueInterface $queue */
      $queue = \Drupal::getContainer()
        ->get('queue')
        ->get('acquia_contenthub_export_queue');
      if ($queue
        ->numberOfItems() > 0) {
        throw new \Exception(dt('You cannot audit queued entities when the queue is not empty because you run the risk of re-queuing the same entities. Please retry when the queue is empty.'));
      }
      $entities = $entities_tracking
        ->getQueuedEntities($entity_type_id);
      break;
    default:
      throw new \Exception(dt('You can only use the following values for status: EXPORTED, INITIATED, REINDEX, QUEUED.'));
  }
  $this->output
    ->writeln(dt('Auditing entities with export status = @status...', [
    '@status' => $status,
  ]));

  // Creating the batch process.
  $operations = [];
  $chunks = array_chunk($entities, static::BATCH_PROCESS_CHUNK_SIZE);
  foreach ($chunks as $chunk) {
    $operations[] = [
      'acquia_contenthub_audit_publisher',
      [
        $chunk,
        $publish,
      ],
    ];
  }

  // Setting up batch process.
  $batch = [
    'title' => dt("Checks published entities with Content Hub for correct status"),
    'operations' => $operations,
    'finished' => 'acquia_contenthub_audit_publisher_finished',
  ];

  // Batch processing.
  batch_set($batch);

  // Start the batch process.
  drush_backend_batch_process();
}