You are here

public function OAuthKey::write in Lightning API 8.3

Same name and namespace in other branches
  1. 8.4 src/OAuthKey.php \Drupal\lightning_api\OAuthKey::write()
  2. 8 src/OAuthKey.php \Drupal\lightning_api\OAuthKey::write()
  3. 8.2 src/OAuthKey.php \Drupal\lightning_api\OAuthKey::write()

Writes a key to the file system.

Parameters

string $destination: The desired destination of the key. Can be a directory or a full path.

string $key: The data to write.

Return value

string The final path of the written key.

Throws

\RuntimeException If an I/O error occurred while writing the key.

File

src/OAuthKey.php, line 91

Class

OAuthKey
Generates key pairs for use with OAuth.

Namespace

Drupal\lightning_api

Code

public function write($destination, $key) {
  $destination = rtrim($destination, '/');
  if (is_dir($destination)) {
    $destination .= '/' . hash('sha256', $key) . '.key';
  }
  if (file_put_contents($destination, $key)) {
    $this->fileSystem
      ->chmod($destination, static::PERMISSIONS);
    return $destination;
  }
  else {
    throw new \RuntimeException('The key could not be written.');
  }
}