You are here

public function AcquiaContenthubCommands::contenthubCompare in Acquia Content Hub 8

Loads the CDF from a local and remote source, compares them and prints the differences.

@command acquia:contenthub-compare @aliases ach-comp,acquia-contenthub-compare

Parameters

$entity_type: Entity type

$uuid: Entity's UUID

File

src/Commands/AcquiaContenthubCommands.php, line 118

Class

AcquiaContenthubCommands
A Drush commandfile.

Namespace

Drupal\acquia_contenthub\Commands

Code

public function contenthubCompare($entity_type, $uuid) {
  $entity_type_manager = \Drupal::entityTypeManager();
  if (!$entity_type_manager
    ->getDefinition($entity_type)) {
    throw new \Exception(dt("The entity type provided does not exist."));
  }
  if (!Uuid::isValid($uuid)) {
    throw new \Exception(dt("Argument provided is not a UUID."));
  }

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');

  /** @var \Drupal\Core\Entity\EntityRepository $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');

  /** @var \Drupal\acquia_contenthub\Client\ClientManager $client_manager */
  $client_manager = \Drupal::service('acquia_contenthub.client_manager');
  $client = $client_manager
    ->getConnection();

  // Get our local CDF version.
  $local_entity = $entity_repository
    ->loadEntityByUuid($entity_type, $uuid);
  $local_cdf = $serializer
    ->normalize($local_entity, 'acquia_contenthub_cdf');
  if (!$local_cdf) {

    // Basic structure we'll reference for the diff.
    $local_cdf = [
      'entities' => [
        [],
      ],
    ];
  }
  else {
    $local_cdf = $this
      ->handleNormalizedData($local_cdf);
  }

  // Get the Remote CDF version.
  $remote_cdf = $client
    ->readEntity($uuid);
  if (!$remote_cdf) {
    $remote_cdf = [];
  }
  $local_compare = array_diff($local_cdf['entities'][0], (array) $remote_cdf);
  $this
    ->output()
    ->writeln("Data from the local entity that doesn't appear in the remote entity, retrieved from Content Hub Backend:");
  $this
    ->output()
    ->writeln(json_encode($local_compare, JSON_PRETTY_PRINT));
  $this
    ->output()
    ->writeln("Data from the remote entity that doesn't appear in the local entity:");
  $remote_compare = array_diff((array) $remote_cdf, $local_cdf);
  $this
    ->output()
    ->writeln(json_encode($remote_compare, JSON_PRETTY_PRINT));
}