Passed
Push — bind-etag ( 38dc63 )
by Akihito
02:25
created

StorageRedisMemcachedModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\EtagPool;
8
use Memcached;
9
use Psr\Cache\CacheItemPoolInterface;
10
use Ray\Di\AbstractModule;
11
use Ray\PsrCacheModule\Annotation\CacheNamespace;
12
use Ray\PsrCacheModule\Annotation\MemcacheConfig;
13
use Ray\PsrCacheModule\MemcachedProvider;
14
use Ray\PsrCacheModule\Psr6RedisModule;
15
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
16
17
use function array_map;
18
use function explode;
19
20
/**
21
 * Bind redis to shared storage, Bind memcache to etag pool
22
 */
23
final class StorageRedisMemcachedModule extends AbstractModule
24
{
25
    /** @var string */
26
    private $redisServer;
27
28
    /** @var array */
29
    private $memcacheServer;
30
31
    /**
32
     * @param string $redisServer 'localhost:6379' {host}:{port}
33
     */
34
    public function __construct(string $redisServer, string $memcacheServer, ?AbstractModule $module = null)
35
    {
36
        $this->redisServer = $redisServer;
37
        $this->memcacheServer = array_map(static function ($memcacheServer) {
38
            return explode(':', $memcacheServer);
39
        }, explode(',', $memcacheServer));
40
        parent::__construct($module);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function configure(): void
47
    {
48
        $this->install(new Psr6RedisModule($this->redisServer));
49
        $this->bind(CacheItemPoolInterface::class)->annotatedWith(EtagPool::class)->toConstructor(MemcachedAdapter::class, [
50
            'namespace' => CacheNamespace::class,
51
        ]);
52
        $this->bind()->annotatedWith(MemcacheConfig::class)->toInstance($this->memcacheServer);
53
        $this->bind(Memcached::class)->toProvider(MemcachedProvider::class);
54
    }
55
}
56