Files
kupshop/bundles/KupShop/SynchronizationBundle/Util/SynchronizationConfiguration.php
2025-08-02 16:30:27 +02:00

58 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\SynchronizationBundle\Util;
use KupShop\SynchronizationBundle\Exception\SynchronizationConfigException;
class SynchronizationConfiguration
{
private array $configLoader = [];
public function set(string $key, callable $loader, int $ttl = 3600): void
{
$this->configLoader[$key] = [
'loader' => $loader,
'ttl' => $ttl,
];
}
public function has(string $key): bool
{
return isset($this->configLoader[$key]);
}
public function get(string $key)
{
if (!$this->has($key)) {
throw new SynchronizationConfigException(sprintf('Key "%s" does not exists ibn configuration!', $key));
}
if (!($config = getCache($this->getCacheKey($key)))) {
// naloaduju config
$config = $this->configLoader[$key]['loader']();
// zacahuje config
setCache($this->getCacheKey($key), $config, $this->configLoader[$key]['ttl']);
}
return $config;
}
public function clear(string $key): bool
{
if ($this->has($key) || getCache($this->getCacheKey($key))) {
clearCache($this->getCacheKey($key));
return true;
}
return false;
}
private function getCacheKey(string $key): string
{
return 'synchronizationConfig_'.$key;
}
}