forked from broadway/broadway
/
DBALEventStore.php
166 lines (137 loc) · 4.78 KB
/
DBALEventStore.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
160
161
162
163
164
165
166
<?php
/*
* This file is part of the broadway/broadway package.
*
* (c) Qandidate.com <opensource@qandidate.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Broadway\EventStore;
use Broadway\Domain\DateTime;
use Broadway\Domain\DomainEventStream;
use Broadway\Domain\DomainEventStreamInterface;
use Broadway\Domain\DomainMessage;
use Broadway\Serializer\SerializerInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema;
/**
* Event store using a relational database as storage.
*
* The implementation uses doctrine DBAL for the communication with the
* underlying data store.
*/
class DBALEventStore implements EventStoreInterface
{
private $connection;
private $payloadSerializer;
private $metadataSerializer;
private $loadStatement = null;
private $tableName;
/**
* @param string $tableName
*/
public function __construct(
Connection $connection,
SerializerInterface $payloadSerializer,
SerializerInterface $metadataSerializer,
$tableName
) {
$this->connection = $connection;
$this->payloadSerializer = $payloadSerializer;
$this->metadataSerializer = $metadataSerializer;
$this->tableName = $tableName;
}
/**
* {@inheritDoc}
*/
public function load($id)
{
$statement = $this->prepareLoadStatement();
$statement->bindValue('uuid', $id, 'guid');
$statement->execute();
$events = array();
while ($row = $statement->fetch()) {
$events[] = $this->deserializeEvent($row);
}
if (empty($events)) {
throw new EventStreamNotFoundException(sprintf('EventStream not found for aggregate with id %s', $id));
}
return new DomainEventStream($events);
}
/**
* {@inheritDoc}
*/
public function append($id, DomainEventStreamInterface $eventStream)
{
$this->connection->beginTransaction();
try {
foreach ($eventStream as $domainMessage) {
$this->insertMessage($this->connection, $domainMessage);
}
$this->connection->commit();
} catch (DBALException $exception) {
$this->connection->rollback();
throw DBALEventStoreException::create($exception);
}
}
private function insertMessage(Connection $connection, DomainMessage $domainMessage)
{
$data = array(
'uuid' => $domainMessage->getId(),
'playhead' => $domainMessage->getPlayhead(),
'metadata' => json_encode($this->metadataSerializer->serialize($domainMessage->getMetadata())),
'payload' => json_encode($this->payloadSerializer->serialize($domainMessage->getPayload())),
'recordedOn' => $domainMessage->getRecordedOn()->toString(),
'type' => $domainMessage->getType(),
);
$connection->insert($this->tableName, $data);
}
/**
* @return Doctrine\DBAL\Schema\Table|null
*/
public function configureSchema(Schema $schema)
{
if ($schema->hasTable($this->tableName)) {
return null;
}
return $this->configureTable();
}
public function configureTable()
{
$schema = new Schema();
$table = $schema->createTable($this->tableName);
$table->addColumn('id', 'integer', array('autoincrement' => true));
$table->addColumn('uuid', 'guid', array('length' => 36));
$table->addColumn('playhead', 'integer', array('unsigned' => true));
$table->addColumn('payload', 'text');
$table->addColumn('metadata', 'text');
$table->addColumn('recordedOn', 'string', array('length' => 32));
$table->addColumn('type', 'text');
$table->setPrimaryKey(array('id'));
$table->addUniqueIndex(array('uuid', 'playhead'));
return $table;
}
private function prepareLoadStatement()
{
if (null === $this->loadStatement) {
$query = 'SELECT uuid, playhead, metadata, payload, recordedOn
FROM ' . $this->tableName . '
WHERE uuid = :uuid
ORDER BY playhead ASC';
$this->loadStatement = $this->connection->prepare($query);
}
return $this->loadStatement;
}
private function deserializeEvent($row)
{
return new DomainMessage(
$row['uuid'],
$row['playhead'],
$this->metadataSerializer->deserialize(json_decode($row['metadata'], true)),
$this->payloadSerializer->deserialize(json_decode($row['payload'], true)),
DateTime::fromString($row['recordedOn'])
);
}
}