Completed
Push — master ( 3dae8f...74637f )
by Masiukevich
15:41 queued 14:21
created

EventSourcingModule::withSqlStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Event Sourcing implementation module
5
 *
6
 * @author  Maksim Masiukevich <[email protected]>
7
 * @license MIT
8
 * @license https://opensource.org/licenses/MIT
9
 */
10
11
declare(strict_types = 1);
12
13
namespace ServiceBus\EventSourcingModule;
14
15
use ServiceBus\Common\Module\ServiceBusModule;
16
use ServiceBus\EventSourcing\EventStream\EventStreamRepository;
17
use ServiceBus\EventSourcing\EventStream\Store\EventStreamStore;
18
use ServiceBus\EventSourcing\EventStream\Store\SqlEventStreamStore;
19
use ServiceBus\EventSourcing\Indexes\Store\IndexStore;
20
use ServiceBus\EventSourcing\Indexes\Store\SqlIndexStore;
21
use ServiceBus\EventSourcing\Snapshots\Snapshotter;
22
use ServiceBus\EventSourcing\Snapshots\Store\SnapshotStore;
23
use ServiceBus\EventSourcing\Snapshots\Store\SqlSnapshotStore;
24
use ServiceBus\EventSourcing\Snapshots\Triggers\SnapshotTrigger;
25
use ServiceBus\EventSourcing\Snapshots\Triggers\SnapshotVersionTrigger;
26
use Symfony\Component\DependencyInjection\ContainerBuilder;
27
use Symfony\Component\DependencyInjection\Definition;
28
use Symfony\Component\DependencyInjection\Reference;
29
30
/**
31
 * @todo: custom store initialization
32
 */
