You are here

public function BynderApi::getTags in Bynder 8

Same name and namespace in other branches
  1. 8.3 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(implode($query));
      $allow_expired = FALSE;
      foreach (static::AUTO_UPDATED_TAGS_QUERIES as $candidate) {
        if (md5(implode($query)) == $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 = $this
          ->getAssetBankManager()
          ->getTags($query)
          ->wait();
        $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;
  }
}