-
Notifications
You must be signed in to change notification settings - Fork 74
/
InMemoryEventStoreFactory.php
213 lines (177 loc) · 6.4 KB
/
InMemoryEventStoreFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
/**
* This file is part of prooph/event-store.
* (c) 2014-2020 prooph software GmbH <contact@prooph.de>
* (c) 2015-2020 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventStore\Container;
use Interop\Config\ConfigurationTrait;
use Interop\Config\ProvidesDefaultOptions;
use Interop\Config\RequiresConfig;
use Interop\Config\RequiresConfigId;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\ActionEventEmitterEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\ConfigurationException;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\InMemoryEventStore;
use Prooph\EventStore\Metadata\MetadataEnricher;
use Prooph\EventStore\Metadata\MetadataEnricherAggregate;
use Prooph\EventStore\Metadata\MetadataEnricherPlugin;
use Prooph\EventStore\NonTransactionalInMemoryEventStore;
use Prooph\EventStore\Plugin\Plugin;
use Prooph\EventStore\ReadOnlyEventStore;
use Prooph\EventStore\ReadOnlyEventStoreWrapper;
use Prooph\EventStore\TransactionalActionEventEmitterEventStore;
use Prooph\EventStore\TransactionalEventStore;
use Psr\Container\ContainerInterface;
final class InMemoryEventStoreFactory implements
ProvidesDefaultOptions,
RequiresConfig,
RequiresConfigId
{
use ConfigurationTrait;
/**
* @var string
*/
private $configId;
/**
* @var bool
*/
private $isTransactional;
/**
* Creates a new instance from a specified config, specifically meant to be used as static factory.
*
* In case you want to use another config key than provided by the factories, you can add the following factory to
* your config:
*
* <code>
* <?php
* return [
* InMemoryEventStore::class => [InMemoryEventStoreFactory::class, 'service_name'],
* ];
* </code>
*
* @throws InvalidArgumentException
*/
public static function __callStatic(string $name, array $arguments): ReadOnlyEventStore
{
if (! isset($arguments[0]) || ! $arguments[0] instanceof ContainerInterface) {
throw new InvalidArgumentException(
\sprintf('The first argument must be of type %s', ContainerInterface::class)
);
}
return (new static($name))->__invoke($arguments[0]);
}
public function __construct(string $configId = 'default')
{
$this->configId = $configId;
}
/**
* @throws ConfigurationException
*/
public function __invoke(ContainerInterface $container): ReadOnlyEventStore
{
$config = $container->get('config');
$config = $this->options($config, $this->configId);
$this->isTransactional = $this->isTransactional($config);
$eventStore = $this->createEventStore();
if ($config['read_only']) {
$eventStore = new ReadOnlyEventStoreWrapper($eventStore);
}
if (! $config['wrap_action_event_emitter']) {
return $eventStore;
}
if (! isset($config['event_emitter'])) {
$eventEmitter = new ProophActionEventEmitter($this->determineEventsForDefaultEmitter());
} else {
$eventEmitter = $container->get($config['event_emitter']);
}
$wrapper = $this->createActionEventEmitterDecorator($eventStore, $eventEmitter);
foreach ($config['plugins'] as $pluginAlias) {
$plugin = $container->get($pluginAlias);
if (! $plugin instanceof Plugin) {
throw ConfigurationException::configurationError(
\sprintf(
'Plugin %s does not implement the Plugin interface',
$pluginAlias
)
);
}
$plugin->attachToEventStore($wrapper);
}
if (\count($config['metadata_enrichers']) > 0) {
$metadataEnrichers = [];
foreach ($config['metadata_enrichers'] as $metadataEnricherAlias) {
$metadataEnricher = $container->get($metadataEnricherAlias);
if (! $metadataEnricher instanceof MetadataEnricher) {
throw ConfigurationException::configurationError(
\sprintf(
'Metadata enricher %s does not implement the MetadataEnricher interface',
$metadataEnricherAlias
)
);
}
$metadataEnrichers[] = $metadataEnricher;
}
$plugin = new MetadataEnricherPlugin(
new MetadataEnricherAggregate($metadataEnrichers)
);
$plugin->attachToEventStore($wrapper);
}
return $wrapper;
}
/**
* {@inheritdoc}
*/
public function dimensions(): iterable
{
return ['prooph', 'event_store'];
}
/**
* {@inheritdoc}
*/
public function defaultOptions(): iterable
{
return [
'metadata_enrichers' => [],
'plugins' => [],
'wrap_action_event_emitter' => true,
'transactional' => true,
'read_only' => false,
];
}
private function determineEventsForDefaultEmitter(): array
{
if ($this->isTransactional) {
return TransactionalActionEventEmitterEventStore::ALL_EVENTS;
}
return ActionEventEmitterEventStore::ALL_EVENTS;
}
private function createEventStore(): EventStore
{
if ($this->isTransactional) {
return new InMemoryEventStore();
}
return new NonTransactionalInMemoryEventStore();
}
private function createActionEventEmitterDecorator(
EventStore $eventStore,
ActionEventEmitter $actionEventEmitter
): ActionEventEmitterEventStore {
if ($this->isTransactional) {
/** @var TransactionalEventStore $eventStore */
return new TransactionalActionEventEmitterEventStore($eventStore, $actionEventEmitter);
}
return new ActionEventEmitterEventStore($eventStore, $actionEventEmitter);
}
private function isTransactional(array $config): bool
{
return isset($config['transactional']) && $config['transactional'] === true;
}
}