|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace PersonalGalaxy\Calendar\Entity; |
|
5
|
|
|
|
|
6
|
|
|
use PersonalGalaxy\Calendar\{ |
|
7
|
|
|
Entity\Event\Identity, |
|
8
|
|
|
Entity\Event\Name, |
|
9
|
|
|
Entity\Event\Slot, |
|
10
|
|
|
Entity\Event\Note, |
|
11
|
|
|
Entity\Agenda\Identity as Agenda, |
|
|
|
|
|
|
12
|
|
|
Event\EventWasAdded, |
|
13
|
|
|
Event\EventWasRenamed, |
|
14
|
|
|
Event\EventWasMoved, |
|
15
|
|
|
Event\EventWasCanceled, |
|
16
|
|
|
Event\Event\NoteWasAdded, |
|
17
|
|
|
}; |
|
18
|
|
|
use Innmind\EventBus\{ |
|
19
|
|
|
ContainsRecordedEventsInterface, |
|
20
|
|
|
EventRecorder, |
|
21
|
|
|
}; |
|
22
|
|
|
|
|
23
|
|
|
final class Event implements ContainsRecordedEventsInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use EventRecorder; |
|
26
|
|
|
|
|
27
|
|
|
private $identity; |
|
28
|
|
|
private $agenda; |
|
29
|
|
|
private $name; |
|
30
|
|
|
private $slot; |
|
31
|
|
|
private $note; |
|
32
|
|
|
|
|
33
|
12 |
|
private function __construct( |
|
34
|
|
|
Identity $identity, |
|
35
|
|
|
Agenda $agenda, |
|
36
|
|
|
Name $name, |
|
37
|
|
|
Slot $slot |
|
38
|
|
|
) { |
|
39
|
12 |
|
$this->identity = $identity; |
|
40
|
12 |
|
$this->agenda = $agenda; |
|
41
|
12 |
|
$this->name = $name; |
|
42
|
12 |
|
$this->slot = $slot; |
|
43
|
12 |
|
$this->note = new Note(''); |
|
44
|
12 |
|
} |
|
45
|
|
|
|
|
46
|
12 |
|
public static function add( |
|
47
|
|
|
Identity $identity, |
|
48
|
|
|
Agenda $agenda, |
|
49
|
|
|
Name $name, |
|
50
|
|
|
Slot $slot |
|
51
|
|
|
): self { |
|
52
|
12 |
|
$self = new self($identity, $agenda, $name, $slot); |
|
53
|
12 |
|
$self->record(new EventWasAdded($identity, $agenda, $name, $slot)); |
|
54
|
|
|
|
|
55
|
12 |
|
return $self; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
public function identity(): Identity |
|
59
|
|
|
{ |
|
60
|
4 |
|
return $this->identity; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function agenda(): Agenda |
|
64
|
|
|
{ |
|
65
|
2 |
|
return $this->agenda; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
public function name(): Name |
|
69
|
|
|
{ |
|
70
|
5 |
|
return $this->name; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
4 |
|
public function slot(): Slot |
|
74
|
|
|
{ |
|
75
|
4 |
|
return $this->slot; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
public function note(): Note |
|
79
|
|
|
{ |
|
80
|
3 |
|
return $this->note; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
3 |
|
public function rename(Name $name): self |
|
84
|
|
|
{ |
|
85
|
3 |
|
if ($name->equals($this->name)) { |
|
86
|
1 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$this->name = $name; |
|
90
|
2 |
|
$this->record(new EventWasRenamed($this->identity, $name)); |
|
91
|
|
|
|
|
92
|
2 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
public function move(Slot $slot): self |
|
96
|
|
|
{ |
|
97
|
2 |
|
$this->slot = $slot; |
|
98
|
2 |
|
$this->record(new EventWasMoved($this->identity, $slot)); |
|
99
|
|
|
|
|
100
|
2 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function addNote(Note $note): self |
|
104
|
|
|
{ |
|
105
|
2 |
|
$this->note = $note; |
|
106
|
2 |
|
$this->record(new NoteWasAdded($this->identity, $note)); |
|
107
|
|
|
|
|
108
|
2 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Last method that can be called, here only to record the event |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public function cancel(): void |
|
115
|
|
|
{ |
|
116
|
3 |
|
$this->record(new EventWasCanceled($this->identity)); |
|
117
|
3 |
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: