1
0
Files
breadcrumb-the-shire/core/Service/Settings/SettingsAppGateway.php
fs 149d4515de refactor(settings): remove global appearance settings — tenant is sole source
Removes the global app_theme, app_theme_user and app_primary_color settings
from the admin/settings area and the underlying service/cache/API/i18n/docs
layers. The tenant columns (tenants.primary_color, default_theme,
allow_user_theme) become the single source of truth for appearance.
Branding helpers are tenant-only and fall back to a hardcoded 'light' theme
with no brand color when no tenant context is available (login, error,
pre-session pages). The APP_THEME env var is removed.

Scope:
- UI: Appearance tab gone from /admin/settings; tenant edit form drops the
  global-fallback color preview.
- Service: SettingsAppGateway no longer exposes setting-backed accessors
  for theme/user-theme/primary-color. Theme catalog utilities (listThemes,
  isAllowedTheme, normalizeTheme, resolveDefaultTheme) stay and are used
  across Tenant / Auth / User flows; resolveDefaultTheme now hardcodes
  'light' with a catalog fallback (no global/env lookup).
- AdminSettingsService: buildPageData, sanitization, audit snapshot, apply
  step and cache payload are all stripped of the three keys. Dead helper
  applyAppPrimaryColor + normalizePrimaryColor removed.
- Cache: SettingCacheService HOT_PATH_KEYS drops the three keys.
- API: /settings (GET/PUT) and /settings/public (GET) no longer expose
  appearance fields; OpenAPI schemas pruned accordingly.
- Audit redaction list shortened.
- DB: db/init/init.sql seed rows removed. No migration for existing
  installs — legacy rows stay inert.
- i18n + docs: setting.app_theme, setting.app_theme_user,
  setting.app_primary_color removed from de+en locales; reference and
  explanation docs updated.
- Tests: covering tests for the removed methods pruned; ThemeResolutionTest
  rewritten for tenant-only semantics. One unrelated PHP-CS-Fixer issue in
  UserRegistrar.php cleaned up to keep QG-006 green.

Workflow: .agents/runs/SETTINGS-APPEARANCE-TENANT-ONLY/ (gitignored).
Gates: QG-001..003, QG-006, QG-008 all pass (1985 tests, 28764 assertions).
Reviews: code, security, acceptance all pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 22:40:15 +02:00

145 lines
4.5 KiB
PHP

<?php
namespace MintyPHP\Service\Settings;
use MintyPHP\I18n;
/**
* App-wide (global) settings gateway.
*
* Appearance SETTINGS (theme, user-theme toggle, primary color) are NOT
* managed globally — they live on the tenant (tenants.primary_color,
* default_theme, allow_user_theme) and are resolved by branding helpers.
*
* The theme CATALOG utilities (listThemes, isAllowedTheme, normalizeTheme,
* resolveDefaultTheme) stay because consumers across Tenant / Auth / User
* flows need to validate a theme value and resolve a safe default. There is
* no global setting lookup in these helpers anymore — the default is a
* hardcoded 'light' falling back to the first available catalog entry.
*/
class SettingsAppGateway
{
public function __construct(
private readonly SettingsMetadataGateway $settingsMetadataGateway,
private readonly ThemeConfigGateway $themeConfigService
) {
}
public function getAppTitle(): ?string
{
$value = $this->settingsMetadataGateway->getValue(SettingKeys::APP_TITLE_KEY);
$value = $value !== null ? trim($value) : null;
return $value !== '' ? $value : null;
}
public function setAppTitle(?string $title, ?string $description = null): bool
{
$value = $title !== null ? trim($title) : null;
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.app_title';
return $this->settingsMetadataGateway->set(SettingKeys::APP_TITLE_KEY, $value, $desc);
}
public function getAppLocale(): ?string
{
$value = $this->settingsMetadataGateway->getValue(SettingKeys::APP_LOCALE_KEY);
$value = $value !== null ? trim($value) : '';
return $value !== '' ? $value : null;
}
public function setAppLocale(?string $locale, ?string $description = null): bool
{
$value = $locale !== null ? trim($locale) : '';
if ($value === '') {
$value = null;
} else {
$allowed = defined('APP_LOCALES') ? APP_LOCALES : [];
if ($allowed && !in_array($value, $allowed, true)) {
return false;
}
}
$desc = $description ?? 'setting.app_locale';
return $this->settingsMetadataGateway->set(SettingKeys::APP_LOCALE_KEY, $value, $desc);
}
public function isRegistrationEnabled(): bool
{
return settingToBool(
$this->settingsMetadataGateway->getValue(SettingKeys::APP_REGISTRATION_KEY),
true
);
}
public function setRegistrationEnabled(bool $allowed, ?string $description = null): bool
{
$value = $allowed ? '1' : '0';
$desc = $description ?? 'setting.app_registration';
return $this->settingsMetadataGateway->set(SettingKeys::APP_REGISTRATION_KEY, $value, $desc);
}
/**
* Catalog of available themes — purely code-level (config/themes.php),
* not a global setting.
*
* @return array<string, string>
*/
public function listThemes(): array
{
return $this->themeConfigService->all();
}
public function isAllowedTheme(?string $theme): bool
{
$value = strtolower(trim((string) $theme));
return $value !== '' && in_array($value, $this->allowedThemes(), true);
}
/**
* Default theme for flows that need ONE value without a tenant context
* (user creation defaults, SSO-linked account provisioning, etc.).
* Hardcoded to 'light' with a catalog fallback — no global setting.
*/
public function resolveDefaultTheme(): string
{
$allowed = $this->allowedThemes();
if (in_array('light', $allowed, true)) {
return 'light';
}
return (string) ($allowed[0] ?? 'light');
}
public function normalizeTheme(?string $theme): string
{
$value = strtolower(trim((string) $theme));
if ($this->isAllowedTheme($value)) {
return $value;
}
return $this->resolveDefaultTheme();
}
public function normalizeLocale(?string $locale): string
{
$value = strtolower(trim((string) $locale));
$available = defined('APP_LOCALES') ? APP_LOCALES : [I18n::$defaultLocale];
if ($value === '' || !in_array($value, $available, true)) {
return I18n::$locale ?? I18n::$defaultLocale;
}
return $value;
}
/**
* @return list<string>
*/
private function allowedThemes(): array
{
return array_keys($this->listThemes());
}
}