1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\LaravelAdapter\EventStore; |
3
|
|
|
|
4
|
|
|
use Illuminate\Database\Connection; |
5
|
|
|
use Illuminate\Database\DatabaseManager; |
6
|
|
|
use Illuminate\Database\QueryException; |
7
|
|
|
use SmoothPhp\Contracts\Domain\DomainEventStream; |
8
|
|
|
use SmoothPhp\Contracts\Domain\DomainMessage; |
9
|
|
|
use SmoothPhp\Contracts\EventStore\DomainEventStreamInterface; |
10
|
|
|
use SmoothPhp\Contracts\EventStore\EventStore; |
11
|
|
|
use SmoothPhp\Contracts\EventStore\EventStreamNotFound; |
12
|
|
|
use SmoothPhp\Contracts\Serialization\Serializer; |
13
|
|
|
use SmoothPhp\Domain\DateTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class LaravelEventStore |
17
|
|
|
* @package SmoothPhp\LaravelAdapter\EventStore |
18
|
|
|
* @author Simon Bennett <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class LaravelEventStore implements EventStore |
21
|
|
|
{ |
22
|
|
|
/** @var Serializer */ |
23
|
|
|
private $serializer; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $eventStoreTableName; |
27
|
|
|
|
28
|
|
|
/** @var Connection */ |
29
|
|
|
private $db; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param DatabaseManager $databaseManager |
33
|
|
|
* @param Serializer $serializer |
34
|
|
|
* @param string $eventStoreConnectionName |
35
|
|
|
* @param string $eventStoreTableName |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
DatabaseManager $databaseManager, |
39
|
|
|
Serializer $serializer, |
40
|
|
|
$eventStoreConnectionName, |
41
|
|
|
$eventStoreTableName |
42
|
|
|
) { |
43
|
|
|
$this->db = $databaseManager->connection($eventStoreConnectionName); |
44
|
|
|
$this->serializer = $serializer; |
45
|
|
|
$this->eventStoreTableName = $eventStoreTableName; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $id |
50
|
|
|
* @return DomainEventStream |
51
|
|
|
* @throws EventStreamNotFound |
52
|
|
|
*/ |
53
|
|
|
public function load($id) |
54
|
|
|
{ |
55
|
|
|
$rows = $this->db->table($this->eventStoreTableName) |
56
|
|
|
->select(['uuid', 'playhead', 'metadata', 'payload', 'recorded_on']) |
57
|
|
|
->where('uuid', $id) |
58
|
|
|
->orderBy('playhead', 'asc') |
59
|
|
|
->get(); |
60
|
|
|
$events = []; |
61
|
|
|
|
62
|
|
|
foreach ($rows as $row) { |
63
|
|
|
$events[] = $this->deserializeEvent($row); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (empty($events)) { |
67
|
|
|
throw new EventStreamNotFound(sprintf('EventStream not found for aggregate with id %s', $id)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return new \SmoothPhp\Domain\DomainEventStream($events); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $id |
75
|
|
|
* @param DomainEventStream $eventStream |
76
|
|
|
*/ |
77
|
|
|
public function append($id, DomainEventStream $eventStream) |
78
|
|
|
{ |
79
|
|
|
$id = (string)$id; //Used to thrown errors if ID will not cast to string |
|
|
|
|
80
|
|
|
|
81
|
|
|
$this->db->beginTransaction(); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
foreach ($eventStream as $domainMessage) { |
85
|
|
|
$this->insertEvent( $domainMessage); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->db->commit(); |
89
|
|
|
} catch (QueryException $ex) { |
90
|
|
|
$this->db->rollBack(); |
91
|
|
|
|
92
|
|
|
throw $ex; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param DomainMessage $domainMessage |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
private function insertEvent(DomainMessage $domainMessage) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->db->table($this->eventStoreTableName) |
102
|
|
|
->insert( |
103
|
|
|
[ |
104
|
|
|
'uuid' => (string)$domainMessage->getId(), |
105
|
|
|
'playhead' => $domainMessage->getPlayHead(), |
106
|
|
|
'metadata' => json_encode($this->serializer->serialize($domainMessage->getMetadata())), |
107
|
|
|
'payload' => json_encode($this->serializer->serialize($domainMessage->getPayload())), |
108
|
|
|
'recorded_on' => (string)$domainMessage->getRecordedOn(), |
109
|
|
|
'type' => $domainMessage->getType(), |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param \stdClass |
116
|
|
|
* @return DomainMessage |
117
|
|
|
*/ |
118
|
|
|
private function deserializeEvent($row) |
119
|
|
|
{ |
120
|
|
|
return new \SmoothPhp\Domain\DomainMessage( |
121
|
|
|
$row->uuid, |
122
|
|
|
$row->playhead, |
123
|
|
|
$this->serializer->deserialize(json_decode($row->metadata, true)), |
124
|
|
|
$this->serializer->deserialize(json_decode($row->payload, true)), |
125
|
|
|
new DateTime($row->recorded_on) |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
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.