-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoctrineDbalMessageRepository.php
95 lines (77 loc) · 3.79 KB
/
DoctrineDbalMessageRepository.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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Bridge\Doctrine\EventSourcing;
use Doctrine\DBAL\Connection;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateId;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateIdInterface;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateVersion;
use SonsOfPHP\Component\EventSourcing\Aggregate\AggregateVersionInterface;
use SonsOfPHP\Component\EventSourcing\Exception\AggregateNotFoundException;
use SonsOfPHP\Component\EventSourcing\Exception\EventSourcingException;
use SonsOfPHP\Component\EventSourcing\Message\MessageInterface;
use SonsOfPHP\Component\EventSourcing\Message\Repository\MessageRepositoryInterface;
use SonsOfPHP\Component\EventSourcing\Message\Serializer\MessageSerializerInterface;
use SonsOfPHP\Component\EventSourcing\Metadata;
/**
* @author Joshua Estes <[email protected]>
*/
class DoctrineDbalMessageRepository implements MessageRepositoryInterface
{
public function __construct(private readonly Connection $connection, private readonly MessageSerializerInterface $serializer, private readonly TableSchemaInterface $tableSchema) {}
public function persist(MessageInterface $message): void
{
$message->getAggregateId();
$message->getAggregateVersion();
$data = $this->serializer->serialize($message);
$requiredMetadata = [
Metadata::EVENT_ID,
Metadata::EVENT_TYPE,
Metadata::AGGREGATE_ID,
Metadata::AGGREGATE_VERSION,
Metadata::TIMESTAMP,
Metadata::TIMESTAMP_FORMAT,
];
if (\count($requiredMetadata) !== \count(array_intersect_key(array_flip($requiredMetadata), $data['metadata']))) {
throw new EventSourcingException('metadata is missing one or more required values');
}
// returns int|string The number of aggected rows
$this->connection->insert(
$this->tableSchema->getTableName(),
$this->tableSchema->mapEventDataToColumns($data),
array_values($this->tableSchema->getColumns())
);
}
public function find(string|AggregateIdInterface $id, int|AggregateVersionInterface $version = null): iterable
{
if (!$id instanceof AggregateIdInterface) {
$id = new AggregateId($id);
}
if (\is_int($version)) {
$version = new AggregateVersion($version);
}
$columnsWithTypes = $this->tableSchema->getColumns();
$aggregateIdColumn = $this->tableSchema->getAggregateIdColumn();
$aggregateVersionColumn = $this->tableSchema->getAggregateVersionColumn();
$builder = $this->connection->createQueryBuilder();
$builder->select(array_keys($columnsWithTypes));
$builder->from($this->tableSchema->getTableName());
$builder->where(sprintf('%s = :aggregate_id', $aggregateIdColumn));
$builder->orderBy($aggregateVersionColumn, 'ASC');
$builder->setParameter('aggregate_id', $id->toString(), $columnsWithTypes[$aggregateIdColumn]);
if ($version instanceof AggregateVersionInterface) {
$builder->andWhere(sprintf('%s > :aggregate_version', $aggregateVersionColumn));
$builder->setParameter('aggregate_version', $version->toInt(), $columnsWithTypes[$aggregateVersionColumn]);
}
$results = $builder->executeQuery()->iterateAssociative(); // Generator
$resultCount = 0;
foreach ($results as $result) {
++$resultCount;
$data = $this->tableSchema->mapColumnsToEventData($result);
$message = $this->serializer->deserialize($data);
yield $message;
}
if (0 === $resultCount) {
throw new AggregateNotFoundException(sprintf('Aggregate "%s" could not be found', $id->toString()));
}
}
}