|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple EventStore Manager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace SimpleEventStoreManager\Application\Event; |
|
12
|
|
|
|
|
13
|
|
|
use Elasticsearch\ClientBuilder; |
|
14
|
|
|
use SimpleEventStoreManager\Application\Event\Exceptions\NotSupportedDriverException; |
|
15
|
|
|
use SimpleEventStoreManager\Application\Event\Exceptions\NotSupportedReturnTypeException; |
|
16
|
|
|
use SimpleEventStoreManager\Application\Event\Exceptions\NotValidEventException; |
|
17
|
|
|
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface; |
|
18
|
|
|
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface; |
|
19
|
|
|
use SimpleEventStoreManager\Infrastructure\Services\ElasticService; |
|
20
|
|
|
|
|
21
|
|
|
class EventManager |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $driver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $connectionParams; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var EventStoreRepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $repo; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private $returnType; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ElasticService |
|
45
|
|
|
*/ |
|
46
|
|
|
private $elastic; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return EventManager |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function build() |
|
52
|
|
|
{ |
|
53
|
|
|
return new self(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $driver |
|
58
|
|
|
* @return $this |
|
59
|
|
|
* @throws NotSupportedDriverException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setDriver($driver) |
|
62
|
|
|
{ |
|
63
|
|
|
$allowedDrivers = [ |
|
64
|
|
|
'dbal', |
|
65
|
|
|
'in-memory', |
|
66
|
|
|
'mongo', |
|
67
|
|
|
'pdo', |
|
68
|
|
|
'redis', |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
if (!in_array($driver, $allowedDrivers)) { |
|
72
|
|
|
throw new NotSupportedDriverException($driver.' is not a supported driver.'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->driver = $driver; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $parameters |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
|
|
public function setConnection(array $parameters = []) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->connectionParams = $parameters; |
|
87
|
|
|
$this->setRepo(); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* setRepo. |
|
94
|
|
|
*/ |
|
95
|
|
|
private function setRepo() |
|
96
|
|
|
{ |
|
97
|
|
|
$aggregateRepo = 'SimpleEventStoreManager\Infrastructure\Persistence\\'.$this->normalizeDriverName($this->driver).'EventStoreRepository'; |
|
98
|
|
|
$driver = 'SimpleEventStoreManager\Infrastructure\Drivers\\'.$this->normalizeDriverName($this->driver).'Driver'; |
|
99
|
|
|
$instance = (new $driver($this->connectionParams))->instance(); |
|
100
|
|
|
$this->repo = new $aggregateRepo($instance); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $driver |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
private function normalizeDriverName($driver) |
|
109
|
|
|
{ |
|
110
|
|
|
$driver = str_replace([' ', '-'], '', $driver); |
|
111
|
|
|
|
|
112
|
|
|
return ucwords($driver); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return EventStoreRepositoryInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getRepo() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repo; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param int $returnType |
|
125
|
|
|
* @return $this |
|
126
|
|
|
* @throws NotSupportedReturnTypeException |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setReturnType($returnType = EventStoreRepositoryInterface::RETURN_AS_ARRAY) |
|
129
|
|
|
{ |
|
130
|
|
|
if (false === $this->checkReturnType($returnType)) { |
|
131
|
|
|
throw new NotSupportedReturnTypeException($returnType . ' is not a valid returnType value.'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$this->returnType = $returnType; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param $returnType |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
private function checkReturnType($returnType) |
|
144
|
|
|
{ |
|
145
|
|
|
$allowedReturnTypeArray = [EventStoreRepositoryInterface::RETURN_AS_ARRAY, EventStoreRepositoryInterface::RETURN_AS_OBJECT]; |
|
146
|
|
|
|
|
147
|
|
|
return in_array($returnType, $allowedReturnTypeArray); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return int |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getReturnType() |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->returnType; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $hosts |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setElasticServer(array $hosts = []) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->elastic = new ElasticService( |
|
165
|
|
|
ClientBuilder::create() |
|
166
|
|
|
->setHosts($hosts) |
|
167
|
|
|
->build() |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param array $events |
|
175
|
|
|
* @throws NotValidEventException |
|
176
|
|
|
*/ |
|
177
|
|
|
public function storeEvents(array $events = []) |
|
178
|
|
|
{ |
|
179
|
|
|
/** @var EventInterface $event */ |
|
180
|
|
|
foreach ($events as $event) { |
|
181
|
|
|
if (!$event instanceof EventInterface) { |
|
182
|
|
|
throw new NotValidEventException('Not a valid instance of EventInterface was provided.'); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$this->repo->save($event); |
|
186
|
|
|
|
|
187
|
|
|
if ($this->elastic) { |
|
188
|
|
|
$this->elastic->addAggregateToIndex($event); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|