magento2-plugin/Storage/EncryptedConfigStorage.php
Wouter Samaey 25ecd61284 WIP
2019-07-24 23:01:24 +02:00

85 lines
2.2 KiB
PHP

<?php
/**
* ConfigStorage
*
* @copyright Copyright © 2019 Storefront bvba. All rights reserved.
* @author info@storefront.be
*/
namespace Storefront\BTCPay\Storage;
use BTCPayServer\Storage\KeyInterface;
use BTCPayServer\Storage\StorageInterface;
class EncryptedConfigStorage implements StorageInterface {
/**
* @var \Magento\Framework\Encryption\EncryptorInterface
*/
private $encryptor;
/**
* @var \Magento\Framework\App\Config\ValueFactory
*/
private $configValueFactory;
/**
* @var \Magento\Framework\App\Config\ConfigResource\ConfigInterface
*/
private $configResource;
public function __construct(\Magento\Framework\Encryption\EncryptorInterface $encryptor, \Magento\Framework\App\Config\ConfigResource\ConfigInterface $configResource, \Magento\Framework\App\Config\ValueFactory $configValueFactory) {
$this->configValueFactory = $configValueFactory;
$this->encryptor = $encryptor;
$this->configResource = $configResource;
}
private function getConfigKey($id) {
return 'btcpay/keys/' . $id;
}
/**
* @param KeyInterface $key
*/
public function persist(\BTCPayServer\KeyInterface $key) {
$unencrypted = serialize($key);
$encrypted = $this->encryptor->encrypt($unencrypted);
$configKey = $this->getConfigKey($key->getId());
$this->configResource->saveConfig($configKey, $encrypted);
}
/**
* @param string $id
*
* @return KeyInterface
*/
public function load($id) {
$configKey = $this->getConfigKey($id);
$encrypted = $this->getConfigWithoutCache($configKey);
$decrypted = $this->encryptor->decrypt($encrypted);
$key = unserialize($decrypted);
return $key;
}
private function getConfigWithoutCache($path) {
/* @var $dataCollection \Magento\Config\Model\ResourceModel\Config\Data\Collection */
$dataCollection = $this->configValueFactory->create()->getCollection();
$dataCollection->addFieldToFilter('path', ['eq' => $path]);
$row = $dataCollection->getFirstItem();
if ($row->getId()) {
return $row->getValue();
} else {
return false;
}
}
}