-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEventSourcedDomainEntity.php
More file actions
120 lines (103 loc) · 2.47 KB
/
Copy pathEventSourcedDomainEntity.php
File metadata and controls
120 lines (103 loc) · 2.47 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace EricksonReyes\DomainDrivenDesign\Example\Domain;
use DateTimeImmutable;
use EricksonReyes\DomainDrivenDesign\Common\ValueObject\Identifier;
use EricksonReyes\DomainDrivenDesign\Domain\Entity;
use EricksonReyes\DomainDrivenDesign\Domain\Event;
use EricksonReyes\DomainDrivenDesign\EventSourcedEntity;
use Exception;
/**
* Class EventSourcedDomainEntity
* @package EricksonReyes\DomainDrivenDesign\Example\Domain
*/
class EventSourcedDomainEntity extends EventSourcedEntity
{
/**
* @var Identifier
*/
private $id;
/**
* @var string
*/
private $additionalData = '';
/**
* @var bool
*/
private $deleted = false;
/**
* @var DateTimeImmutable
*/
private $deletedOn;
/**
* EventSourcedDomainEntity constructor.
* @param Identifier $id
*/
public function __construct(Identifier $id)
{
$this->id = $id;
}
/**
* @return Identifier
*/
public function id(): Identifier
{
return $this->id;
}
/**
* @param Entity $anotherEntity
* @return bool
*/
public function matches(Entity $anotherEntity): bool
{
return $this->id()->matches($anotherEntity->id());
}
/**
* @param Entity $anotherEntity
* @return bool
*/
public function doesNotMatch(Entity $anotherEntity): bool
{
return !$this->matches($anotherEntity);
}
/**
* @return string
*/
public function additionalData(): string
{
return $this->additionalData;
}
/**
* @throws Exception
*/
public function delete(): void
{
$event = DomainEntityWasDeletedEvent::raise(
(string)$this->id(),
new DateTimeImmutable(),
$this->additionalData()
);
if ($this->eventNeverHappened(DomainEntityWasDeletedEvent::class) &&
$this->isTheFirstOccurrenceOfThis($event)
) {
$this->storeAndReplayThis($event);
}
if (!$this->isTheFirstOccurrenceOfThis($event)) {
return;
}
}
/**
* @param Event $event
*/
public function restoreFromEvent(Event $event): void
{
$this->replayThis($event);
}
/**
* @param DomainEntityWasDeletedEvent $event
*/
protected function replayDomainEntityWasDeleted(DomainEntityWasDeletedEvent $event): void
{
$this->deletedOn = $event->happenedOn();
$this->deleted = true;
}
}