1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EventSauce\EventSourcing\Snapshotting; |
6
|
|
|
|
7
|
|
|
use EventSauce\EventSourcing\AggregateRootId; |
8
|
|
|
use EventSauce\EventSourcing\AggregateRootRepository; |
9
|
|
|
use EventSauce\EventSourcing\Message; |
10
|
|
|
use EventSauce\EventSourcing\MessageRepository; |
11
|
|
|
use Generator; |
12
|
|
|
|
13
|
|
|
final class ConstructingAggregateRootRepositoryWithSnapshotting implements AggregateRootRepositoryWithSnapshotting |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $aggregateRootClassName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var MessageRepository |
22
|
|
|
*/ |
23
|
|
|
private $messageRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SnapshotRepository |
27
|
|
|
*/ |
28
|
|
|
private $snapshotRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var AggregateRootRepository |
32
|
|
|
*/ |
33
|
|
|
private $regularRepository; |
34
|
|
|
|
35
|
1 |
|
public function __construct( |
36
|
|
|
string $aggregateRootClassName, |
37
|
|
|
MessageRepository $messageRepository, |
38
|
|
|
SnapshotRepository $snapshotRepository, |
39
|
|
|
AggregateRootRepository $regularRepository |
40
|
|
|
) { |
41
|
1 |
|
$this->aggregateRootClassName = $aggregateRootClassName; |
42
|
1 |
|
$this->messageRepository = $messageRepository; |
43
|
1 |
|
$this->snapshotRepository = $snapshotRepository; |
44
|
1 |
|
$this->regularRepository = $regularRepository; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
1 |
|
public function retrieveFromSnapshot(AggregateRootId $aggregateRootId): object |
48
|
|
|
{ |
49
|
1 |
|
$snapshot = $this->snapshotRepository->retrieve($aggregateRootId); |
50
|
|
|
|
51
|
1 |
|
if ( ! $snapshot instanceof Snapshot) { |
52
|
1 |
|
return $this->retrieve($aggregateRootId); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @var AggregateRootWithSnapshotting $className */ |
56
|
1 |
|
$className = $this->aggregateRootClassName; |
57
|
1 |
|
$events = $this->retrieveAllEventsAfterVersion($aggregateRootId, $snapshot->aggregateRootVersion()); |
58
|
|
|
|
59
|
1 |
|
return $className::reconstituteFromSnapshotAndEvents($snapshot, $events); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function storeSnapshot(AggregateRootWithSnapshotting $aggregateRoot): void |
63
|
|
|
{ |
64
|
1 |
|
$snapshot = $aggregateRoot->createSnapshot(); |
65
|
1 |
|
$this->snapshotRepository->persist($snapshot); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
1 |
|
private function retrieveAllEventsAfterVersion(AggregateRootId $aggregateRootId, int $version) |
69
|
|
|
{ |
70
|
|
|
/** @var Message[]|Generator $messages */ |
71
|
1 |
|
$messages = $this->messageRepository->retrieveAllAfterVersion($aggregateRootId, $version); |
72
|
|
|
|
73
|
1 |
|
foreach ($messages as $message) { |
74
|
1 |
|
yield $message->event(); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return $messages->getReturn(); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function retrieve(AggregateRootId $aggregateRootId): object |
81
|
|
|
{ |
82
|
1 |
|
return $this->regularRepository->retrieve($aggregateRootId); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function persist(object $aggregateRoot): void |
86
|
|
|
{ |
87
|
1 |
|
$this->regularRepository->persist($aggregateRoot); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRootVersion, object ...$events): void |
91
|
|
|
{ |
92
|
1 |
|
$this->regularRepository->persistEvents($aggregateRootId, $aggregateRootVersion, ...$events); |
93
|
1 |
|
} |
94
|
|
|
} |
95
|
|
|
|