1 | <?php |
||
21 | */ |
||
22 | final class LaravelEventStore implements EventStore |
||
23 | { |
||
24 | /** @var Serializer */ |
||
25 | private $serializer; |
||
26 | |||
27 | /** @var string */ |
||
28 | private $eventStoreTableName; |
||
29 | |||
30 | /** @var Connection */ |
||
31 | private $db; |
||
32 | |||
33 | /** |
||
34 | * @param DatabaseManager $databaseManager |
||
35 | * @param Serializer $serializer |
||
36 | * @param string $eventStoreConnectionName |
||
37 | * @param string $eventStoreTableName |
||
38 | */ |
||
39 | public function __construct( |
||
40 | DatabaseManager $databaseManager, |
||
41 | Serializer $serializer, |
||
42 | $eventStoreConnectionName, |
||
43 | $eventStoreTableName |
||
44 | ) { |
||
45 | $this->db = $databaseManager->connection($eventStoreConnectionName); |
||
46 | $this->serializer = $serializer; |
||
47 | $this->eventStoreTableName = $eventStoreTableName; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $id |
||
52 | * @return DomainEventStream |
||
53 | * @throws EventStreamNotFound |
||
54 | */ |
||
55 | public function load($id) |
||
56 | { |
||
57 | $rows = $this->db->table($this->eventStoreTableName) |
||
58 | ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on']) |
||
59 | ->where('uuid', $id) |
||
60 | ->orderBy('playhead', 'asc') |
||
61 | ->get(); |
||
62 | $events = []; |
||
63 | |||
64 | foreach ($rows as $row) { |
||
65 | $events[] = $this->deserializeEvent($row); |
||
66 | } |
||
67 | |||
68 | if (empty($events)) { |
||
69 | throw new EventStreamNotFound(sprintf('EventStream not found for aggregate with id %s', $id)); |
||
70 | } |
||
71 | |||
72 | return new \SmoothPhp\Domain\DomainEventStream($events); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param mixed $id |
||
77 | * @param DomainEventStream $eventStream |
||
78 | */ |
||
79 | public function append($id, DomainEventStream $eventStream) |
||
80 | { |
||
81 | $id = (string)$id; //Used to thrown errors if ID will not cast to string |
||
82 | |||
83 | $this->db->beginTransaction(); |
||
84 | |||
85 | try { |
||
86 | foreach ($eventStream as $domainMessage) { |
||
87 | $this->insertEvent($domainMessage); |
||
88 | } |
||
89 | |||
90 | $this->db->commit(); |
||
91 | } catch (QueryException $ex) { |
||
92 | $this->db->rollBack(); |
||
93 | |||
94 | throw $ex; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param DomainMessage $domainMessage |
||
100 | */ |
||
101 | private function insertEvent(DomainMessage $domainMessage) |
||
102 | { |
||
103 | try { |
||
104 | $this->db->table($this->eventStoreTableName) |
||
105 | ->insert( |
||
106 | [ |
||
107 | 'uuid' => (string)$domainMessage->getId(), |
||
108 | 'playhead' => $domainMessage->getPlayHead(), |
||
109 | 'metadata' => json_encode($this->serializer->serialize($domainMessage->getMetadata())), |
||
110 | 'payload' => json_encode($this->serializer->serialize($domainMessage->getPayload())), |
||
111 | 'recorded_on' => (string)$domainMessage->getRecordedOn(), |
||
112 | 'type' => $domainMessage->getType(), |
||
113 | ] |
||
114 | ); |
||
115 | } catch (\PDOException $ex) { |
||
116 | if ($ex->getCode() == 23000) { |
||
117 | throw new DuplicateAggregatePlayhead((string)$domainMessage->getId(), $domainMessage->getPlayHead()); |
||
118 | } |
||
119 | throw $ex; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param \stdClass |
||
125 | * @return DomainMessage |
||
126 | */ |
||
127 | private function deserializeEvent($row) |
||
128 | { |
||
129 | return new \SmoothPhp\Domain\DomainMessage( |
||
130 | $row->uuid, |
||
131 | $row->playhead, |
||
132 | $this->serializer->deserialize(json_decode($row->metadata, true)), |
||
133 | $this->serializer->deserialize(json_decode($row->payload, true)), |
||
134 | new DateTime($row->recorded_on) |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param string[] $eventTypes |
||
140 | * @return int |
||
141 | */ |
||
142 | public function getEventCountByTypes($eventTypes) |
||
143 | { |
||
144 | return $this->db->table($this->eventStoreTableName) |
||
145 | ->whereIn('type', $eventTypes) |
||
146 | ->count(); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param string[] $eventTypes |
||
151 | * @param int $skip |
||
152 | * @param int $take |
||
153 | * @return DomainEventStream |
||
154 | */ |
||
155 | public function getEventsByType($eventTypes, $skip, $take) |
||
156 | { |
||
157 | $rows = $this->db->table($this->eventStoreTableName) |
||
158 | ->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on']) |
||
159 | ->whereIn('type', $eventTypes) |
||
160 | ->skip($skip) |
||
161 | ->take($take) |
||
162 | ->orderBy('recorded_on', 'asc') |
||
163 | ->get(); |
||
164 | $events = []; |
||
165 | |||
166 | foreach ($rows as $row) { |
||
167 | $events[] = $this->deserializeEvent($row); |
||
168 | } |
||
169 | |||
170 | return new \SmoothPhp\Domain\DomainEventStream($events); |
||
171 | } |
||
172 | } |