Passed
Pull Request — master (#39)
by Daniel
03:37
created

CacheSymfonyFactory::createRedisClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Jellyfish\CacheSymfony;
4
5
use Jellyfish\Config\ConfigFacadeInterface;
6
use Predis\Client;
7
use Predis\ClientInterface;
8
use Symfony\Component\Cache\Adapter\AbstractAdapter;
9
use Symfony\Component\Cache\Adapter\RedisAdapter;
10
11
class CacheSymfonyFactory
12
{
13
    /**
14
     * @var \Jellyfish\Config\ConfigFacadeInterface
15
     */
16
    protected $configFacade;
17
18
    /**
19
     * @var \Jellyfish\CacheSymfony\CacheInterface
20
     */
21
    protected $cache;
22
23
    /**
24
     * @param \Jellyfish\Config\ConfigFacadeInterface $configFacade
25
     */
26
    public function __construct(ConfigFacadeInterface $configFacade)
27
    {
28
        $this->configFacade = $configFacade;
29
    }
30
31
    /**
32
     * @return \Jellyfish\CacheSymfony\CacheInterface
33
     */
34
    public function getCache(): CacheInterface
35
    {
36
        if ($this->cache === null) {
37
            $this->cache = new Cache(
38
                $this->createRedisAdapter()
39
            );
40
        }
41
42
        return $this->cache;
43
    }
44
45
    /**
46
     * @return \Symfony\Component\Cache\Adapter\AbstractAdapter
47
     */
48
    protected function createRedisAdapter(): AbstractAdapter
49
    {
50
        return new RedisAdapter($this->createRedisClient());
51
    }
52
53
    /**
54
     * @return \Predis\ClientInterface
55
     */
56
    protected function createRedisClient(): ClientInterface
57
    {
58
        return new Client(
59
            [
60
                'scheme' => 'tcp',
61
                'host' => $this->configFacade->get(
62
                    CacheSymfonyConstants::REDIS_STORE_HOST,
63
                    CacheSymfonyConstants::DEFAULT_REDIS_STORE_HOST
64
                ),
65
                'port' => $this->configFacade->get(
66
                    CacheSymfonyConstants::REDIS_STORE_PORT,
67
                    CacheSymfonyConstants::DEFAULT_REDIS_STORE_PORT
68
                ),
69
                'database' => $this->configFacade->get(
70
                    CacheSymfonyConstants::REDIS_STORE_DB,
71
                    CacheSymfonyConstants::DEFAULT_REDIS_STORE_DB
72
                ),
73
            ]
74
        );
75
    }
76
}
77