-
Notifications
You must be signed in to change notification settings - Fork 42
/
ClosureAggregateTranslator.php
118 lines (102 loc) · 3.46 KB
/
ClosureAggregateTranslator.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
<?php
/**
* This file is part of prooph/event-sourcing.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\EventSourcing\EventStoreIntegration;
use Iterator;
use Prooph\Common\Messaging\Message;
use Prooph\EventSourcing\Aggregate\AggregateTranslator as EventStoreAggregateTranslator;
use Prooph\EventSourcing\Aggregate\AggregateType;
use RuntimeException;
final class ClosureAggregateTranslator implements EventStoreAggregateTranslator
{
protected $aggregateIdExtractor;
protected $aggregateReconstructor;
protected $pendingEventsExtractor;
protected $replayStreamEvents;
protected $versionExtractor;
/**
* @param object $eventSourcedAggregateRoot
*
* @return int
*/
public function extractAggregateVersion($eventSourcedAggregateRoot): int
{
if (null === $this->versionExtractor) {
$this->versionExtractor = function (): int {
return $this->version;
};
}
return $this->versionExtractor->call($eventSourcedAggregateRoot);
}
/**
* @param object $anEventSourcedAggregateRoot
*
* @return string
*/
public function extractAggregateId($anEventSourcedAggregateRoot): string
{
if (null === $this->aggregateIdExtractor) {
$this->aggregateIdExtractor = function (): string {
return $this->aggregateId();
};
}
return $this->aggregateIdExtractor->call($anEventSourcedAggregateRoot);
}
/**
* @param AggregateType $aggregateType
* @param Iterator $historyEvents
*
* @return object reconstructed AggregateRoot
*/
public function reconstituteAggregateFromHistory(AggregateType $aggregateType, Iterator $historyEvents)
{
if (null === $this->aggregateReconstructor) {
$this->aggregateReconstructor = function ($historyEvents) {
return static::reconstituteFromHistory($historyEvents);
};
}
$arClass = $aggregateType->toString();
if (! \class_exists($arClass)) {
throw new RuntimeException(
\sprintf('Aggregate root class %s cannot be found', $arClass)
);
}
return ($this->aggregateReconstructor->bindTo(null, $arClass))($historyEvents);
}
/**
* @param object $anEventSourcedAggregateRoot
*
* @return Message[]
*/
public function extractPendingStreamEvents($anEventSourcedAggregateRoot): array
{
if (null === $this->pendingEventsExtractor) {
$this->pendingEventsExtractor = function (): array {
return $this->popRecordedEvents();
};
}
return $this->pendingEventsExtractor->call($anEventSourcedAggregateRoot);
}
/**
* @param object $anEventSourcedAggregateRoot
* @param Iterator $events
*
* @return void
*/
public function replayStreamEvents($anEventSourcedAggregateRoot, Iterator $events): void
{
if (null === $this->replayStreamEvents) {
$this->replayStreamEvents = function ($events): void {
$this->replay($events);
};
}
$this->replayStreamEvents->call($anEventSourcedAggregateRoot, $events);
}
}