You are here

public function LockrKeyProvider::getKeyValue in Lockr 8.4

Same name and namespace in other branches
  1. 8.2 src/Plugin/KeyProvider/LockrKeyProvider.php \Drupal\lockr\Plugin\KeyProvider\LockrKeyProvider::getKeyValue()
  2. 8.3 src/Plugin/KeyProvider/LockrKeyProvider.php \Drupal\lockr\Plugin\KeyProvider\LockrKeyProvider::getKeyValue()
  3. 4.x src/Plugin/KeyProvider/LockrKeyProvider.php \Drupal\lockr\Plugin\KeyProvider\LockrKeyProvider::getKeyValue()

Returns the value of a key.

Parameters

\Drupal\key\KeyInterface $key: The key whose value will be retrieved.

Return value

string The key value.

Overrides KeyProviderInterface::getKeyValue

File

src/Plugin/KeyProvider/LockrKeyProvider.php, line 167
Contains Drupal\lockr\Plugin\KeyProvider\LockrKeyProvider.

Class

LockrKeyProvider
Adds a key provider that allows a key to be stored in Lockr.

Namespace

Drupal\lockr\Plugin\KeyProvider

Code

public function getKeyValue(KeyInterface $key) {
  $key_id = $this
    ->getLockrSecretName($key
    ->id());
  try {
    return $this->lockr
      ->getSecretValue($key_id);
  } catch (\Exception $e) {
    if ($e
      ->getCode() === 404) {
      $key_type = $key
        ->getKeyType();
      if ($key_type
        ->getPluginId() === 'lockr_encryption') {
        $key_size = (int) $key_type
          ->getConfiguration()['key_size'];
        $new_value = $this->lockr
          ->generateKey($key_size);
        try {
          $this
            ->setKeyValue($key, $new_value);
        } catch (\Exception $e) {
          $this
            ->logException($e);
          return NULL;
        }
        return $new_value;
      }
    }
    $this
      ->logException($e);
    return NULL;
  }
}