Completed
Push — develop ( 8e6584...40375e )
by Baptiste
02:11
created

Event::slot()   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\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...
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