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

CacheProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 34
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRedisCachePool() 0 24 3
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