|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Place; |
|
4
|
|
|
|
|
5
|
|
|
use Assert\Assertion as Assert; |
|
6
|
|
|
use Broadway\Domain\AggregateRoot; |
|
7
|
|
|
use Broadway\Domain\DomainEventStream; |
|
8
|
|
|
use Broadway\Domain\DomainEventStreamInterface; |
|
9
|
|
|
use Broadway\EventHandling\EventBusInterface; |
|
10
|
|
|
use Broadway\EventSourcing\AggregateFactory\PublicConstructorAggregateFactory; |
|
11
|
|
|
use Broadway\EventSourcing\EventSourcingRepository; |
|
12
|
|
|
use Broadway\EventSourcing\EventStreamDecoratorInterface; |
|
13
|
|
|
use Broadway\EventStore\EventStoreInterface; |
|
14
|
|
|
|
|
15
|
|
|
class PlaceRepository extends EventSourcingRepository |
|
16
|
|
|
{ |
|
17
|
|
|
private const AGGREGATE_CLASS = Place::class; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var EventStoreInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $protectedEventStore; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EventBusInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $protectedEventBus; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array|EventStreamDecoratorInterface[] |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $protectedEventStreamDecorators; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param EventStoreInterface $eventStore |
|
36
|
|
|
* @param EventBusInterface $eventBus |
|
37
|
|
|
* @param EventStreamDecoratorInterface[] $eventStreamDecorators |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
EventStoreInterface $eventStore, |
|
41
|
|
|
EventBusInterface $eventBus, |
|
42
|
|
|
array $eventStreamDecorators = array() |
|
43
|
|
|
) { |
|
44
|
|
|
parent::__construct( |
|
45
|
|
|
$eventStore, |
|
46
|
|
|
$eventBus, |
|
47
|
|
|
self::AGGREGATE_CLASS, |
|
48
|
|
|
new PublicConstructorAggregateFactory(), |
|
49
|
|
|
$eventStreamDecorators |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$this->protectedEventStore = $eventStore; |
|
53
|
|
|
$this->protectedEventBus = $eventBus; |
|
54
|
|
|
$this->protectedEventStreamDecorators = $eventStreamDecorators; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function saveMultiple(AggregateRoot ...$aggregates): void |
|
58
|
|
|
{ |
|
59
|
|
|
if (empty($aggregates)) { |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$firstId = null; |
|
64
|
|
|
$domainEvents = []; |
|
65
|
|
|
foreach ($aggregates as $aggregate) { |
|
66
|
|
|
Assert::isInstanceOf($aggregate, self::AGGREGATE_CLASS); |
|
67
|
|
|
|
|
68
|
|
|
if (!$firstId) { |
|
|
|
|
|
|
69
|
|
|
// We need to pass an aggregate id to the EventStore::append() method, but it's not actually used for |
|
70
|
|
|
// anything except for a check that it's a string. So we just pass the one of the first aggregate. |
|
71
|
|
|
$firstId = $aggregate->getAggregateRootId(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$eventStream = $this->protectedDecorateForWrite($aggregate, $aggregate->getUncommittedEvents()); |
|
75
|
|
|
|
|
76
|
|
|
$domainEvents = array_merge( |
|
77
|
|
|
$domainEvents, |
|
78
|
|
|
iterator_to_array($eventStream->getIterator()) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$eventStream = new DomainEventStream($domainEvents); |
|
83
|
|
|
|
|
84
|
|
|
$this->protectedEventStore->append($firstId, $eventStream); |
|
85
|
|
|
$this->protectedEventBus->publish($eventStream); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function protectedDecorateForWrite( |
|
89
|
|
|
AggregateRoot $aggregate, |
|
90
|
|
|
DomainEventStreamInterface $eventStream |
|
91
|
|
|
): DomainEventStreamInterface { |
|
92
|
|
|
$aggregateIdentifier = $aggregate->getAggregateRootId(); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($this->protectedEventStreamDecorators as $eventStreamDecorator) { |
|
95
|
|
|
$eventStream = $eventStreamDecorator->decorateForWrite( |
|
96
|
|
|
self::AGGREGATE_CLASS, |
|
97
|
|
|
$aggregateIdentifier, |
|
98
|
|
|
$eventStream |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $eventStream; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: