| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Config; |
| 6 |
|
| 7 |
use Exception; |
| 8 |
use UserAccessManager\Wrapper\Wordpress; |
| 9 |
|
| 10 |
class Config |
| 11 |
{ |
| 12 |
protected string $key; |
| 13 |
protected array $wpOptions = []; |
| 14 |
/** |
| 15 |
* @var ConfigParameter[] |
| 16 |
*/ |
| 17 |
protected array $defaultConfigParameters = []; |
| 18 |
/** |
| 19 |
* @var null|ConfigParameter[] |
| 20 |
*/ |
| 21 |
protected ?array $configParameters = null; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
private Wordpress $wordpress, |
| 25 |
string $key |
| 26 |
) { |
| 27 |
$this->key = $key; |
| 28 |
} |
| 29 |
|
| 30 |
public function getWpOption(string $option): mixed |
| 31 |
{ |
| 32 |
if (!isset($this->wpOptions[$option]) === true) { |
| 33 |
$this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 34 |
} |
| 35 |
|
| 36 |
return $this->wpOptions[$option]; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @return ConfigParameter[] |
| 41 |
*/ |
| 42 |
protected function getDefaultConfigParameters(): array |
| 43 |
{ |
| 44 |
return $this->defaultConfigParameters; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param ConfigParameter[] $defaultConfigParameters |
| 49 |
*/ |
| 50 |
public function setDefaultConfigParameters(array $defaultConfigParameters): void |
| 51 |
{ |
| 52 |
$this->defaultConfigParameters = $defaultConfigParameters; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @return ConfigParameter[] |
| 57 |
*/ |
| 58 |
public function getConfigParameters(): array |
| 59 |
{ |
| 60 |
if ($this->configParameters === null) { |
| 61 |
$configParameters = $this->getDefaultConfigParameters(); |
| 62 |
$currentOptions = (array) $this->getWpOption($this->key); |
| 63 |
|
| 64 |
foreach ($currentOptions as $key => $option) { |
| 65 |
if (isset($configParameters[$key])) { |
| 66 |
$configParameters[$key]->setValue($option); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$this->configParameters = $configParameters; |
| 71 |
} |
| 72 |
|
| 73 |
return $this->configParameters; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param array $rawParameters |
| 78 |
*/ |
| 79 |
public function setConfigParameters(array $rawParameters): void |
| 80 |
{ |
| 81 |
$configParameters = $this->getConfigParameters(); |
| 82 |
|
| 83 |
foreach ($rawParameters as $key => $value) { |
| 84 |
if (isset($configParameters[$key]) === true) { |
| 85 |
$configParameters[$key]->setValue($value); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$this->configParameters = $configParameters; |
| 90 |
|
| 91 |
$simpleConfigParameters = []; |
| 92 |
|
| 93 |
foreach ($configParameters as $parameter) { |
| 94 |
$simpleConfigParameters[$parameter->getId()] = $parameter->getValue(); |
| 95 |
} |
| 96 |
|
| 97 |
$this->wordpress->updateOption($this->key, $simpleConfigParameters); |
| 98 |
} |
| 99 |
|
| 100 |
public function flushConfigParameters(): void |
| 101 |
{ |
| 102 |
$this->defaultConfigParameters = []; |
| 103 |
$this->configParameters = null; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @throws Exception |
| 108 |
*/ |
| 109 |
public function getParameterValueRaw(string $parameterName): mixed |
| 110 |
{ |
| 111 |
$options = $this->getConfigParameters(); |
| 112 |
|
| 113 |
if (isset($options[$parameterName]) === false) { |
| 114 |
throw new Exception("Unknown config parameter '$parameterName'."); |
| 115 |
} |
| 116 |
|
| 117 |
return $options[$parameterName]->getValue(); |
| 118 |
} |
| 119 |
|
| 120 |
public function getParameterValue(string $parameterName): mixed |
| 121 |
{ |
| 122 |
try { |
| 123 |
return $this->getParameterValueRaw($parameterName); |
| 124 |
} catch (Exception) { |
| 125 |
} |
| 126 |
|
| 127 |
return null; |
| 128 |
} |
| 129 |
} |
| 130 |
|