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\Agenda\Identity as Agenda, |
|
|
|
|
11
|
|
|
Event\EventWasAdded, |
12
|
|
|
Event\EventWasRenamed, |
13
|
|
|
Event\EventWasMoved, |
14
|
|
|
Event\EventWasCanceled, |
15
|
|
|
}; |
16
|
|
|
use Innmind\EventBus\{ |
17
|
|
|
ContainsRecordedEventsInterface, |
18
|
|
|
EventRecorder, |
19
|
|
|
}; |
20
|
|
|
|
21
|
|
|
final class Event implements ContainsRecordedEventsInterface |
22
|
|
|
{ |
23
|
|
|
use EventRecorder; |
24
|
|
|
|
25
|
|
|
private $identity; |
26
|
|
|
private $agenda; |
27
|
|
|
private $name; |
28
|
|
|
private $slot; |
29
|
|
|
|
30
|
10 |
|
private function __construct( |
31
|
|
|
Identity $identity, |
32
|
|
|
Agenda $agenda, |
33
|
|
|
Name $name, |
34
|
|
|
Slot $slot |
35
|
|
|
) { |
36
|
10 |
|
$this->identity = $identity; |
37
|
10 |
|
$this->agenda = $agenda; |
38
|
10 |
|
$this->name = $name; |
39
|
10 |
|
$this->slot = $slot; |
40
|
10 |
|
} |
41
|
|
|
|
42
|
10 |
|
public static function add( |
43
|
|
|
Identity $identity, |
44
|
|
|
Agenda $agenda, |
45
|
|
|
Name $name, |
46
|
|
|
Slot $slot |
47
|
|
|
): self { |
48
|
10 |
|
$self = new self($identity, $agenda, $name, $slot); |
49
|
10 |
|
$self->record(new EventWasAdded($identity, $agenda, $name, $slot)); |
50
|
|
|
|
51
|
10 |
|
return $self; |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
public function identity(): Identity |
55
|
|
|
{ |
56
|
4 |
|
return $this->identity; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function agenda(): Agenda |
60
|
|
|
{ |
61
|
2 |
|
return $this->agenda; |
62
|
|
|
} |
63
|
|
|
|
64
|
5 |
|
public function name(): Name |
65
|
|
|
{ |
66
|
5 |
|
return $this->name; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
public function slot(): Slot |
70
|
|
|
{ |
71
|
4 |
|
return $this->slot; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function rename(Name $name): self |
75
|
|
|
{ |
76
|
3 |
|
if ($name->equals($this->name)) { |
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$this->name = $name; |
81
|
2 |
|
$this->record(new EventWasRenamed($this->identity, $name)); |
82
|
|
|
|
83
|
2 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
public function move(Slot $slot): self |
87
|
|
|
{ |
88
|
2 |
|
$this->slot = $slot; |
89
|
2 |
|
$this->record(new EventWasMoved($this->identity, $slot)); |
90
|
|
|
|
91
|
2 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Last method that can be called, here only to record the event |
96
|
|
|
*/ |
97
|
3 |
|
public function cancel(): void |
98
|
|
|
{ |
99
|
3 |
|
$this->record(new EventWasCanceled($this->identity)); |
100
|
3 |
|
} |
101
|
|
|
} |
102
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: