-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathEventSourcingAggregateRoot.php
More file actions
70 lines (60 loc) · 1.26 KB
/
EventSourcingAggregateRoot.php
File metadata and controls
70 lines (60 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Ddd\Domain\EventSourcing;
use Ddd\Domain\DomainEvent;
use Ddd\Domain\DomainEventPublisher;
/**
* Class EventSourcingAggregateRoot
*/
trait EventSourcingAggregateRoot
{
/**
* @var DomainEvent[]
*/
private $recordedEvents;
/**
* @param DomainEvent $event
*/
protected function publishThat(DomainEvent $event)
{
$this->apply($event);
$this->record($event);
$this->notify($event);
}
/**
* @param DomainEvent $event
*/
protected function notify(DomainEvent $event)
{
DomainEventPublisher::instance()->publish($event);
}
/**
* @param DomainEvent $event
*/
protected function record(DomainEvent $event)
{
$this->recordedEvents[] = $event;
}
/**
* @param DomainEvent $event
*/
protected function apply(DomainEvent $event)
{
$parts = explode('\\', get_class($event));
$apply = 'apply' . end($parts);
$this->$apply($event);
}
/**
* @return DomainEvent[]
*/
public function recordedEvents()
{
return $this->recordedEvents;
}
/**
* @return void
*/
public function clearRecordedEvents()
{
$this->recordedEvents = [];
}
}