You are here

protected function TrafficRegistry::getTagIds in URLs queuer 8

Retrieve database IDs for the given set of tags or add missing.

Parameters

string[] $tags: Unassociative list of cache tags.

bool $add_missing: Add tags that are missing to the database.

Return value

string[] Associative array with ID as key and the tag as value.

Throws

\LogicException Thrown when $tags is left empty.

2 calls to TrafficRegistry::getTagIds()
TrafficRegistry::add in src/TrafficRegistry.php
Register a new URL or path with its associated cache tags at the registry.
TrafficRegistry::getUrls in src/TrafficRegistry.php
Collect URLs and paths associated with the given list of tags.

File

src/TrafficRegistry.php, line 156

Class

TrafficRegistry
Provides a database-driven traffic registry with URLs and tags.

Namespace

Drupal\purge_queuer_url

Code

protected function getTagIds(array $tags, $add_missing = TRUE) {
  if (empty($tags)) {
    throw new \LogicException('$path cannot be empty!');
  }

  // Define the closure that queries existing tags from the database.
  $load_from_db = function (&$tags, &$ids) {
    $db_results = $this->connection
      ->select('purge_queuer_url_tag', 't')
      ->fields('t', [
      'tagid',
      'tag',
    ])
      ->condition('tag', $tags, 'IN')
      ->execute();
    foreach ($db_results as $tag) {
      $ids[intval($tag->tagid)] = $tag->tag;
      unset($tags[array_search($tag->tag, $tags)]);
    }
  };

  // First attempt to load everything from the database.
  $ids = [];
  $load_from_db($tags, $ids);

  // When given tags don't exist, they're left in $tags.
  // Missing tags are left in $tags, add them to the database if needed.
  if (count($tags) && $add_missing) {
    $q = $this->connection
      ->insert('purge_queuer_url_tag')
      ->fields([
      'tag',
    ]);
    foreach ($tags as $tag) {
      $q
        ->values([
        'tag' => $tag,
      ]);
    }
    $q
      ->execute();
    $load_from_db($tags, $ids);
  }
  return $ids;
}