You are here

QueueHelper.php in Entity Share 8.2

Same filename and directory in other branches
  1. 8.3 modules/entity_share_async/src/Service/QueueHelper.php

File

modules/entity_share_async/src/Service/QueueHelper.php
View source
<?php

declare (strict_types=1);
namespace Drupal\entity_share_async\Service;

use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\State\StateInterface;

/**
 * Class QueueHelper.
 *
 * @package Drupal\entity_share_async\Service
 */
class QueueHelper implements QueueHelperInterface {

  /**
   * The queue factory service.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * QueueHelper constructor.
   *
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   *   The queue factory service.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(QueueFactory $queue_factory, StateInterface $state) {
    $this->queueFactory = $queue_factory;
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public function enqueue($remote_id, $channel_id, array $uuids) {

    /** @var \Drupal\Core\Queue\QueueInterface $queue */
    $queue = $this->queueFactory
      ->get(QueueHelperInterface::QUEUE_NAME);
    $async_states = $this->state
      ->get(QueueHelperInterface::STATE_ID, []);
    foreach ($uuids as $uuid) {
      if (!isset($async_states[$remote_id][$channel_id][$uuid])) {

        // Add the entity to the async states.
        $async_states[$remote_id][$channel_id][$uuid] = $uuid;

        // Create queue item.
        $item = [
          'uuid' => $uuid,
          'remote_id' => $remote_id,
          'channel_id' => $channel_id,
        ];
        $queue
          ->createItem($item);
      }
    }

    // Update states.
    $this->state
      ->set(QueueHelperInterface::STATE_ID, $async_states);
  }

}

Classes

Namesort descending Description
QueueHelper Class QueueHelper.