forked from EventSaucePHP/MessageRepositoryForIlluminate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IlluminateUuidV4MessageRepository.php
155 lines (133 loc) · 5.81 KB
/
IlluminateUuidV4MessageRepository.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
<?php
namespace EventSauce\MessageRepository\IlluminateMessageRepository;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Header;
use EventSauce\EventSourcing\Message;
use EventSauce\EventSourcing\MessageRepository;
use EventSauce\EventSourcing\OffsetCursor;
use EventSauce\EventSourcing\PaginationCursor;
use EventSauce\EventSourcing\Serialization\MessageSerializer;
use EventSauce\EventSourcing\UnableToPersistMessages;
use EventSauce\EventSourcing\UnableToRetrieveMessages;
use EventSauce\MessageRepository\TableSchema\DefaultTableSchema;
use EventSauce\MessageRepository\TableSchema\TableSchema;
use EventSauce\UuidEncoding\BinaryUuidEncoder;
use EventSauce\UuidEncoding\UuidEncoder;
use Generator;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Collection;
use LogicException;
use Ramsey\Uuid\Uuid;
use Throwable;
use function count;
use function get_class;
use function json_decode;
use function sprintf;
/**
* @deprecated Will be removed in 2.0.0
* @see IlluminateMessageRepository
*/
class IlluminateUuidV4MessageRepository implements MessageRepository
{
private TableSchema $tableSchema;
private UuidEncoder $uuidEncoder;
public function __construct(
private ConnectionInterface $connection,
private string $tableName,
private MessageSerializer $serializer,
private int $jsonEncodeOptions = 0,
?TableSchema $tableSchema = null,
?UuidEncoder $uuidEncoder = null,
) {
$this->tableSchema = $tableSchema ?? new DefaultTableSchema();
$this->uuidEncoder = $uuidEncoder ?? new BinaryUuidEncoder();
}
public function persist(Message ...$messages): void
{
if (count($messages) === 0) {
return;
}
$values = [];
$versionColumn = $this->tableSchema->versionColumn();
$eventIdColumn = $this->tableSchema->eventIdColumn();
$payloadColumn = $this->tableSchema->payloadColumn();
$aggregateRootIdColumn = $this->tableSchema->aggregateRootIdColumn();
$additionalColumns = $this->tableSchema->additionalColumns();
foreach ($messages as $message) {
$parameters = [];
$payload = $this->serializer->serializeMessage($message);
$parameters[$versionColumn] = $payload['headers'][Header::AGGREGATE_ROOT_VERSION] ?? 0;
$payload['headers'][Header::EVENT_ID] = $payload['headers'][Header::EVENT_ID] ?? Uuid::uuid4()->toString();
$parameters[$eventIdColumn] = $this->uuidEncoder->encodeString($payload['headers'][Header::EVENT_ID]);
$parameters[$payloadColumn] = json_encode($payload, $this->jsonEncodeOptions);
$parameters[$aggregateRootIdColumn] = $this->uuidEncoder->encodeString($payload['headers'][Header::AGGREGATE_ROOT_ID]);
foreach ($additionalColumns as $column => $header) {
$parameters[$column] = $payload['headers'][$header];
}
$values[] = $parameters;
}
try {
$this->connection->table($this->tableName)->insert($values);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
}
public function retrieveAll(AggregateRootId $id): Generator
{
$builder = $this->connection->table($this->tableName)
->where($this->tableSchema->aggregateRootIdColumn(), $this->uuidEncoder->encodeString($id->toString()))
->orderBy($this->tableSchema->versionColumn(), 'ASC');
try {
return $this->yieldMessagesForResult($builder->get(['payload']));
} catch (Throwable $exception) {
throw UnableToRetrieveMessages::dueTo('', $exception);
}
}
/** @psalm-return Generator<Message> */
public function retrieveAllAfterVersion(AggregateRootId $id, int $aggregateRootVersion): Generator
{
$versionColumn = $this->tableSchema->versionColumn();
$builder = $this->connection->table($this->tableName)
->where($this->tableSchema->aggregateRootIdColumn(), $this->uuidEncoder->encodeString($id->toString()))
->where($versionColumn, '>', $aggregateRootVersion)
->orderBy($versionColumn, 'ASC');
try {
return $this->yieldMessagesForResult($builder->get(['payload']));
} catch (Throwable $exception) {
throw UnableToRetrieveMessages::dueTo('', $exception);
}
}
/**
* @param Collection<int, mixed> $result
* @psalm-return Generator<int, Message>
*/
private function yieldMessagesForResult(Collection $result): Generator
{
foreach ($result as $row) {
yield $message = $this->serializer->unserializePayload(json_decode($row->payload, true));
}
return isset($message) ? $message->header(Header::AGGREGATE_ROOT_VERSION) ?: 0 : 0;
}
public function paginate(PaginationCursor $cursor): Generator
{
if ( ! $cursor instanceof OffsetCursor) {
throw new LogicException(sprintf('Wrong cursor type used, expected %s, received %s', OffsetCursor::class, get_class($cursor)));
}
$offset = $cursor->offset();
$incrementalIdColumn = $this->tableSchema->incrementalIdColumn();
$builder = $this->connection->table($this->tableName)
->limit($cursor->limit())
->where($incrementalIdColumn, '>', $cursor->offset())
->orderBy($incrementalIdColumn, 'ASC');
try {
$result = $builder->get(['payload']);
foreach ($result as $row) {
$offset++;
yield $this->serializer->unserializePayload(json_decode($row->payload, true));
}
return $cursor->withOffset($offset);
} catch (Throwable $exception) {
throw UnableToRetrieveMessages::dueTo('', $exception);
}
}
}