You are here

class ContentHubImportQueue in Acquia Content Hub 8.2

Implements an Import Queue for entities.

Hierarchy

Expanded class hierarchy of ContentHubImportQueue

1 file declares its use of ContentHubImportQueue
ContentHubImportQueueForm.php in modules/acquia_contenthub_subscriber/src/Form/ContentHubImportQueueForm.php
1 string reference to 'ContentHubImportQueue'
acquia_contenthub_subscriber.services.yml in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
1 service uses ContentHubImportQueue
acquia_contenthub_subscriber.acquia_contenthub_import_queue in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
Drupal\acquia_contenthub_subscriber\ContentHubImportQueue

File

modules/acquia_contenthub_subscriber/src/ContentHubImportQueue.php, line 15

Namespace

Drupal\acquia_contenthub_subscriber
View source
class ContentHubImportQueue {
  use StringTranslationTrait;
  use DependencySerializationTrait;

  /**
   * Subscriber Import Queue.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * The Queue Worker.
   *
   * @var \Drupal\Core\Queue\QueueWorkerManager
   */
  protected $queueManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(QueueFactory $queue_factory, QueueWorkerManager $queue_manager) {
    $this->queue = $queue_factory
      ->get('acquia_contenthub_subscriber_import');
    $this->queueManager = $queue_manager;
  }

  /**
   * Obtains the number of items in the import queue.
   *
   * @return mixed
   *   The number of items in the import queue.
   */
  public function getQueueCount() {
    return $this->queue
      ->numberOfItems();
  }

  /**
   * Handle the route to create a batch process.
   */
  public function process() {
    $batch = [
      'title' => $this
        ->t('Process all entities to be imported'),
      'operations' => [],
      'finished' => [
        [
          $this,
          'batchFinished',
        ],
        [],
      ],
    ];

    // Count number of the items in this queue, create enough batch operations.
    for ($i = 0; $i < $this
      ->getQueueCount(); $i++) {

      // Create batch operations.
      $batch['operations'][] = [
        [
          $this,
          'batchProcess',
        ],
        [],
      ];
    }

    // Adds the batch sets.
    batch_set($batch);
  }

  /**
   * Process the batch.
   *
   * The batch worker will run through the queued items and process them
   * according to their queue method.
   *
   * @param mixed $context
   *   The batch context.
   */
  public function batchProcess(&$context) {
    $queueWorker = $this->queueManager
      ->createInstance('acquia_contenthub_subscriber_import');
    if ($item = $this->queue
      ->claimItem()) {
      try {
        $queueWorker
          ->processItem($item->data);
        $this->queue
          ->deleteItem($item);
      } catch (SuspendQueueException $exception) {
        $context['errors'][] = $exception
          ->getMessage();
        $context['success'] = FALSE;
        $this->queue
          ->releaseItem($item);
      } catch (EntityStorageException $exception) {
        $context['errors'][] = $exception
          ->getMessage();
        $context['success'] = FALSE;
        $this->queue
          ->releaseItem($item);
      }
    }
  }

  /**
   * Batch finish callback.
   *
   * This will inspect the results of the batch and will display a message to
   * indicate how the batch process ended.
   *
   * @param bool $success
   *   The result of batch process.
   * @param array $result
   *   The result of $context.
   * @param array $operations
   *   The operations that were run.
   */
  public static function batchFinished($success, array $result, array $operations) {
    if ($success) {
      \Drupal::messenger()
        ->addMessage(t("Processed all Content Hub entities."));
      return;
    }
    $error_operation = reset($operations);
    \Drupal::messenger()
      ->addMessage(t('An error occurred while processing @operation with arguments : @args', [
      '@operation' => $error_operation[0],
      '@args' => print_r($error_operation[0], TRUE),
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubImportQueue::$queue protected property Subscriber Import Queue.
ContentHubImportQueue::$queueManager protected property The Queue Worker.
ContentHubImportQueue::batchFinished public static function Batch finish callback.
ContentHubImportQueue::batchProcess public function Process the batch.
ContentHubImportQueue::getQueueCount public function Obtains the number of items in the import queue.
ContentHubImportQueue::process public function Handle the route to create a batch process.
ContentHubImportQueue::__construct public function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.