You are here

public function PrivateFileKeyProvider::setKeyValue in Apigee Edge 8

Sets the value of a key.

Parameters

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

string $key_value: The key value.

Return value

bool TRUE if successful, FALSE if unsuccessful.

Overrides KeyProviderSettableValueInterface::setKeyValue

File

src/Plugin/KeyProvider/PrivateFileKeyProvider.php, line 118

Class

PrivateFileKeyProvider
Stores Apigee Edge authentication credentials in a private file.

Namespace

Drupal\apigee_edge\Plugin\KeyProvider

Code

public function setKeyValue(KeyInterface $key, $key_value) {

  // Throwing an exception would be better than returning FALSE but the key
  // module's design does not allow this.
  // Related issue: https://www.drupal.org/project/key/issues/3038212
  try {
    $this
      ->checkRequirements($key);
  } catch (KeyProviderRequirementsException $exception) {
    $context = [
      '@message' => (string) $exception,
    ];
    $context += Error::decodeException($exception);
    $this
      ->getLogger()
      ->error('Could not save Apigee Edge authentication key value in the private file storage: @message %function (line %line of %file). <pre>@backtrace_string</pre>', $context);
    return FALSE;
  }
  $file_uri = $this
    ->getFileUri($key);
  $file_path = $this
    ->getFileSystem()
    ->dirname($file_uri);

  // Make sure the folder exists.
  $this
    ->getFileSystem()
    ->prepareDirectory($file_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  try {

    // Save the token data.
    return $this
      ->getFileSystem()
      ->saveData($key_value, $file_uri, FileSystemInterface::EXISTS_REPLACE);
  } catch (FileException $e) {
    return FALSE;
  }
}