You are here

public function UniqueQueueItem::addItem in TMGMT Extension Suite 8.2

Same name and namespace in other branches
  1. 8.3 src/Utils/UniqueQueueItem.php \Drupal\tmgmt_extension_suit\Utils\UniqueQueueItem::addItem()
  2. 8 src/Utils/UniqueQueueItem.php \Drupal\tmgmt_extension_suit\Utils\UniqueQueueItem::addItem()

Adds item into a queue only if it's unique.

Parameters

string $queue_name: Queue name.

array $data: Data to be added.

bool $force: Force adding item into the queue.

File

src/Utils/UniqueQueueItem.php, line 47
Contains QueueUniqueItem service class.

Class

UniqueQueueItem

Namespace

Drupal\tmgmt_extension_suit\Utils

Code

public function addItem($queue_name, $data, $force = FALSE) {
  $serialized_data = serialize($data);
  if (!$force) {
    $count = $this->databaseConnection
      ->select('queue', 'q')
      ->condition('q.name', $queue_name)
      ->condition('q.data', $serialized_data)
      ->countQuery()
      ->execute()
      ->fetchField();
    if ($count != 0) {
      return;
    }
  }
  $result = $this->queueFactory
    ->get($queue_name)
    ->createItem($data);
  if ($result) {
    $this->logger
      ->info('New unique item has been added into the "@queue" queue. Serialized queue item: @item, forced: @forced.', [
      '@queue' => $queue_name,
      '@item' => $serialized_data,
      '@forced' => (int) $force,
    ]);
  }
}