|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/cqrs project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\EventSourcing\Aggregate; |
|
12
|
|
|
|
|
13
|
|
|
trait AggregateRootTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var AggregateIdInterface */ |
|
16
|
|
|
private $identifier; |
|
17
|
|
|
|
|
18
|
|
|
/** @var AggregateRevision */ |
|
19
|
|
|
private $revision; |
|
20
|
|
|
|
|
21
|
|
|
/** @var DomainEventSequence */ |
|
22
|
|
|
private $trackedEvents; |
|
23
|
|
|
|
|
24
|
|
|
public static function reconstituteFromHistory( |
|
25
|
|
|
AggregateIdInterface $aggregateId, |
|
26
|
|
|
DomainEventSequence $history |
|
27
|
|
|
): AggregateRootInterface { |
|
28
|
|
|
$aggRoot = new static($aggregateId); |
|
29
|
|
|
foreach ($history as $eventOccured) { |
|
30
|
|
|
$aggRoot = $aggRoot->reflectThat($eventOccured, false); |
|
31
|
|
|
} |
|
32
|
|
|
return $aggRoot; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function getIdentifier(): AggregateIdInterface |
|
36
|
|
|
{ |
|
37
|
1 |
|
return $this->identifier; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function getRevision(): AggregateRevision |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->revision; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function getTrackedEvents(): DomainEventSequence |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->trackedEvents; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function markClean(): AggregateRootInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$aggRoot = clone $this; |
|
53
|
|
|
$aggRoot->trackedEvents = DomainEventSequence::makeEmpty(); |
|
54
|
|
|
return $aggRoot; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
protected function __construct(AggregateIdInterface $aggregateId) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->identifier = $aggregateId; |
|
60
|
1 |
|
$this->revision = AggregateRevision::makeEmpty(); |
|
61
|
1 |
|
$this->trackedEvents = DomainEventSequence::makeEmpty(); |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
protected function reflectThat(DomainEventInterface $eventOccured, bool $track = true): self |
|
65
|
|
|
{ |
|
66
|
1 |
|
$aggRoot = clone $this; |
|
67
|
1 |
|
if ($track) { |
|
68
|
1 |
|
$aggRoot->revision = $aggRoot->revision->increment(); |
|
69
|
1 |
|
$eventOccured = $eventOccured->withAggregateRevision($aggRoot->revision); |
|
70
|
1 |
|
$aggRoot->trackedEvents = $aggRoot->trackedEvents->push($eventOccured); |
|
71
|
|
|
} else { |
|
72
|
|
|
$expectedAggregateRevision = $aggRoot->revision->increment(); |
|
73
|
|
|
if (!$expectedAggregateRevision->equals($eventOccured->getAggregateRevision())) { |
|
74
|
|
|
throw new \Exception(sprintf( |
|
75
|
|
|
"Given event-revision %s does not match expected AR revision at %s", |
|
76
|
|
|
$eventOccured->getAggregateRevision(), |
|
77
|
|
|
$expectedAggregateRevision |
|
78
|
|
|
)); |
|
79
|
|
|
} |
|
80
|
|
|
$aggRoot->revision = $expectedAggregateRevision; |
|
81
|
|
|
} |
|
82
|
1 |
|
$aggRoot->invokeEventHandler($eventOccured); |
|
83
|
1 |
|
return $aggRoot; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
private function invokeEventHandler(DomainEventInterface $event) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$handlerName = preg_replace("/Event$/", "", (new \ReflectionClass($event))->getShortName()); |
|
89
|
1 |
|
$handlerMethod = "when".ucfirst($handlerName); |
|
90
|
1 |
|
$handler = [ $this, $handlerMethod ]; |
|
91
|
1 |
|
if (!is_callable($handler)) { |
|
92
|
|
|
throw new \Exception("Handler '$handlerMethod' isn't callable on ".static::class); |
|
93
|
|
|
} |
|
94
|
1 |
|
call_user_func($handler, $event); |
|
95
|
1 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|