|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\EventSourcing; |
|
4
|
|
|
|
|
5
|
|
|
use Broadway\Domain\DomainEventStream; |
|
6
|
|
|
use Broadway\Domain\DomainEventStreamInterface; |
|
7
|
|
|
use Broadway\Domain\DomainMessage; |
|
8
|
|
|
|
|
9
|
|
|
class CopyAwareEventStoreDecorator extends AbstractEventStoreDecorator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @inheritdoc |
|
13
|
|
|
*/ |
|
14
|
|
|
public function load($id) |
|
15
|
|
|
{ |
|
16
|
|
|
return $this->loadCompleteStream(parent::load($id)); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param DomainEventStreamInterface $eventStream |
|
21
|
|
|
* @return DomainEventStreamInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private function loadCompleteStream(DomainEventStreamInterface $eventStream) |
|
24
|
|
|
{ |
|
25
|
|
|
$events = iterator_to_array($eventStream); |
|
26
|
|
|
/** @var DomainMessage $oldestMessage */ |
|
27
|
|
|
$oldestMessage = current($events); |
|
28
|
|
|
if (intval($oldestMessage->getPlayhead()) === 0) { |
|
29
|
|
|
return $eventStream; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$parentId = $this->identifyParent($oldestMessage); |
|
33
|
|
|
$parentEventStream = parent::load($parentId); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$inheritedEvents = $this->limitEventStreamToPlayhead($parentEventStream, $oldestMessage->getPlayhead()); |
|
36
|
|
|
$combinedEvents = array_merge($inheritedEvents, $events); |
|
37
|
|
|
|
|
38
|
|
|
return $this->loadCompleteStream(new DomainEventStream($combinedEvents)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param DomainMessage $message |
|
43
|
|
|
* @return string |
|
44
|
|
|
* |
|
45
|
|
|
* @throws UnknownParentAggregateException |
|
46
|
|
|
*/ |
|
47
|
|
|
private function identifyParent(DomainMessage $message) |
|
48
|
|
|
{ |
|
49
|
|
|
/** @var AggregateCopiedEventInterface $domainEvent */ |
|
50
|
|
|
$domainEvent = $message->getPayload(); |
|
51
|
|
|
|
|
52
|
|
|
if (!$domainEvent instanceof AggregateCopiedEventInterface) { |
|
53
|
|
|
throw new UnknownParentAggregateException(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $domainEvent->getParentAggregateId(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param DomainEventStreamInterface $eventStream |
|
61
|
|
|
* @param int $playhead |
|
62
|
|
|
* |
|
63
|
|
|
* @return DomainMessage[] |
|
64
|
|
|
*/ |
|
65
|
|
|
private function limitEventStreamToPlayhead(DomainEventStreamInterface $eventStream, $playhead) |
|
66
|
|
|
{ |
|
67
|
|
|
return array_filter( |
|
68
|
|
|
iterator_to_array($eventStream), |
|
69
|
|
|
function (DomainMessage $message) use ($playhead) { |
|
70
|
|
|
return intval($message->getPlayhead()) < $playhead; |
|
71
|
|
|
} |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.