Skip to content

Commit a6378aa

Browse files
committed
refactor(policy): remove runtime typed fallback branches
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent ee7d75f commit a6378aa

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

lib/Service/Policy/Runtime/PolicySource.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public function __construct(
3131
public function loadSystemPolicy(string $policyKey): ?PolicyLayer {
3232
$definition = $this->registry->get($policyKey);
3333
$defaultValue = $definition->normalizeValue($definition->defaultSystemValue());
34-
$storedValue = $this->appConfig->getAppValueString($definition->getAppConfigKey(), '');
35-
$hasExplicitSystemValue = $storedValue !== '';
34+
$hasExplicitSystemValue = $this->appConfig->hasAppKey($definition->getAppConfigKey());
35+
$storedValue = $hasExplicitSystemValue
36+
? $this->readSystemValue($definition->getAppConfigKey(), $defaultValue)
37+
: null;
3638
$value = $hasExplicitSystemValue
3739
? $definition->normalizeValue($storedValue)
3840
: $defaultValue;
@@ -184,7 +186,7 @@ public function saveSystemPolicy(string $policyKey, mixed $value, bool $allowChi
184186

185187
if ($normalizedValue === $defaultValue) {
186188
if ($allowChildOverride) {
187-
$this->appConfig->setAppValueString($definition->getAppConfigKey(), (string)$normalizedValue);
189+
$this->writeSystemValue($definition->getAppConfigKey(), $normalizedValue);
188190
$this->appConfig->setAppValueString($allowOverrideConfigKey, '1');
189191
return;
190192
}
@@ -194,10 +196,54 @@ public function saveSystemPolicy(string $policyKey, mixed $value, bool $allowChi
194196
return;
195197
}
196198

197-
$this->appConfig->setAppValueString($definition->getAppConfigKey(), (string)$normalizedValue);
199+
$this->writeSystemValue($definition->getAppConfigKey(), $normalizedValue);
198200
$this->appConfig->setAppValueString($allowOverrideConfigKey, $allowChildOverride ? '1' : '0');
199201
}
200202

203+
private function readSystemValue(string $key, mixed $defaultValue): mixed {
204+
if (is_int($defaultValue)) {
205+
return $this->appConfig->getAppValueInt($key, $defaultValue);
206+
}
207+
208+
if (is_bool($defaultValue)) {
209+
return $this->appConfig->getAppValueBool($key, $defaultValue);
210+
}
211+
212+
if (is_float($defaultValue)) {
213+
return $this->appConfig->getAppValueFloat($key, $defaultValue);
214+
}
215+
216+
if (is_array($defaultValue)) {
217+
return $this->appConfig->getAppValueArray($key, $defaultValue);
218+
}
219+
220+
return $this->appConfig->getAppValueString($key, (string)$defaultValue);
221+
}
222+
223+
private function writeSystemValue(string $key, mixed $value): void {
224+
if (is_int($value)) {
225+
$this->appConfig->setAppValueInt($key, $value);
226+
return;
227+
}
228+
229+
if (is_bool($value)) {
230+
$this->appConfig->setAppValueBool($key, $value);
231+
return;
232+
}
233+
234+
if (is_float($value)) {
235+
$this->appConfig->setAppValueFloat($key, $value);
236+
return;
237+
}
238+
239+
if (is_array($value)) {
240+
$this->appConfig->setAppValueArray($key, $value);
241+
return;
242+
}
243+
244+
$this->appConfig->setAppValueString($key, (string)$value);
245+
}
246+
201247
private function getSystemAllowOverrideConfigKey(string $policyConfigKey): string {
202248
return $policyConfigKey . '.allow_child_override';
203249
}

0 commit comments

Comments
 (0)