You are here

function acquia_contenthub_subscriber_track_imported_linked_entities in Acquia Content Hub 8.2

Sends imported entities from legacy tracking table to interest list.

Parameters

mixed $context: The context array.

1 string reference to 'acquia_contenthub_subscriber_track_imported_linked_entities'
AcquiaContentHubSubscriberCommands::upgrade in modules/acquia_contenthub_subscriber/src/Commands/AcquiaContentHubSubscriberCommands.php
Subscriber Upgrade Command.

File

modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.migrate.inc, line 21
Post-update functions for Content Hub Subscriber.

Code

function acquia_contenthub_subscriber_track_imported_linked_entities(&$context) {
  $database = \Drupal::database();
  if (!$database
    ->schema()
    ->tableExists('acquia_contenthub_entities_tracking')) {
    return;
  }
  $import_status_auto_update = [
    'AUTO_UPDATE_ENABLED',
    'PENDING_SYNC',
    'QUEUED',
  ];
  $query = $database
    ->select('acquia_contenthub_entities_tracking', 't')
    ->fields('t', [
    'entity_id',
    'entity_uuid',
    'entity_type',
  ]);
  $query
    ->condition('status_import', $import_status_auto_update, 'IN');
  if (!isset($context['sandbox']['progress'])) {
    $max = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $context['sandbox']['progress'] = 0;
    $context['finished'] = 0;
    $context['sandbox']['max'] = $max;
  }
  $limit = 50;

  // Obtain the list of entities to enqueue.
  $results = $query
    ->range($context['sandbox']['progress'], $limit)
    ->execute();
  $uuids = [];
  $subscriber_tracker = \Drupal::service('acquia_contenthub_subscriber.tracker');
  $entity_repository = \Drupal::service('entity.repository');
  foreach ($results as $result) {
    $entity = $entity_repository
      ->loadEntityByUuid($result->entity_type, $result->entity_uuid);
    $subscriber_tracker
      ->track($entity, '');
    $uuids[] = $result->entity_uuid;

    // Updating progress.
    $context['sandbox']['progress']++;
  }

  /** @var \Drupal\acquia_contenthub\Client\ClientFactory $client_factory */
  $client_factory = \Drupal::service('acquia_contenthub.client.factory');
  $config = \Drupal::configFactory()
    ->get('acquia_contenthub.admin_settings');
  $webhook = $config
    ->get('webhook');
  $webhook_uuid = $webhook['uuid'] ?? NULL;
  if ($webhook_uuid && $uuids && ($config
    ->get('send_contenthub_updates') ?? TRUE)) {

    // Adding imported entities to interest list.
    $settings = $client_factory
      ->getSettings();
    $client_factory
      ->getClient($settings)
      ->addEntitiesToInterestList($webhook_uuid, $uuids);
    $logger = \Drupal::logger('acquia_contenthub_subscriber');
    $logger
      ->debug(dt('Added %count out of %total entities to the interest list.', [
      '%count' => $context['sandbox']['progress'],
      '%total' => $context['sandbox']['max'],
    ]));
  }

  // Did we finish yet?
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['finished'] = 1;

    // Delete all dependent entities. They will be imported as dependencies.
    $database
      ->delete('acquia_contenthub_entities_tracking')
      ->condition('status_import', 'IS_DEPENDENT', '=')
      ->execute();
    $database
      ->delete('acquia_contenthub_entities_tracking')
      ->condition('status_import', $import_status_auto_update, 'IN')
      ->execute();
  }
}