StorageRedisDsnModule::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\QueryRepository;
6
7
use BEAR\RepositoryModule\Annotation\MarshallerOptions;
8
use BEAR\RepositoryModule\Annotation\RedisDsn;
9
use BEAR\RepositoryModule\Annotation\RedisDsnOptions;
10
use BEAR\RepositoryModule\Annotation\ResourceObjectPool;
11
use Override;
12
use Ray\Di\AbstractModule;
13
use Ray\Di\ProviderInterface;
14
use Ray\PsrCacheModule\Annotation\CacheNamespace;
15
use ReflectionException;
16
use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
17
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
18
use Symfony\Component\Cache\Marshaller\MarshallerInterface;
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
     * @param string                                                   $dsn                Redis DSN
46
     * @param array<string, bool|int|string|array<string, mixed>|null> $options            Redis DSN Options
47
     * @param int                                                      $defaultLifetime    Default lifetime for cache items in seconds
48
     * @param array<string, mixed>                                     $marshallingOptions Marshalling options
49
     *
50
     * Example with compression:
51
     * <code>
52
     * new StorageRedisDsnModule(
53
     *     dsn: 'redis://localhost:6379',
54
     *     marshallingOptions: [
55
     *         'enabled' => true,
56
     *         'type' => 'deflate',      // 'default' or 'deflate'
57
     *         'use_igbinary' => true    // requires ext-igbinary
58
     *     ]
59
     * );
60
     * </code>
61
     *
62
     * Marshaller types:
63
     * - 'default': Standard serialization with optional igbinary support
64
     * - 'deflate': Compression using zlib (reduces memory usage)
65
     */
66
    public function __construct(
67
        private readonly string $dsn,
68
        private readonly array $options = [],
69
        private readonly int $defaultLifetime = 0,
70
        private readonly array $marshallingOptions = [],
71
        AbstractModule|null $module = null,
72
    ) {
73
        parent::__construct($module);
74
    }
75
76
    /** @throws ReflectionException */
77
    #[Override]
78
    protected function configure(): void
79
    {
80
        $this->bind()->annotatedWith(RedisDsn::class)->toInstance($this->dsn);
81
        $this->bind()->annotatedWith(RedisDsnOptions::class)->toInstance($this->options);
82
        $this->bind()->annotatedWith(MarshallerOptions::class)->toInstance($this->marshallingOptions);
83
        $this->bind(ProviderInterface::class)->annotatedWith('redis')->to(RedisDsnProvider::class);
84
        $this->bind()->annotatedWith('redis')->toProvider(RedisDsnProvider::class);
85
        $this->bind()->annotatedWith('defaultLifetime')->toInstance($this->defaultLifetime);
86
        $this->bind(ProviderInterface::class)->annotatedWith('marshaller')->to(MarshallerProvider::class);
87
        $this->bind(MarshallerInterface::class)->annotatedWith('marshaller')->toProvider(MarshallerProvider::class);
88
        $this->bind(TagAwareAdapterInterface::class)->annotatedWith(ResourceObjectPool::class)->toConstructor(
89
            RedisTagAwareAdapter::class,
90
            [
91
                'redis' => 'redis',
92
                'namespace' => CacheNamespace::class,
93
                'defaultLifetime' => 'defaultLifetime',
94
                'marshaller' => 'marshaller',
95
            ],
96
        );
97
    }
98
}
99