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

StorageRedisDsnModule::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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