|
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
|
|
|
use Gica\Cqrs\EventStore\EventStream; |
|
14
|
|
|
|
|
15
|
|
|
class AggregateRepository |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EventStore |
|
19
|
|
|
*/ |
|
20
|
|
|
private $eventStore; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EventsApplierOnAggregate |
|
24
|
|
|
*/ |
|
25
|
|
|
private $eventsApplierOnAggregate; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var EventStream[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $aggregateToEventStreamMap; |
|
31
|
|
|
|
|
32
|
6 |
|
public function __construct( |
|
33
|
|
|
EventStore $eventStore, |
|
34
|
|
|
EventsApplierOnAggregate $eventsApplierOnAggregate |
|
35
|
|
|
) |
|
36
|
|
|
{ |
|
37
|
6 |
|
$this->eventStore = $eventStore; |
|
38
|
6 |
|
$this->eventsApplierOnAggregate = $eventsApplierOnAggregate; |
|
39
|
6 |
|
$this->aggregateToEventStreamMap = new \SplObjectStorage(); |
|
|
|
|
|
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
public function loadAggregate(string $aggregateClass, $aggregateId) |
|
43
|
|
|
{ |
|
44
|
5 |
|
$aggregate = new $aggregateClass; |
|
45
|
|
|
|
|
46
|
5 |
|
$priorEvents = $this->eventStore->loadEventsForAggregate($aggregateClass, $aggregateId); |
|
47
|
|
|
|
|
48
|
5 |
|
$this->aggregateToEventStreamMap[$aggregate] = $priorEvents; |
|
49
|
|
|
|
|
50
|
|
|
/** @var EventWithMetaData[] $priorEvents */ |
|
51
|
5 |
|
$this->eventsApplierOnAggregate->applyEventsOnAggregate($aggregate, $priorEvents); |
|
52
|
|
|
|
|
53
|
5 |
|
return $aggregate; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function saveAggregate($aggregateId, $aggregate, $newEventsWithMetaData) |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var AggregateEventStream $priorEvents */ |
|
62
|
3 |
|
$priorEvents = $this->aggregateToEventStreamMap[$aggregate]; |
|
63
|
|
|
|
|
64
|
3 |
|
$this->eventStore->appendEventsForAggregate( |
|
65
|
3 |
|
$aggregateId, get_class($aggregate), $newEventsWithMetaData, $priorEvents->getVersion(), $priorEvents->getSequence()); |
|
66
|
|
|
} |
|
67
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..