| @@ -4,13 +4,13 @@ | ||
| 4 | 4 | |
| 5 | 5 | namespace UserAccessManager\Config; |
| 6 | 6 | |
| 7 | 7 | use Exception; |
| 8 | +use UserAccessManager\Config\Parameter\ConfigParameter; | |
| 8 | 9 | use UserAccessManager\Wrapper\Wordpress; |
| 9 | 10 | |
| 10 | 11 | class Config |
| 11 | 12 | { |
| 12 | - protected string $key; | |
| 13 | 13 | protected array $wpOptions = []; |
| 14 | 14 | /** |
| 15 | 15 | * @var ConfigParameter[] |
| 16 | 16 | */ |
| @@ -21,16 +21,15 @@ | ||
| 21 | 21 | protected ?array $configParameters = null; |
| 22 | 22 | |
| 23 | 23 | public function __construct( |
| 24 | 24 | private Wordpress $wordpress, |
| 25 | - string $key | |
| 25 | + protected string $key | |
| 26 | 26 | ) { |
| 27 | - $this->key = $key; | |
| 28 | 27 | } |
| 29 | 28 | |
| 30 | 29 | public function getWpOption(string $option): mixed |
| 31 | 30 | { |
| 32 | - if (!isset($this->wpOptions[$option]) === true) { | |
| 31 | + if (isset($this->wpOptions[$option]) === false) { | |
| 33 | 32 | $this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 34 | 33 | } |
| 35 | 34 | |
| 36 | 35 | return $this->wpOptions[$option]; |
| @@ -56,27 +55,23 @@ | ||
| 56 | 55 | * @return ConfigParameter[] |
| 57 | 56 | */ |
| 58 | 57 | public function getConfigParameters(): array |
| 59 | 58 | { |
| 60 | - if ($this->configParameters === null) { | |
| 61 | - $configParameters = $this->getDefaultConfigParameters(); | |
| 62 | - $currentOptions = (array) $this->getWpOption($this->key); | |
| 59 | + if ($this->configParameters !== null) { | |
| 60 | + return $this->configParameters; | |
| 61 | + } | |
| 63 | 62 | |
| 64 | - foreach ($currentOptions as $key => $option) { | |
| 65 | - if (isset($configParameters[$key])) { | |
| 66 | - $configParameters[$key]->setValue($option); | |
| 67 | - } | |
| 63 | + $configParameters = $this->getDefaultConfigParameters(); | |
| 64 | + | |
| 65 | + foreach ((array) $this->getWpOption($this->key) as $key => $option) { | |
| 66 | + if (isset($configParameters[$key]) === true) { | |
| 67 | + $configParameters[$key]->setValue($option); | |
| 68 | 68 | } |
| 69 | - | |
| 70 | - $this->configParameters = $configParameters; | |
| 71 | 69 | } |
| 72 | 70 | |
| 73 | - return $this->configParameters; | |
| 71 | + return $this->configParameters = $configParameters; | |
| 74 | 72 | } |
| 75 | 73 | |
| 76 | - /** | |
| 77 | - * @param array $rawParameters | |
| 78 | - */ | |
| 79 | 74 | public function setConfigParameters(array $rawParameters): void |
| 80 | 75 | { |
| 81 | 76 | $configParameters = $this->getConfigParameters(); |
| 82 | 77 | |
| @@ -85,17 +80,15 @@ | ||
| 85 | 80 | $configParameters[$key]->setValue($value); |
| 86 | 81 | } |
| 87 | 82 | } |
| 88 | 83 | |
| 89 | - $this->configParameters = $configParameters; | |
| 84 | + $persistableValues = []; | |
| 90 | 85 | |
| 91 | - $simpleConfigParameters = []; | |
| 92 | - | |
| 93 | 86 | foreach ($configParameters as $parameter) { |
| 94 | - $simpleConfigParameters[$parameter->getId()] = $parameter->getValue(); | |
| 87 | + $persistableValues[$parameter->getId()] = $parameter->getValue(); | |
| 95 | 88 | } |
| 96 | 89 | |
| 97 | - $this->wordpress->updateOption($this->key, $simpleConfigParameters); | |
| 90 | + $this->wordpress->updateOption($this->key, $persistableValues); | |
| 98 | 91 | } |
| 99 | 92 | |
| 100 | 93 | public function flushConfigParameters(): void |
| 101 | 94 | { |
| @@ -121,9 +114,8 @@ | ||
| 121 | 114 | { |
| 122 | 115 | try { |
| 123 | 116 | return $this->getParameterValueRaw($parameterName); |
| 124 | 117 | } catch (Exception) { |
| 118 | + return null; | |
| 125 | 119 | } |
| 126 | - | |
| 127 | - return null; | |
| 128 | 120 | } |
| 129 | 121 | } |