1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\EventSourcing; |
3
|
|
|
|
4
|
|
|
use SmoothPhp\Contracts\EventSourcing\AggregateRoot as AggregateRootInterface; |
5
|
|
|
use SmoothPhp\Contracts\EventSourcing\Entity as EntityInterface; |
6
|
|
|
use SmoothPhp\Contracts\EventSourcing\Event; |
7
|
|
|
use SmoothPhp\Domain\DomainEventStream; |
8
|
|
|
use SmoothPhp\Domain\DomainMessage; |
9
|
|
|
use SmoothPhp\Domain\Metadata; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AggregateRoot |
13
|
|
|
* @author Simon Bennett <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AggregateRoot implements AggregateRootInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var [] |
19
|
|
|
*/ |
20
|
|
|
private $uncommittedEvents = []; |
21
|
|
|
|
22
|
|
|
private $playHead = -1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Event $event |
26
|
|
|
*/ |
27
|
18 |
|
public function apply(Event $event) |
28
|
|
|
{ |
29
|
18 |
|
$this->handleRecursively($event); |
30
|
|
|
|
31
|
18 |
|
$this->playHead++; |
32
|
18 |
|
$this->uncommittedEvents[] = DomainMessage::recordNow( |
33
|
18 |
|
$this->getAggregateRootId(), |
34
|
18 |
|
$this->playHead, |
35
|
18 |
|
new Metadata(array()), |
36
|
|
|
$event |
37
|
|
|
); |
38
|
18 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Handles event if capable. |
43
|
|
|
* |
44
|
|
|
* @param $event |
45
|
|
|
*/ |
46
|
24 |
View Code Duplication |
protected function handle(Event $event) |
47
|
|
|
{ |
48
|
24 |
|
$method = $this->getApplyMethod($event); |
49
|
24 |
|
if (!method_exists($this, $method)) { |
50
|
18 |
|
return; |
51
|
|
|
} |
52
|
6 |
|
$this->$method($event); |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Event $event |
57
|
|
|
*/ |
58
|
24 |
|
protected function handleRecursively(Event $event) |
59
|
|
|
{ |
60
|
24 |
|
$this->handle($event); |
61
|
|
|
|
62
|
24 |
|
foreach ($this->getChildren() as $child) { |
63
|
6 |
|
$child->registerAggregateRoot($this); |
64
|
6 |
|
$child->handleRecursively($event); |
65
|
|
|
} |
66
|
24 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Event $event |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
24 |
|
private function getApplyMethod(Event $event) |
73
|
|
|
{ |
74
|
24 |
|
$classParts = explode('\\', get_class($event)); |
75
|
|
|
|
76
|
24 |
|
return 'apply' . end($classParts); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return DomainEventStream |
81
|
|
|
*/ |
82
|
15 |
|
public function getUncommittedEvents() |
83
|
|
|
{ |
84
|
15 |
|
$stream = new DomainEventStream($this->uncommittedEvents); |
85
|
|
|
|
86
|
15 |
|
$this->uncommittedEvents = []; |
87
|
|
|
|
88
|
15 |
|
return $stream; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param DomainEventStream $stream |
93
|
|
|
*/ |
94
|
6 |
|
public function initializeState(DomainEventStream $stream) |
95
|
|
|
{ |
96
|
6 |
|
foreach ($stream as $message) { |
97
|
6 |
|
$this->playHead++; |
98
|
6 |
|
$this->handleRecursively($message->getPayload()); |
99
|
|
|
} |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return EntityInterface[] $entity |
104
|
|
|
*/ |
105
|
15 |
|
public function getChildren() |
106
|
|
|
{ |
107
|
15 |
|
return []; |
108
|
|
|
} |
109
|
|
|
} |