LockSymfonyServiceProvider::createRedisClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 15
rs 9.8666
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\LockSymfony;
6
7
use Jellyfish\Lock\LockIdentifierGeneratorInterface;
8
use Pimple\Container;
9
use Pimple\ServiceProviderInterface;
10
use Predis\Client;
11
use Symfony\Component\Lock\Factory as SymfonyLockFactory;
12
use Symfony\Component\Lock\Store\RedisStore;
13
use Symfony\Component\Lock\StoreInterface;
14
15
class LockSymfonyServiceProvider implements ServiceProviderInterface
16
{
17
    /**
18
     * @param \Pimple\Container $pimple
19
     *
20
     * @return void
21
     */
22
    public function register(Container $pimple): void
23
    {
24
        $this->registerLockFactory($pimple);
25
    }
26
27
    /**
28
     * @param \Pimple\Container $container
29
     *
30
     * @return \Jellyfish\LockSymfony\LockSymfonyServiceProvider
31
     */
32
    protected function registerLockFactory(Container $container): LockSymfonyServiceProvider
33
    {
34
        $self = $this;
35
36
        $container->offsetSet('lock_factory', function (Container $container) use ($self) {
37
            return new LockFactory(
38
                $self->createSymfonyLockFactory($container),
39
                $self->createLockIdentifierGenerator()
40
            );
41
        });
42
43
        return $this;
44
    }
45
46
    /**
47
     * @param \Pimple\Container $container
48
     *
49
     * @return \Symfony\Component\Lock\Factory
50
     */
51
    protected function createSymfonyLockFactory(Container $container): SymfonyLockFactory
52
    {
53
        $redisClient = $this->createRedisClient($container);
54
        $redisStore = $this->createRedisStore($redisClient);
55
56
        return new SymfonyLockFactory($redisStore);
57
    }
58
59
    /**
60
     * @param \Predis\Client $redisClient
61
     *
62
     * @return \Symfony\Component\Lock\StoreInterface
63
     */
64
    protected function createRedisStore(Client $redisClient): StoreInterface
65
    {
66
        return new RedisStore($redisClient);
67
    }
68
69
    /**
70
     * @param \Pimple\Container $container
71
     *
72
     * @return \Predis\Client
73
     */
74
    protected function createRedisClient(Container $container): Client
75
    {
76
        $config = $container->offsetGet('config');
77
78
        return new Client([
79
            'scheme' => 'tcp',
80
            'host' => $config->get(
81
                LockSymfonyConstants::REDIS_STORE_HOST,
82
                LockSymfonyConstants::DEFAULT_REDIS_STORE_HOST
83
            ), 'port' => $config->get(
84
                LockSymfonyConstants::REDIS_STORE_PORT,
85
                LockSymfonyConstants::DEFAULT_REDIS_STORE_PORT
86
            ), 'database' => $config->get(
87
                LockSymfonyConstants::REDIS_STORE_DB,
88
                LockSymfonyConstants::DEFAULT_REDIS_STORE_DB
89
            ),
90
        ]);
91
    }
92
93
    /**
94
     * @return \Jellyfish\Lock\LockIdentifierGeneratorInterface
95
     */
96
    protected function createLockIdentifierGenerator(): LockIdentifierGeneratorInterface
97
    {
98
        return new LockIdentifierGenerator();
99
    }
100
}
101