Refactor helper drift in lib: centralize theme/translation and remove app() hotspots

This commit is contained in:
2026-03-19 08:23:14 +01:00
parent 5da506a20f
commit 5739cc1200
24 changed files with 197 additions and 88 deletions

View File

@@ -724,12 +724,8 @@ class UserAccountService
private function normalizeTheme($value): string
{
$theme = strtolower(trim((string) $value));
$themes = function_exists('appThemes') ? appThemes() : ['light' => 'Light', 'dark' => 'Dark'];
if ($theme === '' || !isset($themes[$theme])) {
return function_exists('appDefaultTheme') ? appDefaultTheme() : 'light';
}
return $theme;
$theme = is_scalar($value) ? (string) $value : null;
return $this->settingsGateway->normalizeTheme($theme);
}
private function normalizeLocale($value): string

View File

@@ -14,7 +14,8 @@ class UserLifecycleRestoreService
private readonly UserReadRepositoryInterface $userReadRepository,
private readonly UserWriteRepositoryInterface $userWriteRepository,
private readonly UserLifecycleAuditService $userLifecycleAuditService,
private readonly DatabaseSessionRepository $databaseSessionRepository
private readonly DatabaseSessionRepository $databaseSessionRepository,
private readonly UserSettingsGateway $userSettingsGateway
) {
}
@@ -171,8 +172,7 @@ class UserLifecycleRestoreService
private function normalizeTheme(mixed $value): string
{
$theme = strtolower(trim((string) $value));
$themes = appThemes();
return isset($themes[$theme]) ? $theme : appDefaultTheme();
$theme = is_scalar($value) ? (string) $value : null;
return $this->userSettingsGateway->normalizeTheme($theme);
}
}

View File

@@ -2,8 +2,12 @@
namespace MintyPHP\Service\User;
use MintyPHP\Service\I18n\TranslatesServiceText;
class UserPasswordPolicyService
{
use TranslatesServiceText;
private const PASSWORD_MIN_LENGTH = 12;
public function validate(string $password, string $password2, bool $required, ?string $email): array
@@ -61,15 +65,4 @@ class UserPasswordPolicyService
return self::PASSWORD_MIN_LENGTH;
}
// Wrapper so this service can be used in tests before the t() helper is loaded.
private function translate(string $text, mixed ...$args): string
{
if (function_exists('t')) {
return t($text, ...$args);
}
if ($args === []) {
return $text;
}
return vsprintf($text, array_map(static fn ($arg) => (string) $arg, $args));
}
}

View File

@@ -153,7 +153,8 @@ class UserServicesFactory
$this->createUserReadRepository(),
$this->createUserWriteRepository(),
$this->createUserLifecycleAuditService(),
$this->databaseSessionRepository
$this->databaseSessionRepository,
$this->createUserSettingsGateway()
);
}

View File

@@ -35,6 +35,24 @@ class UserSettingsGateway
return $this->settingsAppGateway->getAppTheme();
}
/**
* @return array<string, string>
*/
public function appThemes(): array
{
return $this->settingsAppGateway->listThemes();
}
public function defaultTheme(): string
{
return $this->settingsAppGateway->resolveDefaultTheme();
}
public function normalizeTheme(?string $theme): string
{
return $this->settingsAppGateway->normalizeTheme($theme);
}
public function getUserInactivityDeactivateDays(): int
{
return $this->settingsUserLifecycleGateway->getUserInactivityDeactivateDays();