Completed
Push — develop ( 40375e...b9556e )
by Baptiste
01:56
created

Event::note()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PersonalGalaxy\Calendar\Entity\Agenda. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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