Completed
Push — master ( 749211...788a53 )
by Dominique
03:23
created

GenericPeriod   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 95
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A in() 0 11 5
A getStart() 0 3 1
A encloses() 0 9 3
A create() 0 11 2
A getEnd() 0 3 1
A __construct() 0 6 1
B intersects() 0 21 7
A equals() 0 4 2
1
<?php
2
declare(strict_types=1);
3
namespace Kreemers\Period;
4
5
use DateTime;
6
use Kreemers\Period\Exception\EndBeforeStartException;
7
8
final class GenericPeriod
9
{
10
    /**
11
     * @var DateTime
12
     */
13
    private $start;
14
15
    /**
16
     * @var DateTime
17
     */
18
    private $end;
19
20 1
    private function __construct(
21
        DateTime $start,
22
        DateTime $end
23
    ) {
24 1
        $this->start = clone $start;
25 1
        $this->end = clone $end;
26 1
    }
27
28 2
    public static function create(
29
        DateTime $start,
30
        DateTime $end
31
    ) {
32 2
        if ($start > $end) {
33 1
            throw new EndBeforeStartException();
34
        }
35
36 1
        return new static(
37 1
            $start,
38
            $end
39
        );
40
    }
41
42 1
    public function getStart(): DateTime
43
    {
44 1
        return $this->start;
45
    }
46
47 1
    public function getEnd(): DateTime
48
    {
49 1
        return $this->end;
50
    }
51
52 3
    public function equals(GenericPeriod $period): bool
53
    {
54 3
        return $this->getStart() == $period->getStart()
55 3
            && $this->getEnd() == $period->getEnd();
56
    }
57
58 2
    public function in(GenericPeriod $period): bool
59
    {
60 2
        if ($this->getStart() >= $period->getStart()
61 2
            && $this->getStart() <= $period->getEnd()
62 2
            && $this->getEnd() >= $period->getStart()
63 2
            && $this->getEnd() <= $period->getEnd()
64
        ) {
65 1
            return true;
66
        }
67
68 1
        return false;
69
    }
70
71 2
    public function encloses(GenericPeriod $period): bool
72
    {
73 2
        if ($this->getStart() <= $period->getStart()
74 2
            && $this->getEnd() >= $period->getEnd()
75
        ) {
76 1
            return true;
77
        }
78
79 1
        return false;
80
    }
81
82 5
    public function intersects(GenericPeriod $period): bool
83
    {
84 5
        if ($this->getStart() >= $period->getStart()
85 5
            && $this->getStart() <= $period->getEnd()
86
        ) {
87 2
            return true;
88
        }
89
90 3
        if ($this->getEnd() <= $period->getEnd()
91 3
            && $this->getEnd() >= $period->getStart()
92
        ) {
93 1
            return true;
94
        }
95
96 2
        if ($this->in($period)
97 2
            || $this->encloses($period)
98
        ) {
99 1
            return true;
100
        }
101
102 1
        return false;
103
    }
104
}