Lines
50.00%
11 / 22
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| create | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| createFilesystemAdapter | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| createRedisAdapter | 0.00% | 0 / 11 | 0.00% | 0 / 1 | 6 | ||
| 29 | readonly class CacheFactory | |
| 30 | { | |
| 31 | public function __construct( | |
| 32 | private Configuration $configuration, | |
| 33 | private string $cacheDir, | |
| 34 | ) { | |
| 35 | } | |
| 36 | ||
| 37 | public function create(): CacheInterface | |
| 38 | { | |
| 39 | $resolver = new CacheSettingsResolver(new DatabaseConfigurationStore($this->configuration->getDb())); | |
| 40 | $settings = $resolver->resolve(); | |
| 41 | ||
| 42 | return match ($settings->adapter) { | |
| 43 | 'redis' => $this->createRedisAdapter($settings), | |
| 44 | default => $this->createFilesystemAdapter($settings), | |
| 45 | }; | |
| 46 | } | |
| 47 | ||
| 48 | private function createFilesystemAdapter(CacheSettings $settings): FilesystemAdapter | |
| 49 | { | |
| 50 | return new FilesystemAdapter( | |
| 51 | namespace: 'pmf', | |
| 52 | defaultLifetime: $settings->defaultTtl, | |
| 53 | directory: $this->cacheDir, | |
| 54 | ); | |
| 55 | } | |
| 56 | ||
| 57 | private function createRedisAdapter(CacheSettings $settings): RedisAdapter | |
| 58 | { | |
| 59 | if (!extension_loaded('redis')) { | |
| 60 | throw new RuntimeException( | |
| 61 | 'The "redis" PHP extension is required when using the Redis cache adapter. ' | |
| 62 | . 'Please install ext-redis or change storage.cacheAdapter to "filesystem".', | |
| 63 | ); | |
| 64 | } | |
| 65 | ||
| 66 | $connection = RedisAdapter::createConnection($settings->redisDsn, ['timeout' => $settings->connectTimeout]); | |
| 67 | ||
| 68 | return new RedisAdapter( | |
| 69 | redis: $connection, | |
| 70 | namespace: $settings->redisPrefix, | |
| 71 | defaultLifetime: $settings->defaultTtl, | |
| 72 | ); | |
| 73 | } | |
| 74 | } |