Passed
Pull Request — 1.x (#144)
by Akihito
04:47 queued 02:20
created

StorageRedisDsnModule::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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 Psr\Cache\CacheItemPoolInterface;
10
use Ray\Di\AbstractModule;
11
use Ray\Di\ProviderInterface;
12
use Ray\Di\Scope;
13
use Ray\PsrCacheModule\Annotation\CacheNamespace;
14
use Ray\PsrCacheModule\Annotation\Local;
15
use Ray\PsrCacheModule\Annotation\Shared;
16
use Ray\PsrCacheModule\ApcuAdapter;
17
use Ray\PsrCacheModule\RedisAdapter;
18
use ReflectionException;
19
20
/**
21
 * Provides ResourceStorageInterface and derived bindings
22
 * *
23
 * * The following bindings are provided:
24
 * *
25
 * * CacheItemPoolInterface-EtagPool::class
26
 * *
27
 * * The following module are installed:
28
 * *
29
 * * Psr6RedisModule
30
 * Provides CacheItemPool and derived bindings
31
 *
32
 * [...]
33
 *
34
 * The following bindings are provided:
35
 *
36
 *  ::RedisDsn
37
 *  CacheItemPoolInterface::Local
38
 *  CacheItemPoolInterface::Shared
39
 */
40
final class StorageRedisDsnModule extends AbstractModule
41
{
42
    /**
43
     * Redis configuration DSN
44
     *
45
     * See https://symfony.com/doc/current/components/cache/adapters/redis_adapter.html
46
     *
47
     * @param string              $dsn
48
     * @param array               $dsnOptions
49
     * @param AbstractModule|null $module
50
     */
51
    public function __construct(
52
        private readonly string $dsn,
53
        private readonly array $dsnOptions = [],
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->dsnOptions);
64
        $this->bind(CacheItemPoolInterface::class)->annotatedWith(Local::class)->toConstructor(ApcuAdapter::class, ['namespace' => CacheNamespace::class])->in(Scope::SINGLETON);
65
        $this->bind(CacheItemPoolInterface::class)->annotatedWith(Shared::class)->toConstructor(RedisAdapter::class, [
66
            'redisProvider' => 'redis',
67
            'namespace' => CacheNamespace::class,
68
        ]);
69
        $this->bind(ProviderInterface::class)->annotatedWith('redis')->to(RedisDsnProvider::class);
70
    }
71
}
72