You are here

class AcquiaContentHubPublisherAuditCommands in Acquia Content Hub 8.2

Drush commands for Acquia Content Hub Publishers Audit.

@package Drupal\acquia_contenthub_publisher\Commands

Hierarchy

Expanded class hierarchy of AcquiaContentHubPublisherAuditCommands

1 string reference to 'AcquiaContentHubPublisherAuditCommands'
drush.services.yml in modules/acquia_contenthub_publisher/drush.services.yml
modules/acquia_contenthub_publisher/drush.services.yml
1 service uses AcquiaContentHubPublisherAuditCommands
acquia_contenthub_publisher_audit.commands in modules/acquia_contenthub_publisher/drush.services.yml
Drupal\acquia_contenthub_publisher\Commands\AcquiaContentHubPublisherAuditCommands

File

modules/acquia_contenthub_publisher/src/Commands/AcquiaContentHubPublisherAuditCommands.php, line 16

Namespace

Drupal\acquia_contenthub_publisher\Commands
View source
class AcquiaContentHubPublisherAuditCommands extends DrushCommands {
  use DependencySerializationTrait;

  /**
   * The queue object.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * The published entity tracker.
   *
   * @var \Drupal\acquia_contenthub_publisher\PublisherTracker
   */
  protected $tracker;

  /**
   * AcquiaContentHubPublisherAuditCommands constructor.
   *
   * @param \Drupal\acquia_contenthub_publisher\PublisherTracker $tracker
   *   The published entity tracker.
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   *   Queue factory.
   */
  public function __construct(PublisherTracker $tracker, QueueFactory $queue_factory) {
    $this->queue = $queue_factory
      ->get('acquia_contenthub_publish_export');
    $this->tracker = $tracker;
  }

  /**
   * Audits exported entities and republishes if inconsistencies are found.
   *
   * @param string|null $entity_type_id
   *   Entity type id.
   *
   * @option publish
   *   Republish inconsistent entities to Content Hub.
   * @default publish false
   *
   * @option status
   *   The export status of the entities to audit, defaults to exported if not
   *   given. Possible values: exported, confirmed, queued.
   * @default status exported
   *
   * @command acquia:contenthub-audit-publisher
   * @aliases ach-audit-publisher, ach-ap
   *
   * @throws \Exception
   */
  public function audit(string $entity_type_id = '') {
    $publish = $this->input
      ->getOption('publish');
    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 UserAbortException();
      }
    }
    $status = strtolower($this->input
      ->getOption('status'));
    $entities = $this
      ->getContentHubTrackedEntities($status, $entity_type_id);
    $this
      ->output()
      ->writeln(dt('<fg=green>Auditing entities with export status = @status</>', [
      '@status' => $status,
    ]));

    // Setting up batch process.
    $batch = [
      'title' => dt('Checks @status entities and compares them with Content Hub.', [
        '@status' => $status,
      ]),
      'operations' => [],
      'finished' => '\\Drupal\\acquia_contenthub\\AuditTrackEntities::batchFinished',
    ];
    $chunks = array_chunk($entities, 50);
    foreach ($chunks as $chunk) {
      $batch['operations'][] = [
        '\\Drupal\\acquia_contenthub\\AuditTrackEntities::batchProcess',
        [
          $chunk,
          $publish,
          'publisher_audit',
        ],
      ];
    }

    // Adds the batch sets.
    batch_set($batch);

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

  /**
   * Fetch Content Hub entities from export tracking table.
   *
   * @param string $status
   *   The status of the entities to audit.
   * @param string $entity_type_id
   *   The entity type.
   *
   * @return array
   *   Array containing list of entities.
   *
   * @throws \Exception
   */
  private function getContentHubTrackedEntities(string $status, $entity_type_id = '') : array {
    switch ($status) {
      case PublisherTracker::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.
        if ($this->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 or delete the queued items manually'));
        }
      case PublisherTracker::CONFIRMED:
      case PublisherTracker::EXPORTED:
        $entities = $this->tracker
          ->listTrackedEntities($status, $entity_type_id);
        break;
      default:
        throw new \Exception(dt('You can only use the following values for status: exported, confirmed, queued.'));
    }
    return $entities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiaContentHubPublisherAuditCommands::$queue protected property The queue object.
AcquiaContentHubPublisherAuditCommands::$tracker protected property The published entity tracker.
AcquiaContentHubPublisherAuditCommands::audit public function Audits exported entities and republishes if inconsistencies are found.
AcquiaContentHubPublisherAuditCommands::getContentHubTrackedEntities private function Fetch Content Hub entities from export tracking table.
AcquiaContentHubPublisherAuditCommands::__construct public function AcquiaContentHubPublisherAuditCommands constructor.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2