1 | <?php |
||
21 | final class LaravelEventStore implements EventStore |
||
22 | { |
||
23 | /** @var Serializer */ |
||
24 | private $serializer; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $eventStoreTableName; |
||
28 | |||
29 | /** @var Connection */ |
||
30 | private $db; |
||
31 | |||
32 | /** |
||
33 | * @param DatabaseManager $databaseManager |
||
34 | * @param Serializer $serializer |
||
35 | * @param string $eventStoreConnectionName |
||
36 | * @param string $eventStoreTableName |
||
37 | */ |
||
38 | public function __construct( |
||
48 | |||
49 | /** |
||
50 | * @param string $id |
||
51 | * @return DomainEventStream |
||
52 | * @throws EventStreamNotFound |
||
53 | */ |
||
54 | public function load($id) |
||
73 | |||
74 | /** |
||
75 | * @param mixed $id |
||
76 | * @param DomainEventStream $eventStream |
||
77 | * @param bool $ignorePlayhead |
||
78 | * @throws \PDOException |
||
79 | * @throws \SmoothPhp\EventStore\DuplicateAggregatePlayhead |
||
80 | * @throws \Illuminate\Database\QueryException |
||
81 | */ |
||
82 | public function append($id, DomainEventStream $eventStream, bool $ignorePlayhead = false) |
||
83 | { |
||
84 | $id = (string)$id; //Used to thrown errors if ID will not cast to string |
||
|
|||
85 | |||
86 | $this->db->beginTransaction(); |
||
87 | |||
88 | try { |
||
89 | foreach ($eventStream as $domainMessage) { |
||
90 | $this->insertEvent($this->domainMessageToArray($domainMessage), $ignorePlayhead); |
||
91 | } |
||
92 | |||
93 | $this->db->commit(); |
||
94 | } catch (QueryException $ex) { |
||
95 | $this->db->rollBack(); |
||
96 | |||
97 | throw $ex; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param array $eventRow |
||
103 | * @param bool $ignorePlayhead |
||
104 | * @throws DuplicateAggregatePlayhead |
||
105 | */ |
||
106 | private function insertEvent(array $eventRow, bool $ignorePlayhead) |
||
107 | { |
||
108 | try { |
||
109 | $this->db->table($this->eventStoreTableName)->insert($eventRow); |
||
110 | } catch (\PDOException $ex) { |
||
111 | if ((string) $ex->getCode() === '23000') { |
||
112 | if ($ignorePlayhead) { |
||
113 | $eventRow['playhead'] ++; |
||
114 | return $this->insertEvent($eventRow, true); |
||
115 | } |
||
116 | throw new DuplicateAggregatePlayhead($eventRow['uuid'], $eventRow['playhead']); |
||
117 | } |
||
118 | throw $ex; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param \stdClass |
||
124 | * @return DomainMessage |
||
125 | */ |
||
126 | private function deserializeEvent($row) |
||
136 | |||
137 | /** |
||
138 | * @param string[] $eventTypes |
||
139 | * @return int |
||
140 | */ |
||
141 | public function getEventCountByTypes($eventTypes) |
||
147 | |||
148 | /** |
||
149 | * @param string[] $eventTypes |
||
150 | * @param int $skip |
||
151 | * @param int $take |
||
152 | * @return DomainEventStream |
||
153 | */ |
||
154 | public function getEventsByType($eventTypes, $skip, $take) |
||
171 | |||
172 | /** |
||
173 | * @param DomainMessage $domainMessage |
||
174 | * @return array |
||
175 | */ |
||
176 | private function domainMessageToArray(DomainMessage $domainMessage): array |
||
187 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.