Passed
Push — master ( dbf047...1b561f )
by Daniel
02:26
created

CacheSymfonyServiceProvider::createRedisAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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