|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Copyright (c) 2016 Constantin Galbenu <[email protected]> * |
|
4
|
|
|
******************************************************************************/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Gica\Cqrs\Aggregate; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
use Gica\Cqrs\Event\EventsApplier\EventsApplierOnAggregate; |
|
10
|
|
|
use Gica\Cqrs\Event\EventWithMetaData; |
|
11
|
|
|
use Gica\Cqrs\EventStore; |
|
12
|
|
|
use Gica\Cqrs\EventStore\AggregateEventStream; |
|
13
|
|
|
|
|
14
|
|
|
class AggregateRepository |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EventStore |
|
18
|
|
|
*/ |
|
19
|
|
|
private $eventStore; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EventsApplierOnAggregate |
|
23
|
|
|
*/ |
|
24
|
|
|
private $eventsApplier; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var \SplObjectStorage |
|
28
|
|
|
*/ |
|
29
|
|
|
private $aggregateToEventStreamMap; |
|
30
|
|
|
|
|
31
|
10 |
|
public function __construct( |
|
32
|
|
|
EventStore $eventStore, |
|
33
|
|
|
EventsApplierOnAggregate $eventsApplier |
|
34
|
|
|
) |
|
35
|
|
|
{ |
|
36
|
10 |
|
$this->eventStore = $eventStore; |
|
37
|
10 |
|
$this->eventsApplier = $eventsApplier; |
|
38
|
10 |
|
$this->aggregateToEventStreamMap = new \SplObjectStorage(); |
|
39
|
10 |
|
} |
|
40
|
|
|
|
|
41
|
10 |
|
public function loadAggregate(string $aggregateClass, $aggregateId) |
|
42
|
|
|
{ |
|
43
|
10 |
|
$aggregate = new $aggregateClass; |
|
44
|
|
|
|
|
45
|
10 |
|
$priorEvents = $this->eventStore->loadEventsForAggregate($aggregateClass, $aggregateId); |
|
46
|
|
|
|
|
47
|
10 |
|
$this->aggregateToEventStreamMap[$aggregate] = $priorEvents; |
|
48
|
|
|
|
|
49
|
|
|
/** @var EventWithMetaData[] $priorEvents */ |
|
50
|
10 |
|
$this->eventsApplier->applyEventsOnAggregate($aggregate, $priorEvents); |
|
51
|
|
|
|
|
52
|
10 |
|
return $aggregate; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $aggregateId |
|
57
|
|
|
* @param $aggregate |
|
58
|
|
|
* @param EventWithMetaData[] $newEventsWithMeta |
|
59
|
|
|
* @return EventWithMetaData[] decorated events with sequence and index |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function saveAggregate($aggregateId, $aggregate, $newEventsWithMeta) |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var AggregateEventStream $priorEvents */ |
|
64
|
4 |
|
$priorEvents = $this->aggregateToEventStreamMap[$aggregate]; |
|
65
|
|
|
|
|
66
|
4 |
|
$this->eventStore->appendEventsForAggregate( |
|
67
|
4 |
|
$aggregateId, get_class($aggregate), $newEventsWithMeta, $priorEvents->getVersion(), $priorEvents->getSequence()); |
|
68
|
|
|
|
|
69
|
4 |
|
$decoratedEvents = []; |
|
70
|
|
|
|
|
71
|
4 |
|
foreach ($newEventsWithMeta as $index => $eventWithMetaData) { |
|
72
|
4 |
|
$decoratedEvents[] = $eventWithMetaData->withSequenceAndIndex($priorEvents->getSequence(), $index); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
return $decoratedEvents; |
|
76
|
|
|
} |
|
77
|
|
|
} |