View source
<?php
namespace Drupal\apigee_edge\Plugin\KeyProvider;
use Drupal\apigee_edge\Exception\KeyProviderRequirementsException;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Utility\Error;
use Drupal\key\KeyInterface;
use Drupal\key\Plugin\KeyPluginFormInterface;
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PrivateFileKeyProvider extends KeyProviderRequirementsBase implements KeyPluginFormInterface, KeyProviderSettableValueInterface {
private $fileSystem;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.channel.apigee_edge'), $container
->get('file_system'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
public function realGetKeyValue(KeyInterface $key) {
if ($key
->isNew()) {
return NULL;
}
return file_get_contents($this
->getFileUri($key)) ?: NULL;
}
public function setKeyValue(KeyInterface $key, $key_value) {
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);
$this
->getFileSystem()
->prepareDirectory($file_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
try {
return $this
->getFileSystem()
->saveData($key_value, $file_uri, FileSystemInterface::EXISTS_REPLACE);
} catch (FileException $e) {
return FALSE;
}
}
public function deleteKeyValue(KeyInterface $key) {
try {
$this
->getFileSystem()
->delete($this
->getFileUri($key));
} catch (FileException $e) {
}
}
public function checkRequirements(KeyInterface $key) : void {
$file_private_path = $this
->getFileSystem()
->realpath('private://');
if (!(bool) $file_private_path) {
throw new KeyProviderRequirementsException('Private filesystem has not been configured yet.', $this
->t("Private filesystem has not been configured yet. <a href=':file_docs_uri' target='_blank'>Learn more</a>", [
':file_docs_uri' => 'https://www.drupal.org/docs/8/modules/apigee-edge/configure-the-connection-to-apigee-edge#configure-private-file',
]));
}
if (!is_writable($file_private_path)) {
throw new KeyProviderRequirementsException('The private file path is not writable.', $this
->t('The private file path is not writable.'));
}
if (!is_dir($file_private_path)) {
throw new KeyProviderRequirementsException('The private file path does not exist.', $this
->t('The private file path does not exist.'));
}
}
protected function getFileUri(KeyInterface $key) : string {
return "private://.apigee_edge/{$key->id()}.json";
}
protected function getFileSystem() : FileSystemInterface {
return $this->fileSystem ?? \Drupal::service('file_system');
}
}