Implements Spec 098: workspace-level settings slices for Backup retention, Drift severity mapping, and Operations retention/threshold. Spec - specs/098-settings-slices-v1-backup-drift-ops/spec.md What changed - Workspace Settings page: grouped Backup/Drift/Operations sections, unset-input UX w/ helper text, per-setting reset actions (confirmed) - Settings registry: adds/updates validation + normalization (incl. drift severity mapping normalization to lowercase) - Backup retention: adds workspace default + floor clamp; job clamps effective keep-last up to floor - Drift findings: optional workspace severity mapping; adds `critical` severity support + badge mapping - Operations pruning: retention computed per workspace via settings; scheduler unchanged; stuck threshold is storage-only Safety / Compliance notes - Filament v5 / Livewire v4: no Livewire v3 usage; relies on existing Filament v5 + Livewire v4 stack - Provider registration unchanged (Laravel 11+/12 uses bootstrap/providers.php) - Destructive actions: per-setting reset uses Filament actions with confirmation - Global search: not affected (no resource changes) - Assets: no new assets registered; no `filament:assets` changes Tests - vendor/bin/sail artisan test --compact tests/Feature/SettingsFoundation/WorkspaceSettingsManageTest.php \ tests/Feature/SettingsFoundation/WorkspaceSettingsViewOnlyTest.php \ tests/Feature/BackupScheduling/BackupScheduleLifecycleTest.php \ tests/Feature/Drift/DriftPolicySnapshotDriftDetectionTest.php \ tests/Feature/Scheduling/PruneOldOperationRunsScheduleTest.php \ tests/Unit/Badges/FindingBadgesTest.php Formatting - vendor/bin/sail bin pint --dirty Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #120
173 lines
5.0 KiB
PHP
173 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support\Settings;
|
|
|
|
use App\Models\Finding;
|
|
|
|
final class SettingsRegistry
|
|
{
|
|
/**
|
|
* @var array<string, SettingDefinition>
|
|
*/
|
|
private array $definitions;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->definitions = [];
|
|
|
|
$this->register(new SettingDefinition(
|
|
domain: 'backup',
|
|
key: 'retention_keep_last_default',
|
|
type: 'int',
|
|
systemDefault: 30,
|
|
rules: ['required', 'integer', 'min:1', 'max:365'],
|
|
normalizer: static fn (mixed $value): int => (int) $value,
|
|
));
|
|
|
|
$this->register(new SettingDefinition(
|
|
domain: 'backup',
|
|
key: 'retention_min_floor',
|
|
type: 'int',
|
|
systemDefault: 1,
|
|
rules: ['required', 'integer', 'min:1', 'max:365'],
|
|
normalizer: static fn (mixed $value): int => (int) $value,
|
|
));
|
|
|
|
$this->register(new SettingDefinition(
|
|
domain: 'drift',
|
|
key: 'severity_mapping',
|
|
type: 'json',
|
|
systemDefault: [],
|
|
rules: [
|
|
'required',
|
|
'array',
|
|
static function (string $attribute, mixed $value, \Closure $fail): void {
|
|
if (! is_array($value)) {
|
|
$fail('The severity mapping must be a JSON object.');
|
|
|
|
return;
|
|
}
|
|
|
|
foreach ($value as $findingType => $severity) {
|
|
if (! is_string($findingType) || trim($findingType) === '') {
|
|
$fail('Each severity mapping key must be a non-empty string.');
|
|
|
|
return;
|
|
}
|
|
|
|
if (! is_string($severity)) {
|
|
$fail(sprintf('Severity for "%s" must be a string.', $findingType));
|
|
|
|
return;
|
|
}
|
|
|
|
$normalizedSeverity = strtolower($severity);
|
|
|
|
if (! in_array($normalizedSeverity, self::supportedFindingSeverities(), true)) {
|
|
$fail(sprintf(
|
|
'Severity for "%s" must be one of: %s.',
|
|
$findingType,
|
|
implode(', ', self::supportedFindingSeverities()),
|
|
));
|
|
|
|
return;
|
|
}
|
|
}
|
|
},
|
|
],
|
|
normalizer: static fn (mixed $value): array => self::normalizeSeverityMapping($value),
|
|
));
|
|
|
|
$this->register(new SettingDefinition(
|
|
domain: 'operations',
|
|
key: 'operation_run_retention_days',
|
|
type: 'int',
|
|
systemDefault: 90,
|
|
rules: ['required', 'integer', 'min:7', 'max:3650'],
|
|
normalizer: static fn (mixed $value): int => (int) $value,
|
|
));
|
|
|
|
$this->register(new SettingDefinition(
|
|
domain: 'operations',
|
|
key: 'stuck_run_threshold_minutes',
|
|
type: 'int',
|
|
systemDefault: 0,
|
|
rules: ['required', 'integer', 'min:0', 'max:10080'],
|
|
normalizer: static fn (mixed $value): int => (int) $value,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* @return array<string, SettingDefinition>
|
|
*/
|
|
public function all(): array
|
|
{
|
|
return $this->definitions;
|
|
}
|
|
|
|
public function find(string $domain, string $key): ?SettingDefinition
|
|
{
|
|
return $this->definitions[$this->cacheKey($domain, $key)] ?? null;
|
|
}
|
|
|
|
public function require(string $domain, string $key): SettingDefinition
|
|
{
|
|
$definition = $this->find($domain, $key);
|
|
|
|
if ($definition instanceof SettingDefinition) {
|
|
return $definition;
|
|
}
|
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown setting key: %s.%s', $domain, $key));
|
|
}
|
|
|
|
private function register(SettingDefinition $definition): void
|
|
{
|
|
$this->definitions[$this->cacheKey($definition->domain, $definition->key)] = $definition;
|
|
}
|
|
|
|
private function cacheKey(string $domain, string $key): string
|
|
{
|
|
return $domain.'.'.$key;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
private static function supportedFindingSeverities(): array
|
|
{
|
|
return [
|
|
Finding::SEVERITY_LOW,
|
|
Finding::SEVERITY_MEDIUM,
|
|
Finding::SEVERITY_HIGH,
|
|
Finding::SEVERITY_CRITICAL,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
private static function normalizeSeverityMapping(mixed $value): array
|
|
{
|
|
if (! is_array($value)) {
|
|
return [];
|
|
}
|
|
|
|
$normalized = [];
|
|
|
|
foreach ($value as $findingType => $severity) {
|
|
if (! is_string($findingType) || trim($findingType) === '' || ! is_string($severity)) {
|
|
continue;
|
|
}
|
|
|
|
$normalized[$findingType] = strtolower($severity);
|
|
}
|
|
|
|
ksort($normalized);
|
|
|
|
return $normalized;
|
|
}
|
|
}
|