Passed
Pull Request — 1.x (#144)
by Akihito
02:40
created

StorageRedisDsnModule::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\RedisDsn;
8
use BEAR\RepositoryModule\Annotation\RedisDsnOptions;
9
use BEAR\RepositoryModule\Annotation\ResourceObjectPool;
10
use Psr\Cache\CacheItemPoolInterface;
11
use Ray\Di\AbstractModule;
12
use Ray\Di\ProviderInterface;
13
use Ray\Di\Scope;
14
use Ray\PsrCacheModule\Annotation\CacheNamespace;
15
use Ray\PsrCacheModule\Annotation\Local;
16
use Ray\PsrCacheModule\Annotation\Shared;
17
use Ray\PsrCacheModule\ApcuAdapter;
18
use Ray\PsrCacheModule\RedisAdapter;
19
use ReflectionException;
20
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
21
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
22
23
/**
24
 * Provides ResourceStorageInterface and derived bindings
25
 * *
26
 * * The following bindings are provided:
27
 * *
28
 * * CacheItemPoolInterface-EtagPool::class
29
 * *
30
 * * The following module are installed:
31
 * *
32
 * * Psr6RedisModule
33
 * Provides CacheItemPool and derived bindings
34
 *
35
 * [...]
36
 *
37
 * The following bindings are provided:
38
 *
39
 *  ::RedisDsn
40
 *  CacheItemPoolInterface::Local
41
 *  CacheItemPoolInterface::Shared
42
 */
43
final class StorageRedisDsnModule extends AbstractModule
44
{
45
    /**
46
     * Redis configuration DSN
47
     *
48
     * @param string                                                   $dsn     Redis DSN
49
     * @param array<string, bool|int|string|array<string, mixed>|null> $options Redis DSN Options
50
     */
51
    public function __construct(
52
        private readonly string $dsn,
53
        private readonly array $options = [],
54
        AbstractModule|null $module = null,
55
    ) {
56
        parent::__construct($module);
57
    }
58
59
    /** @throws ReflectionException */
60
    protected function configure(): void
61
    {
62
        $this->bind()->annotatedWith(RedisDsn::class)->toInstance($this->dsn);
63
        $this->bind()->annotatedWith(RedisDsnOptions::class)->toInstance($this->options);
64
        $this->bind(CacheItemPoolInterface::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON);
65
        $this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON);
66
        $this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(RedisAdapter::class, [
67
            'redisProvider' => 'redis',
68
            'namespace' => CacheNamespace::class,
69
        ]);
70
        $this->bind(ProviderInterface::class)->annotatedWith('redis')->to(RedisDsnProvider::class);
71
        $this->bind()->annotatedWith('redis')->toProvider(RedisDsnProvider::class);
72
        $this->bind(TagAwareAdapterInterface::class)->annotatedWith(ResourceObjectPool::class)->toConstructor(
73
            RedisTagAwareAdapter::class,
74
            [
75
                'redis' => 'redis',
76
                'namespace' => CacheNamespace::class,
77
            ],
78
        );
79
    }
80
}
81