Files
breadcrumb-the-shire/lib/Service/Settings/SettingsLoginPersistenceGateway.php

63 lines
2.1 KiB
PHP

<?php
namespace MintyPHP\Service\Settings;
class SettingsLoginPersistenceGateway
{
private const AUTO_REMEMBER_FALLBACK = false;
private const LIFETIME_DAYS_FALLBACK = 30;
private const LIFETIME_DAYS_MIN = 1;
private const LIFETIME_DAYS_MAX = 365;
public function __construct(private readonly SettingsMetadataGateway $settingsMetadataGateway)
{
}
public function isMicrosoftAutoRememberDefault(): bool
{
$value = strtolower(trim((string) ($this->settingsMetadataGateway->getValue(SettingKeys::MICROSOFT_AUTO_REMEMBER_DEFAULT_KEY) ?? '')));
if ($value === '') {
return self::AUTO_REMEMBER_FALLBACK;
}
return in_array($value, ['1', 'true', 'yes', 'on'], true);
}
public function setMicrosoftAutoRememberDefault(bool $enabled, ?string $description = null): bool
{
$desc = $description ?? 'setting.microsoft_auto_remember_default';
return $this->settingsMetadataGateway->set(SettingKeys::MICROSOFT_AUTO_REMEMBER_DEFAULT_KEY, $enabled ? '1' : '0', $desc);
}
public function getRememberTokenLifetimeDays(): int
{
$value = $this->settingsMetadataGateway->getInt(SettingKeys::REMEMBER_TOKEN_LIFETIME_DAYS_KEY);
if ($value !== null && $this->isValidLifetime($value)) {
return $value;
}
return self::LIFETIME_DAYS_FALLBACK;
}
public function setRememberTokenLifetimeDays(?int $days, ?string $description = null): bool
{
$value = $days ?? self::LIFETIME_DAYS_FALLBACK;
if (!$this->isValidLifetime($value)) {
return false;
}
$desc = $description ?? 'setting.remember_token_lifetime_days';
return $this->settingsMetadataGateway->set(SettingKeys::REMEMBER_TOKEN_LIFETIME_DAYS_KEY, (string) $value, $desc);
}
public function getRememberTokenLifetimeSeconds(): int
{
return $this->getRememberTokenLifetimeDays() * 86400;
}
private function isValidLifetime(int $days): bool
{
return $days >= self::LIFETIME_DAYS_MIN && $days <= self::LIFETIME_DAYS_MAX;
}
}