You are here

function acquia_contenthub_publisher_enqueue_exported_entities in Acquia Content Hub 8.2

Reads exported entities from old tracking table and enqueues them for export.

Parameters

mixed $context: The context object.

1 string reference to 'acquia_contenthub_publisher_enqueue_exported_entities'
AcquiaContentHubPublisherCommands::upgrade in modules/acquia_contenthub_publisher/src/Commands/AcquiaContentHubPublisherCommands.php
Publisher Upgrade Command.

File

modules/acquia_contenthub_publisher/acquia_contenthub_publisher.migrate.inc, line 14
Batch API functions to enqueue entities from legacy tracking table.

Code

function acquia_contenthub_publisher_enqueue_exported_entities(&$context) {
  if (!is_array($context) && !class_implements($context)['ArrayAccess']) {
    throw new \Exception("Queue context must be an array or an object extending ArrayObject");
  }
  $database = \Drupal::database();
  $logger = \Drupal::logger('acquia_contenthub_publisher');
  $query = $database
    ->select('acquia_contenthub_entities_tracking', 't')
    ->fields('t', [
    'entity_id',
    'entity_uuid',
    'entity_type',
  ]);
  $query
    ->condition('status_export', [
    'EXPORTED',
    'INITIATED',
    'QUEUED',
  ], 'IN');
  if (!isset($context['sandbox']['progress'])) {
    $max = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $context['sandbox']['progress'] = 0;
    $context['finished'] = 0;
    $context['sandbox']['max'] = $max;
    $context['results']['enqueued'] = 0;
    $context['results']['max'] = $max;
  }
  $limit = 50;
  $entity_type_manager = $entity = \Drupal::entityTypeManager();

  // Obtain the list of entities to enqueue.
  $results = $query
    ->range($context['sandbox']['progress'], $limit)
    ->execute();
  foreach ($results as $result) {

    // Enqueue entity if it is not paragraphs.
    if ($result->entity_type !== 'paragraph') {
      $entity = $entity_type_manager
        ->getStorage($result->entity_type)
        ->load($result->entity_id);
      if ($entity) {
        _acquia_contenthub_publisher_enqueue_entity($entity, 'update');
        $logger
          ->info('Enqueue-ing entity for export: @entity_type - @entity_id', [
          '@entity_type' => $result->entity_type,
          '@entity_id' => $result->entity_id,
        ]);
        $context['results']['enqueued']++;
      }
      else {
        $logger
          ->warning('Upgrade : Entity (type = @entity_type, id = @entity_id, uuid = @uuid) could not be loaded. It cannot be enqueued for export.', [
          '@entity_type' => $result->entity_type,
          '@entity_id' => $result->entity_id,
          '@uuid' => $result->entity_uuid,
        ]);
      }
    }

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

  // Did we finish yet?
  $context['finished'] = $context['sandbox']['max'] == 0 ? 1 : $context['sandbox']['progress'] / $context['sandbox']['max'];
  $message = dt('Processed @progress out of @total entities', [
    '@progress' => $context['sandbox']['progress'],
    '@total' => $context['sandbox']['max'],
  ]);
  $logger
    ->warning($message);
}