first commit
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user