Passed
Pull Request — master (#1699)
by Nico
43:19 queued 18:45
created

CacheProvider::getRedisCachePool()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 0
dl 0
loc 24
ccs 0
cts 16
cp 0
crap 12
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace Stu\Component\Cache;
4
5
use Cache\Adapter\Redis\RedisCachePool;
6
use Exception;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Redis;
9
use Stu\Module\Config\StuConfigInterface;
10
11
final class CacheProvider implements CacheProviderInterface
12
{
13
    private StuConfigInterface $config;
14
15
    public function __construct(
16
        StuConfigInterface $config,
17
    ) {
18
        $this->config = $config;
19
    }
20
21
    public function getRedisCachePool(): CacheItemPoolInterface
22
    {
23
        $redis = new Redis();
24
25
        $cacheSettings = $this->config->getCacheSettings();
26
27
        if ($cacheSettings->getRedisSocket() !== null) {
28
            try {
29
                $redis->connect($cacheSettings->getRedisSocket());
30
            } catch (Exception $e) {
31
                $redis->connect(
32
                    $cacheSettings->getRedisHost(),
33
                    $cacheSettings->getRedisPort()
34
                );
35
            }
36
        } else {
37
            $redis->connect(
38
                $cacheSettings->getRedisHost(),
39
                $cacheSettings->getRedisPort()
40
            );
41
        }
42
        $redis->setOption(Redis::OPT_PREFIX, $this->config->getDbSettings()->getDatabase());
43
44
        return new RedisCachePool($redis);
45
    }
46
}
47