Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CacheSettingsResolver
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resolve
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
6
 normalizeRedisPrefix
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Resolves cache settings from database configuration values.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-03-16
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Cache;
21
22use phpMyFAQ\Configuration\Storage\DatabaseConfigurationStore;
23
24readonly 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}