You are here

public function ContentHubImportQueueWorker::processItem in Acquia Content Hub 8.2

Processes acquia_contenthub_subscriber_import queue items.

Parameters

mixed $data: The data in the queue.

Throws

\Exception

Overrides QueueWorkerInterface::processItem

File

modules/acquia_contenthub_subscriber/src/Plugin/QueueWorker/ContentHubImportQueueWorker.php, line 147

Class

ContentHubImportQueueWorker
Queue worker for importing entities.

Namespace

Drupal\acquia_contenthub_subscriber\Plugin\QueueWorker

Code

public function processItem($data) : void {
  $this
    ->initializeClient();
  if (!$this->client) {
    $this->achLoggerChannel
      ->error('Acquia Content Hub client cannot be initialized because connection settings are empty.');
    return;
  }
  $settings = $this->factory
    ->getSettings();
  $webhook = $settings
    ->getWebhook('uuid');
  try {
    $interests = $this->client
      ->getInterestsByWebhook($webhook);
  } catch (\Exception $exception) {
    $this->achLoggerChannel
      ->error(sprintf('Following error occurred while we were trying to get the interest list: %s', $exception
      ->getMessage()));
    return;
  }
  $config = $this->configFactory
    ->get('acquia_contenthub.admin_settings');
  $send_update = $config
    ->get('send_contenthub_updates') ?? TRUE;
  $process_items = explode(', ', $data->uuids);

  // Get rid of items potentially deleted from the interest list.
  $uuids = array_intersect($process_items, $interests);
  if (count($uuids) !== count($process_items)) {

    // Log the uuids no longer on the interest list for this webhook.
    $missing_uuids = array_diff($process_items, $uuids);
    $this->achLoggerChannel
      ->info(sprintf('Skipped importing the following missing entities: %s. This occurs when entities are deleted at the Publisher before importing.', implode(', ', $missing_uuids)));
  }
  if (!$uuids) {
    $this->achLoggerChannel
      ->info('There are no matching entities in the queues and the site interest list.');
    return;
  }
  try {
    $stack = $this->common
      ->importEntities(...$uuids);

    // Reinitialize the client to refresh the client CDF metrics.
    $this->client = $this->factory
      ->getClient();
  } catch (ContentHubImportException $e) {

    // Get UUIDs.
    $e_uuids = $e
      ->getUuids();
    if (array_diff($uuids, $e_uuids) == array_diff($e_uuids, $uuids) && $e
      ->isEntitiesMissing()) {

      // The UUIDs can't be imported since they aren't in the Service.
      // The missing UUIDs are the same as the ones that were sent for import.
      if ($webhook) {
        foreach ($uuids as $uuid) {
          try {
            if (!$this->tracker
              ->getEntityByRemoteIdAndHash($uuid)) {

              // If we cannot load, delete interest and tracking record.
              if ($send_update) {
                $this->client
                  ->deleteInterest($uuid, $webhook);
              }
              $this->tracker
                ->delete($uuid);
              $this->achLoggerChannel
                ->info(sprintf('The following entity was deleted from interest list and tracking table: %s', $uuid));
            }
          } catch (\Exception $ex) {
            $this->achLoggerChannel
              ->error(sprintf('Entity deletion from tracking table and interest list failed. Entity: %s. Message: %s', $uuid, $ex
              ->getMessage()));
          }
          return;
        }
      }
    }
    else {

      // There are import problems but probably on dependent entities.
      $this->achLoggerChannel
        ->error(sprintf('Import failed: %s.', $e
        ->getMessage()));
      throw $e;
    }
  }
  if ($webhook && $send_update) {
    try {
      $this->client
        ->addEntitiesToInterestList($webhook, array_keys($stack
        ->getDependencies()));
      $this->achLoggerChannel
        ->info(sprintf('The following imported entities have been added to the interest list on Content Hub for webhook "%s": [%s].', $webhook, implode(', ', $uuids)));
    } catch (\Exception $e) {
      $this->achLoggerChannel
        ->error(sprintf('Error adding the following entities to the interest list for webhook "%s": [%s]. Error message: "%s".', $webhook, implode(', ', $uuids), $e
        ->getMessage()));
    }
  }
}