|
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 function Amp\call; |
|
16
|
|
|
use Amp\Promise; |
|
17
|
|
|
use ServiceBus\EventSourcing\Indexes\IndexKey; |
|
18
|
|
|
use ServiceBus\EventSourcing\Indexes\IndexValue; |
|
19
|
|
|
use ServiceBus\EventSourcing\Indexes\Store\IndexStore; |
|
20
|
|
|
use ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed; |
|
21
|
|
|
use ServiceBus\Storage\Common\Exceptions\UniqueConstraintViolationCheckFailed; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
final class IndexProvider |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var IndexStore |
|
30
|
|
|
*/ |
|
31
|
|
|
private $store; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param IndexStore $store |
|
35
|
|
|
*/ |
|
36
|
6 |
|
public function __construct(IndexStore $store) |
|
37
|
|
|
{ |
|
38
|
6 |
|
$this->store = $store; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Receive index value. |
|
43
|
|
|
* |
|
44
|
|
|
* @noinspection PhpDocRedundantThrowsInspection |
|
45
|
|
|
* @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise |
|
46
|
|
|
* |
|
47
|
|
|
* @param IndexKey $indexKey |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed |
|
50
|
|
|
* |
|
51
|
|
|
* @return Promise<\ServiceBus\EventSourcing\Indexes\IndexValue|null> |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function get(IndexKey $indexKey): Promise |
|
54
|
|
|
{ |
|
55
|
|
|
/** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */ |
|
56
|
3 |
|
return call( |
|
57
|
|
|
function(IndexKey $indexKey): \Generator |
|
58
|
|
|
{ |
|
59
|
|
|
try |
|
60
|
|
|
{ |
|
61
|
|
|
/** |
|
62
|
|
|
* @psalm-suppress TooManyTemplateParams Wrong Promise template |
|
63
|
|
|
* |
|
64
|
|
|
* @var IndexValue|null $value |
|
65
|
|
|
*/ |
|
66
|
3 |
|
$value = yield $this->store->find($indexKey); |
|
67
|
|
|
|
|
68
|
3 |
|
return $value; |
|
69
|
|
|
} |
|
70
|
|
|
catch (\Throwable $throwable) |
|
71
|
|
|
{ |
|
72
|
|
|
throw IndexOperationFailed::fromThrowable($throwable); |
|
73
|
|
|
} |
|
74
|
3 |
|
}, |
|
75
|
3 |
|
$indexKey |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Is there a value in the index. |
|
81
|
|
|
* |
|
82
|
|
|
* @noinspection PhpDocRedundantThrowsInspection |
|
83
|
|
|
* @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise |
|
84
|
|
|
* |
|
85
|
|
|
* @param IndexKey $indexKey |
|
86
|
|
|
* |
|
87
|
|
|
* @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed |
|
88
|
|
|
* |
|
89
|
|
|
* @return Promise<bool> |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function has(IndexKey $indexKey): Promise |
|
92
|
|
|
{ |
|
93
|
|
|
/** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */ |
|
94
|
1 |
|
return call( |
|
95
|
|
|
function(IndexKey $indexKey): \Generator |
|
96
|
|
|
{ |
|
97
|
|
|
try |
|
98
|
|
|
{ |
|
99
|
|
|
/** |
|
100
|
|
|
* @psalm-suppress TooManyTemplateParams Wrong Promise template |
|
101
|
|
|
* |
|
102
|
|
|
* @var IndexValue|null $value |
|
103
|
|
|
*/ |
|
104
|
1 |
|
$value = yield $this->store->find($indexKey); |
|
105
|
|
|
|
|
106
|
1 |
|
return null !== $value; |
|
107
|
|
|
} |
|
108
|
|
|
catch (\Throwable $throwable) |
|
109
|
|
|
{ |
|
110
|
|
|
throw IndexOperationFailed::fromThrowable($throwable); |
|
111
|
|
|
} |
|
112
|
1 |
|
}, |
|
113
|
1 |
|
$indexKey |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add a value to the index. If false, then the value already exists. |
|
119
|
|
|
* |
|
120
|
|
|
* @noinspection PhpDocRedundantThrowsInspection |
|
121
|
|
|
* @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise |
|
122
|
|
|
* |
|
123
|
|
|
* @param IndexKey $indexKey |
|
124
|
|
|
* @param IndexValue $value |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed |
|
127
|
|
|
* |
|
128
|
|
|
* @return Promise<bool> |
|
129
|
|
|
*/ |
|
130
|
5 |
|
public function add(IndexKey $indexKey, IndexValue $value): Promise |
|
131
|
|
|
{ |
|
132
|
|
|
/** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */ |
|
133
|
5 |
|
return call( |
|
134
|
|
|
function(IndexKey $indexKey, IndexValue $value): \Generator |
|
135
|
|
|
{ |
|
136
|
|
|
try |
|
137
|
|
|
{ |
|
138
|
|
|
/** |
|
139
|
|
|
* @psalm-suppress TooManyTemplateParams Wrong Promise template |
|
140
|
|
|
* |
|
141
|
|
|
* @var int $affectedRows |
|
142
|
|
|
*/ |
|
143
|
5 |
|
$affectedRows = yield $this->store->add($indexKey, $value); |
|
144
|
|
|
|
|
145
|
5 |
|
return 0 !== $affectedRows; |
|
146
|
|
|
} |
|
147
|
1 |
|
catch (UniqueConstraintViolationCheckFailed $exception) |
|
148
|
|
|
{ |
|
149
|
1 |
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
catch (\Throwable $throwable) |
|
152
|
|
|
{ |
|
153
|
|
|
throw IndexOperationFailed::fromThrowable($throwable); |
|
154
|
|
|
} |
|
155
|
5 |
|
}, |
|
156
|
5 |
|
$indexKey, |
|
157
|
5 |
|
$value |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Remove value from index. |
|
163
|
|
|
* |
|
164
|
|
|
* @noinspection PhpDocRedundantThrowsInspection |
|
165
|
|
|
* |
|
166
|
|
|
* @param IndexKey $indexKey |
|
167
|
|
|
* |
|
168
|
|
|
* @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed |
|
169
|
|
|
* |
|
170
|
|
|
* @return Promise It doesn't return any result |
|
171
|
|
|
*/ |
|
172
|
1 |
|
public function remove(IndexKey $indexKey): Promise |
|
173
|
|
|
{ |
|
174
|
|
|
/** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */ |
|
175
|
1 |
|
return call( |
|
176
|
|
|
function(IndexKey $indexKey): \Generator |
|
177
|
|
|
{ |
|
178
|
|
|
try |
|
179
|
|
|
{ |
|
180
|
1 |
|
yield $this->store->delete($indexKey); |
|
181
|
|
|
} |
|
182
|
|
|
catch (\Throwable $throwable) |
|
183
|
|
|
{ |
|
184
|
|
|
throw IndexOperationFailed::fromThrowable($throwable); |
|
185
|
|
|
} |
|
186
|
1 |
|
}, |
|
187
|
1 |
|
$indexKey |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Update value in index. |
|
193
|
|
|
* |
|
194
|
|
|
* @noinspection PhpDocRedundantThrowsInspection |
|
195
|
|
|
* @psalm-suppress MixedTypeCoercion Incorrect resolving the value of the promise |
|
196
|
|
|
* |
|
197
|
|
|
* @param IndexKey $indexKey |
|
198
|
|
|
* @param IndexValue $value |
|
199
|
|
|
* |
|
200
|
|
|
* @throws \ServiceBus\EventSourcingModule\Exceptions\IndexOperationFailed |
|
201
|
|
|
* |
|
202
|
|
|
* @return Promise<bool> |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function update(IndexKey $indexKey, IndexValue $value): Promise |
|
205
|
|
|
{ |
|
206
|
|
|
/** @psalm-suppress InvalidArgument Incorrect psalm unpack parameters (...$args) */ |
|
207
|
1 |
|
return call( |
|
208
|
|
|
function(IndexKey $indexKey, IndexValue $value): \Generator |
|
209
|
|
|
{ |
|
210
|
|
|
try |
|
211
|
|
|
{ |
|
212
|
|
|
/** |
|
213
|
|
|
* @psalm-suppress TooManyTemplateParams Wrong Promise template |
|
214
|
|
|
* |
|
215
|
|
|
* @var int $affectedRows |
|
216
|
|
|
*/ |
|
217
|
1 |
|
$affectedRows = yield $this->store->update($indexKey, $value); |
|
218
|
|
|
|
|
219
|
1 |
|
return 0 !== $affectedRows; |
|
220
|
|
|
} |
|
221
|
|
|
catch (\Throwable $throwable) |
|
222
|
|
|
{ |
|
223
|
|
|
throw IndexOperationFailed::fromThrowable($throwable); |
|
224
|
|
|
} |
|
225
|
1 |
|
}, |
|
226
|
1 |
|
$indexKey, |
|
227
|
1 |
|
$value |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|