View source
<?php
namespace Drupal\lockr\Plugin\KeyProvider;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\key\KeyInterface;
use Drupal\key\Plugin\KeyPluginFormInterface;
use Drupal\key\Plugin\KeyProviderBase;
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
use Drupal\lockr\ClientFactory;
class LockrKeyProvider extends KeyProviderBase implements KeyProviderSettableValueInterface, KeyPluginFormInterface {
protected $client;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientFactory $client_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->clientFactory = $client_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('lockr.client_factory'));
}
public function defaultConfiguration() {
return [
'encoded' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$site_client = $this->clientFactory
->getSiteClient();
$status = $site_client
->exists();
if (!$status['exists']) {
$form['need_register'] = [
'#prefix' => '<p>',
'#markup' => $this
->t('This site has not yet registered with Lockr, please <a href="@link">click here to register</a>.', [
'@link' => Url::fromRoute('lockr.admin')
->toString(),
]),
'#suffix' => '</p>',
];
}
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function getKeyValue(KeyInterface $key) {
$name = $key
->id();
$encoded = $this
->getConfiguration()['encoded'];
$key_client = $this->clientFactory
->getKeyClient();
return $key_client
->encrypted($encoded)
->get($name);
}
public function setKeyValue(KeyInterface $key, $key_value) {
$name = $key
->id();
$label = $key
->label();
$encoded = $this
->getConfiguration()['encoded'] ?: NULL;
$key_client = $this->clientFactory
->getKeyClient();
$encoded = $key_client
->encrypted()
->set($name, $key_value, $label, $encoded);
if ($encoded) {
$this
->setConfiguration([
'encoded' => $encoded,
]);
return TRUE;
}
return FALSE;
}
public function deleteKeyValue(KeyInterface $key) {
$name = $key
->id();
$key_client = $this->clientFactory
->getKeyClient();
return $key_client
->delete($name);
}
}