You are here

public function AuditTrackEntities::batchProcess in Acquia Content Hub 8.2

Checks published entities and compares them with Content Hub.

Parameters

array $entities: An array of records from the tracking table.

bool $reprocess: TRUE to reprocess entities, FALSE to just print.

string $audit_command: Commands trigger from which site pub or sub.

mixed $context: The batch context object.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/AuditTrackEntities.php, line 27

Class

AuditTrackEntities
Audit Pub/Sub track entities.

Namespace

Drupal\acquia_contenthub

Code

public function batchProcess(array $entities, bool $reprocess, string $audit_command, &$context) {
  $uuids = array_column($entities, 'entity_uuid');
  if (!isset($context['sandbox'])) {
    $context['results']['not_published'] = 0;
    $context['results']['outdated'] = 0;
  }

  /** @var \Drupal\acquia_contenthub\Client\ClientFactory $factory */
  $factory = \Drupal::service('acquia_contenthub.client.factory');
  if (!$factory
    ->getClient()) {
    throw new \Exception(dt('The Content Hub client is not connected so no operations could be performed.'));
  }

  /** @var \Acquia\ContentHubClient\CDF\CDFObject[] $cdfs */
  $cdfs = $factory
    ->getClient()
    ->getEntities($uuids)
    ->getEntities();
  foreach ($entities as $entity) {
    $out_of_sync = FALSE;
    $uuid = $entity['entity_uuid'];

    /** @var \Acquia\ContentHubClient\CDF\CDFObject $ch_entity */
    $ch_entity = $cdfs[$uuid] ?? FALSE;
    if (!$ch_entity) {

      // Entity does not exist in Content Hub.
      self::prepareOutput($entity, 'Entity not published.');
      $out_of_sync = TRUE;
      $context['results']['not_published']++;
    }
    elseif ($entity['hash'] !== $ch_entity
      ->getAttribute('hash')
      ->getValue()['und']) {

      // Entity exists in Content Hub but the hash flag does not match.
      self::prepareOutput($entity, 'Outdated entity.');
      $out_of_sync = TRUE;
      $context['results']['outdated']++;
    }
    if ($out_of_sync) {
      $drupal_entity = \Drupal::entityTypeManager()
        ->getStorage($entity['entity_type'])
        ->load($entity['entity_id']);
      if (!$drupal_entity) {

        // The drupal entity could not be loaded.
        self::prepareOutput($entity, 'This entity exists in the tracking table but could not be loaded in Drupal.');
        continue;
      }
      if ($reprocess) {
        self::enqueueTrackedEntities($drupal_entity, $audit_command);
      }
    }
  }
}