Lines
100.00%
27 / 27
Methods
100.00%
3 / 3
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| resolve | 100.00% | 24 / 24 | 100.00% | 1 / 1 | 6 | ||
| normalizeRedisPrefix | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | ||
| 24 | readonly class CacheSettingsResolver | |
| 25 | { | |
| 26 | private const string DEFAULT_ADAPTER = 'filesystem'; | |
| 27 | private const string DEFAULT_REDIS_DSN = 'redis://redis:6379/2'; | |
| 28 | private const string DEFAULT_REDIS_PREFIX = 'pmf_cache_'; | |
| 29 | private const float DEFAULT_CONNECT_TIMEOUT = 1.0; | |
| 30 | private const int DEFAULT_TTL = 3600; | |
| 31 | ||
| 32 | public function __construct( | |
| 33 | private DatabaseConfigurationStore $databaseConfigurationStore, | |
| 34 | ) { | |
| 35 | } | |
| 36 | ||
| 37 | public function resolve(): CacheSettings | |
| 38 | { | |
| 39 | $values = $this->databaseConfigurationStore->fetchValues([ | |
| 40 | 'storage.cacheAdapter', | |
| 41 | 'storage.cacheRedisDsn', | |
| 42 | 'storage.cacheRedisPrefix', | |
| 43 | 'storage.cacheRedisConnectTimeout', | |
| 44 | 'storage.cacheDefaultTtl', | |
| 45 | ]); | |
| 46 | ||
| 47 | $adapter = trim($values['storage.cacheAdapter'] ?? ''); | |
| 48 | if (!in_array($adapter, ['filesystem', 'redis'], strict: true)) { | |
| 49 | $adapter = self::DEFAULT_ADAPTER; | |
| 50 | } | |
| 51 | ||
| 52 | $redisDsn = trim($values['storage.cacheRedisDsn'] ?? ''); | |
| 53 | if ($redisDsn === '') { | |
| 54 | $redisDsn = self::DEFAULT_REDIS_DSN; | |
| 55 | } | |
| 56 | ||
| 57 | $redisPrefix = trim($values['storage.cacheRedisPrefix'] ?? ''); | |
| 58 | if ($redisPrefix === '') { | |
| 59 | $redisPrefix = self::DEFAULT_REDIS_PREFIX; | |
| 60 | } | |
| 61 | $redisPrefix = $this->normalizeRedisPrefix($redisPrefix); | |
| 62 | ||
| 63 | $connectTimeout = (float) ($values['storage.cacheRedisConnectTimeout'] ?? ''); | |
| 64 | if ($connectTimeout <= 0) { | |
| 65 | $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT; | |
| 66 | } | |
| 67 | ||
| 68 | $defaultTtl = (int) ($values['storage.cacheDefaultTtl'] ?? ''); | |
| 69 | if ($defaultTtl <= 0) { | |
| 70 | $defaultTtl = self::DEFAULT_TTL; | |
| 71 | } | |
| 72 | ||
| 73 | return new CacheSettings($adapter, $redisDsn, $redisPrefix, $connectTimeout, $defaultTtl); | |
| 74 | } | |
| 75 | ||
| 76 | private function normalizeRedisPrefix(string $redisPrefix): string | |
| 77 | { | |
| 78 | $normalizedPrefix = preg_replace('/[^-+_.A-Za-z0-9]/', replacement: '_', subject: $redisPrefix) ?? ''; | |
| 79 | ||
| 80 | return trim($normalizedPrefix, characters: '_') !== '' ? $normalizedPrefix : self::DEFAULT_REDIS_PREFIX; | |
| 81 | } | |
| 82 | } |