You are here

class ContentHubImportQueueByFilter in Acquia Content Hub 8.2

Implements an Import Queue for entites based on custom filters.

Hierarchy

Expanded class hierarchy of ContentHubImportQueueByFilter

2 files declare their use of ContentHubImportQueueByFilter
ContentHubImportQueueForm.php in modules/acquia_contenthub_subscriber/src/Form/ContentHubImportQueueForm.php
ImportFromFiltersTest.php in tests/src/Kernel/ImportFromFiltersTest.php
1 string reference to 'ContentHubImportQueueByFilter'
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 ContentHubImportQueueByFilter
acquia_contenthub_subscriber.acquia_contenthub_import_queue_by_filter in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
Drupal\acquia_contenthub_subscriber\ContentHubImportQueueByFilter

File

modules/acquia_contenthub_subscriber/src/ContentHubImportQueueByFilter.php, line 14

Namespace

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

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

  /**
   * Subscriber Import Queue from filters.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $filterQueue;

  /**
   * ContentHubImportQueueByFilter constructor.
   *
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   *   Drupal Queue Factory.
   * @param \Drupal\Core\Queue\QueueWorkerManagerInterface $queue_worker_manager
   *   Queue Worker manager.
   */
  public function __construct(QueueFactory $queue_factory, QueueWorkerManagerInterface $queue_worker_manager) {
    $this->queueWorkerManager = $queue_worker_manager;
    $this->filterQueue = $queue_factory
      ->get('acquia_contenthub_import_from_filters');
  }

  /**
   * Define batch process which handles the creation of import queues.
   *
   * @param array $filter_uuids
   *   Array of cloud filter uuids.
   */
  public function process(array $filter_uuids) {
    $batch = [
      'title' => $this
        ->t('Process all entities to be queued for import'),
      'operations' => [],
      'finished' => [
        [
          $this,
          'batchFinished',
        ],
        [],
      ],
    ];
    foreach ($filter_uuids as $filter_uuid) {
      $data = new \stdClass();
      $data->filter_uuid = $filter_uuid;
      $this->filterQueue
        ->createItem($data);
      $batch['operations'][] = [
        [
          $this,
          'batchProcess',
        ],
        [],
      ];
    }
    batch_set($batch);
  }

  /**
   * Process the batch.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function batchProcess() {
    $filter_queue_worker = $this->queueWorkerManager
      ->createInstance('acquia_contenthub_import_from_filters');
    if ($item = $this->filterQueue
      ->claimItem()) {
      try {
        $filter_queue_worker
          ->processItem($item->data);
        $this->filterQueue
          ->deleteItem($item);
      } catch (SuspendQueueException $exception) {
        $context['errors'][] = $exception
          ->getMessage();
        $context['success'] = FALSE;
        $this->filterQueue
          ->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('Processed cloud filters.');
      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
ContentHubImportQueueByFilter::$filterQueue protected property Subscriber Import Queue from filters.
ContentHubImportQueueByFilter::$queueWorkerManager protected property The Queue Worker.
ContentHubImportQueueByFilter::batchFinished public static function Batch finish callback.
ContentHubImportQueueByFilter::batchProcess public function Process the batch.
ContentHubImportQueueByFilter::process public function Define batch process which handles the creation of import queues.
ContentHubImportQueueByFilter::__construct public function ContentHubImportQueueByFilter constructor.
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.