View source
<?php
namespace Drupal\lockr;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\key\KeyRepositoryInterface;
use Lockr\SecretInfoInterface;
class SecretInfo implements SecretInfoInterface {
protected $configFactory;
protected $keyRepository;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, KeyRepositoryInterface $key_repository) {
$this->configFactory = $config_factory;
$this->secretStorage = $entity_type_manager
->getStorage('lockr_secret');
$this->keyRepository = $key_repository;
}
public function getSecretInfo($name) {
$secret = $this->secretStorage
->load($name);
if (!is_null($secret)) {
return $secret
->getInfo();
}
$config = $this->configFactory
->get('lockr.secret_info');
$info = $config
->get($name);
if (!$info) {
$key = $this->keyRepository
->getKey($name);
if ($key) {
$provider = $key
->getKeyProvider();
$config = $provider
->getConfiguration();
if (isset($config['encoded'])) {
return [
'wrapping_key' => $config['encoded'],
];
}
}
}
return $info ?: [];
}
public function setSecretInfo($name, array $info) {
$secret = $this->secretStorage
->load($name);
if (is_null($secret)) {
$secret = $this->secretStorage
->create([
'id' => $name,
'key_id' => $name,
]);
}
$secret
->setInfo($info);
$secret
->save();
}
public function getAllSecretInfo() {
$info = [];
foreach ($this->secretStorage
->loadMultiple(NULL) as $secret) {
$info[$secret
->id()] = $secret
->getInfo();
}
return $info;
}
}