-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedis.php
159 lines (143 loc) · 5.65 KB
/
Redis.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
<?php
declare(strict_types=1);
namespace TwanHaverkamp\EventStorageInRedisWithPhp\Event\EventStore;
use DateTime;
use DateTimeImmutable;
use Generator;
use JsonException;
use Predis\ClientInterface as PredisClientInterface;
use Predis\PredisException;
use TwanHaverkamp\EventSourcingWithPhp\Aggregate;
use TwanHaverkamp\EventSourcingWithPhp\Event;
use TwanHaverkamp\EventSourcingWithPhp\Event\EventDescriber;
use TwanHaverkamp\EventSourcingWithPhp\Event\EventStore;
use TwanHaverkamp\EventSourcingWithPhp\Event\Exception;
class Redis implements EventStore\EventStoreInterface
{
protected const int KEY_RANGE_LIMIT = 25;
public function __construct(
protected PredisClientInterface $client,
protected EventDescriber\EventDescriberInterface $describer,
) {
}
/**
* @throws Exception\EventRetrievalFailedException when fetching keys with ZRANGE fails or
* when fetching events with GET fails.
*/
public function load(Aggregate\AggregateInterface $aggregate): void
{
foreach ($this->getKeys($aggregate) as $key) {
try {
/** @var string $json */
$json = $this->client->get($key);
} catch (PredisException $e) {
throw new Exception\EventRetrievalFailedException(
message: "Failed to fetch Event with key \"$key\".",
previous: $e,
);
}
/**
* @var array{
* eventClass: class-string<Event\EventInterface>,
* payload: array<string, mixed>,
* recordedAt: string,
* microseconds: int,
* } $data
*/
$data = json_decode($json, true);
/** @var DateTime $recordedAt */
$recordedAt = DateTime::createFromFormat(DATE_ATOM, $data['recordedAt']);
$recordedAt->setTime(
(int)$recordedAt->format('H'),
(int)$recordedAt->format('i'),
(int)$recordedAt->format('s'),
$data['microseconds'],
);
$aggregate->apply($data['eventClass']::fromPayload(
$aggregate->getAggregateRootId(),
$data['payload'],
DateTimeImmutable::createFromMutable($recordedAt),
));
}
}
/**
* @throws Exception\EventStorageFailedException when JSON encoding of a payload throws a {@see JsonException}
* or the Redis client throws a {@see PredisException}.
*/
public function save(Aggregate\AggregateInterface $aggregate): void
{
foreach ($aggregate->getEvents() as $event) {
try {
$this->client->set($key = $this->createKey($event), json_encode([
'eventClass' => $event::class,
'payload' => $event->getPayload(),
'recordedAt' => $event->getRecordedAt()->format(DATE_ATOM),
'microseconds' => (int)$event->getRecordedAt()->format('u'),
], JSON_THROW_ON_ERROR));
} catch (JsonException | PredisException $e) {
throw new Exception\EventStorageFailedException(
message: sprintf(
'Failed to store Event "%s" for Aggregate with AggregateRootId %s.',
$event::class,
$event->getAggregateRootId()->toString(),
),
previous: $e,
);
}
try {
$this->client->zadd(
$aggregate->getAggregateRootId()->toString(),
[$key => (int)$event->getRecordedAt()->format('Uu')],
);
} catch (PredisException $e) {
throw new Exception\EventStorageFailedException(
message: sprintf(
'Failed to store key "%s" for Aggregate with AggregateRootId %s.',
$key,
$aggregate->getAggregateRootId()->toString(),
),
previous: $e,
);
}
$aggregate->remove($event);
}
}
protected function createKey(Event\EventInterface $event): string
{
return sprintf(
'%s:%d-%s',
$event->getAggregateRootId()->toString(),
$event->getRecordedAt()->format('Uu'),
$this->describer->describe($event),
);
}
/**
* @return Generator<string>
* @throws Exception\EventStorageFailedException when fetching keys with ZRANGE fails.
*/
protected function getKeys(Aggregate\AggregateInterface $aggregate): Generator
{
do {
try {
$keys = $this->client->zrange(
$aggregate->getAggregateRootId()->toString(),
$start ??= 0,
$stop ??= (static::KEY_RANGE_LIMIT - 1),
);
} catch (PredisException $e) {
throw new Exception\EventRetrievalFailedException(
message: sprintf(
'Failed to fetch keys for Aggregate with AggregateRootId %s.',
$aggregate->getAggregateRootId()->toString(),
),
previous: $e,
);
}
foreach ($keys as $key) {
yield $key;
}
$start += static::KEY_RANGE_LIMIT;
$stop += static::KEY_RANGE_LIMIT;
} while (count($keys) === static::KEY_RANGE_LIMIT);
}
}