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

Slot::overlaps()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 5
nop 1
dl 0
loc 35
ccs 10
cts 10
cp 1
crap 5
rs 8.439
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace PersonalGalaxy\Calendar\Entity\Event;
5
6
use PersonalGalaxy\Calendar\Exception\{
7
    LogicException,
8
    EmptySlot,
9
};
10
use Innmind\TimeContinuum\PointInTimeInterface;
11
12
final class Slot
13
{
14
    private $start;
15
    private $end;
16
17 29
    public function __construct(
18
        PointInTimeInterface $start,
19
        PointInTimeInterface $end
20
    ) {
21 29
        if ($start->aheadOf($end)) {
22 1
            throw new LogicException;
23
        }
24
25 28
        if ($start->equals($end)) {
26 1
            throw new EmptySlot;
27
        }
28
29 27
        $this->start = $start;
30 27
        $this->end = $end;
31 27
    }
32
33 15
    public function start(): PointInTimeInterface
34
    {
35 15
        return $this->start;
36
    }
37
38 9
    public function end(): PointInTimeInterface
39
    {
40 9
        return $this->end;
41
    }
42
43 9
    public function overlaps(self $slot): bool
44
    {
45
        /**
46
         * this: |----|
47
         * slot:        |----|
48
         */
49 9
        if ($slot->start()->aheadOf($this->end)) {
50 1
            return false;
51
        }
52
53
        /**
54
         * this:        |----|
55
         * slot: |----|
56
         */
57 8
        if ($this->start->aheadOf($slot->end())) {
58 1
            return false;
59
        }
60
61
        /**
62
         * this: |----|
63
         * slot:      |----|
64
         */
65 7
        if ($slot->start()->equals($this->end)) {
66 1
            return false;
67
        }
68
69
        /**
70
         * this:      |----|
71
         * slot: |----|
72
         */
73 6
        if ($this->start->equals($slot->end())) {
74 1
            return false;
75
        }
76
77 5
        return true;
78
    }
79
}
80