You are here

public function BynderApi::getTags in Bynder 8.3

Same name and namespace in other branches
  1. 8 src/BynderApi.php \Drupal\bynder\BynderApi::getTags()
  2. 8.2 src/BynderApi.php \Drupal\bynder\BynderApi::getTags()
  3. 4.0.x src/BynderApi.php \Drupal\bynder\BynderApi::getTags()

Wraps getTags() and makes sure results are cached properly.

1 method overrides BynderApi::getTags()
BynderApiTest::getTags in tests/modules/bynder_test_module/src/BynderApiTest.php
Returns value set in state.

File

src/BynderApi.php, line 338

Class

BynderApi
Bynder API service.

Namespace

Drupal\bynder

Code

public function getTags($query = []) {
  $bynder_configuration = $this->configFactory
    ->get('bynder.settings');
  if ($bynder_configuration
    ->get('debug')) {
    $this->loggerFactory
      ->get('bynder')
      ->debug('Method: %method is called with arguments: @args', [
      '%method' => 'getTags',
      '@args' => print_r($query, TRUE),
    ]);
  }
  try {
    if (empty($args['keyword'])) {
      $query_hash = md5(serialize($query));
      $allow_expired = FALSE;
      foreach (static::AUTO_UPDATED_TAGS_QUERIES as $candidate) {
        if (md5(serialize($candidate)) === $query_hash) {
          $allow_expired = TRUE;
          break;
        }
      }
      if ($cache_item = $this->cache
        ->get(self::CID_TAGS . '_' . $query_hash, $allow_expired)) {
        return $cache_item->data;
      }
      else {
        $result = [];

        // Ensure all results are fetched so start with page 1
        $query['page'] = 1;

        // Set the limit to the default if not provided.
        if (!isset($query['limit'])) {
          $query['limit'] = 50;
        }
        do {
          $items = $this
            ->getAssetBankManager()
            ->getTags($query)
            ->wait();
          $result = array_merge($result, $items);
          $query['page']++;

          // Continue fetching results until the response is empty, or the
          // number of items returned is less than the requested limit.
        } while (!empty($items) && count($items) == $query['limit']);
        $this->cache
          ->set(self::CID_TAGS . '_' . $query_hash, $result, $this->configFactory
          ->get('bynder.settings')
          ->get('cache_lifetime') + $this->time
          ->getRequestTime());
        return $result;
      }
    }
    else {
      $result = $this
        ->getAssetBankManager()
        ->getTags($query)
        ->wait();
    }
    if ($bynder_configuration
      ->get('debug')) {
      $this->loggerFactory
        ->get('bynder')
        ->debug('Method: %method returns: @result', [
        '%method' => 'getTags',
        '@result' => print_r($result, TRUE),
      ]);
    }
    return $result;
  } catch (\Exception $e) {
    if ($bynder_configuration
      ->get('debug')) {
      $this->loggerFactory
        ->get('bynder')
        ->error('Method: %method throws error with message: @message', [
        '%method' => 'getTags',
        '@message' => $e
          ->getMessage(),
      ]);
    }
    throw $e;
  }
}