You are here

protected function ContentHubCommonActions::validateDocument in Acquia Content Hub 8.2

Validate the expected number of retrieved entities.

Parameters

\Acquia\ContentHubClient\CDFDocument $document: The CDFDocument object.

array $uuids: The list of expected uuids.

Throws

\Drupal\acquia_contenthub_subscriber\Exception\ContentHubImportException

1 call to ContentHubCommonActions::validateDocument()
ContentHubCommonActions::getCdfDocument in src/ContentHubCommonActions.php
Retrieves entities and dependencies by uuid and returns a CDFDocument.

File

src/ContentHubCommonActions.php, line 243

Class

ContentHubCommonActions
Common actions across the entirety of Content Hub.

Namespace

Drupal\acquia_contenthub

Code

protected function validateDocument(CDFDocument $document, array $uuids) {
  $cdf_objects = $document
    ->getEntities();
  $uuids_count = count($uuids);
  $document_count = count($cdf_objects);
  $message = '';
  if ($uuids_count <= $document_count) {
    return;
  }
  $diff_uuids = array_diff($uuids, array_keys($cdf_objects));
  foreach ($uuids as $uuid) {
    $cdf_object = $document
      ->getCdfEntity($uuid);
    if (!$cdf_object) {
      $message .= sprintf("The entity with UUID = \"%s\" could not be imported because it is missing from Content Hub.", $uuid) . PHP_EOL;
      continue;
    }
    $dependencies = $cdf_object
      ->getDependencies();
    $vanished_uuids = array_intersect($diff_uuids, array_keys($dependencies));
    if (!empty($vanished_uuids)) {
      $type = $cdf_objects[$uuid]
        ->getAttribute('entity_type')
        ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
      $origin = $cdf_objects[$uuid]
        ->getOrigin();

      // Using array_keys() to only pass the dependency UUIDs, not the hashes.
      $this
        ->requestToRepublishEntity($origin, $type, $uuid, array_keys($dependencies));
      $message .= sprintf("The entity (%s, %s) could not be imported because the following dependencies are missing from Content Hub: %s.", $type, $uuid, implode(', ', $vanished_uuids)) . PHP_EOL;
    }
  }
  $exception = new ContentHubImportException($message, 100);
  $exception
    ->setUuids($diff_uuids);
  throw $exception;
}