File: //proc/self/cwd/nueva/modules/wnetsecurity/src/Repository/Configuration/ConfigurationRepository.php
<?php
/**
* Copyright since 2014 Waynet Sp. z o.o.
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@waynet.pl so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop-project.org/ for more information.
*
* @author Waynet Sp. z o.o. <kontakt@waynet.pl>
* @copyright since 2014 Waynet Sp. z o.o.
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
declare(strict_types=1);
namespace Waynet\Security\Repository\Configuration;
use Waynet\Security\Adapter\ConfigurationAdapter;
use Waynet\Security\Cache\ArrayCache;
use Waynet\Security\Cache\CacheInterface;
use Waynet\Security\Exception\TransactionFailedException;
use Waynet\Security\Exception\UnableToDecryptDataException;
use Waynet\Security\Repository\Traits\RunTransactionTrait;
abstract class ConfigurationRepository
{
use RunTransactionTrait;
private $adapter;
private $cache;
private $encryptionHandler;
public function __construct(
\Db $db,
ConfigurationAdapter $adapter,
CacheInterface $cache = null,
\PhpEncryption $encryptionHandler = null
) {
$this->db = $db;
$this->adapter = $adapter;
$this->cache = $cache ?? new ArrayCache();
$this->encryptionHandler = $encryptionHandler ?? new \PhpEncryption(_NEW_COOKIE_KEY_);
}
/** @return mixed */
protected function get(string $key, bool $global = true)
{
return $this->adapter->get($key, $global);
}
/**
* @param mixed $value
*/
protected function set(string $key, $value, bool $global = true): bool
{
return $this->adapter->set($key, $value, $global);
}
protected function delete(string $key): bool
{
return $this->adapter->delete($key);
}
/**
* @throws UnableToDecryptDataException|\Exception
*/
protected function getEncrypted(string $key, bool $global = true): ?string
{
if ($this->cache->has($key)) {
return $this->cache->get($key);
}
$encrypted = $this->get($key, $global);
$value = $this->decrypt($encrypted, $key);
$this->cache->set($key, $value);
return $value;
}
/**
* @param mixed $value
*/
protected function setEncrypted(string $key, $value, bool $global = true): bool
{
$encrypted = null === $value
? null
: $this->encryptionHandler->encrypt($key . $value);
if (!$this->set($key, $encrypted, $global)) {
return false;
}
$this->cache->set($key, $value);
return true;
}
protected function getTransactionResult(callable $callback, ...$arguments): bool
{
try {
$this->runTransaction($callback, ...$arguments);
return true;
} catch (TransactionFailedException $exception) {
return false;
}
}
/**
* @throws UnableToDecryptDataException|\Exception
*/
private function decrypt(?string $encrypted, string $key): ?string
{
if (!$encrypted) {
return null;
}
if (false === $decrypted = $this->encryptionHandler->decrypt($encrypted)) {
throw new UnableToDecryptDataException(sprintf('Could not decrypt data under configuration key "%s"', $key));
}
return preg_replace("/^$key/", '', $decrypted);
}
}