33
final class EventSourcingModule implements ServiceBusModule
34
{
35
    /**
36
     * @var string
37
     */
38
    private $eventStoreServiceId;
39
40
    /**
41
     * @var string
42
     */
43
    private $snapshotStoreServiceId;
44
45
    /**
46
     * @var string
47
     */
48
    private $indexerStore;
49
50
    /**
51
     * @var string|null
52
     */
53
    private $databaseAdapterServiceId;
54
55
    /**
56
     * @var string|null
57
     */
58
    private $customEventSerializerServiceId;
59
60
    /**
61
     * @var string|null
62
     */
63
    private $customSnapshotStrategyServiceId;
64
65
    /**
66
     * @param string $databaseAdapterServiceId
67
     *
68
     * @return self
69
     */
70 1
    public static function withSqlStorage(string $databaseAdapterServiceId): self
71
    {
72 1
        $self = new self(
73 1
            EventStreamStore::class,
74 1
            SnapshotStore::class,
75 1
            IndexStore::class
76
        );
77
78 1
        $self->databaseAdapterServiceId = $databaseAdapterServiceId;
79
80 1
        return $self;
81
    }
82
83
    /**
84
     * @param string $eventSerializerServiceId
85
     *
86
     * @return $this
87
     */
88
    public function withCustomEventSerializer(string $eventSerializerServiceId): self
89
    {
90
        $this->customEventSerializerServiceId = $eventSerializerServiceId;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $snapshotStrategyServiceId
97
     *
98
     * @return $this
99
     */
100
    public function withCustomSnapshotStrategy(string $snapshotStrategyServiceId): self
101
    {
102
        $this->customSnapshotStrategyServiceId = $snapshotStrategyServiceId;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110 1
    public function boot(ContainerBuilder $containerBuilder): void
111
    {
112
        /** Default configuration used */
113 1
        if(null !== $this->databaseAdapterServiceId)
114
        {
115 1
            $storeArguments = [new Reference($this->databaseAdapterServiceId)];
116
117 1
            $containerBuilder->addDefinitions([
118 1
                $this->eventStoreServiceId    => (new Definition(SqlEventStreamStore::class))->setArguments($storeArguments),
119 1
                $this->snapshotStoreServiceId => (new Definition(SqlSnapshotStore::class))->setArguments($storeArguments),
120 1
                $this->indexerStore           => (new Definition(SqlIndexStore::class))->setArguments($storeArguments)
121
            ]);
122
        }
123
124
125 1
        $this->registerSnapshotter($containerBuilder);
126 1
        $this->registerEventSourcingProvider($containerBuilder);
127 1
        $this->registerIndexer($containerBuilder);
128 1
    }
129
130
    /**
131
     * @param ContainerBuilder $containerBuilder
132
     *
133
     * @return void
134
     */
135 1
    private function registerIndexer(ContainerBuilder $containerBuilder): void
136
    {
137 1
        $containerBuilder->addDefinitions([
138 1
            $this->indexerStore  => (new Definition(SqlIndexStore::class))->setArguments([new Reference($this->databaseAdapterServiceId)]),
0 ignored issues
show
Bug introduced by
It seems like $this->databaseAdapterServiceId can also be of type null; however, parameter $id of Symfony\Component\Depend...eference::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
            $this->indexerStore  => (new Definition(SqlIndexStore::class))->setArguments([new Reference(/** @scrutinizer ignore-type */ $this->databaseAdapterServiceId)]),
Loading history...
139 1
            IndexProvider::class => (new Definition(IndexProvider::class))->setArguments([new Reference($this->indexerStore)])
140
        ]);
141 1
    }
142
143
    /**
144
     * @param ContainerBuilder $containerBuilder
145
     *
146
     * @return void
147
     */
148 1
    private function registerEventSourcingProvider(ContainerBuilder $containerBuilder): void
149
    {
150
        $arguments = [
151 1
            new Reference($this->eventStoreServiceId),
152 1
            new Reference(Snapshotter::class),
153 1
            null !== $this->customEventSerializerServiceId
154
                ? new Reference($this->customEventSerializerServiceId)
155
                : null,
156 1
            new Reference('service_bus.logger')
157
        ];
158
159 1
        $containerBuilder->addDefinitions([
160 1
            EventStreamRepository::class => (new Definition(EventStreamRepository::class))->setArguments($arguments),
161 1
            EventSourcingProvider::class => (new Definition(EventSourcingProvider::class))->setArguments([new Reference(EventStreamRepository::class)])
162
        ]);
163 1
    }
164
165
    /**
166
     * @param ContainerBuilder $containerBuilder
167
     *
168
     * @return void
169
     */
170 1
    private function registerSnapshotter(ContainerBuilder $containerBuilder): void
171
    {
172 1
        if(null === $this->customSnapshotStrategyServiceId)
173
        {
174 1
            $containerBuilder->addDefinitions([
175 1
                SnapshotTrigger::class => new Definition(SnapshotVersionTrigger::class)
176
            ]);
177
178 1
            $this->customSnapshotStrategyServiceId = SnapshotTrigger::class;
179
        }
180
181
        $arguments = [
182 1
            new Reference($this->snapshotStoreServiceId),
183 1
            new Reference($this->customSnapshotStrategyServiceId),
184 1
            new Reference('service_bus.logger')
185
        ];
186
187 1
        $containerBuilder->addDefinitions([
188 1
            Snapshotter::class => (new Definition(Snapshotter::class))->setArguments($arguments)
189
        ]);
190 1
    }
191
192
    /**
193
     * @param string $eventStoreServiceId
194
     * @param string $snapshotStoreServiceId
195
     * @param string $indexerStore
196
     */
197 1
    private function __construct(string $eventStoreServiceId, string $snapshotStoreServiceId, string $indexerStore)
198
    {
199 1
        $this->eventStoreServiceId    = $eventStoreServiceId;
200 1
        $this->snapshotStoreServiceId = $snapshotStoreServiceId;
201 1
        $this->indexerStore           = $indexerStore;
202 1
    }
203
}
204