Completed
Push — master ( 19b369...87a841 )
by Amine
02:43
created

Pause::exportBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace AmineBenHariz\Attendance;
4
5
/**
6
 * Class Pause
7
 * @package AmineBenHariz\Attendance
8
 */
9
class Pause
10
{
11
    /**
12
     * @var \DateTime
13
     */
14
    private $start;
15
16
    /**
17
     * @var \DateTime
18
     */
19
    private $end;
20
21
    /**
22
     * Pause constructor.
23
     * @param \DateTime $start
24
     * @param \DateTime $end
25
     */
26 15
    public function __construct(\DateTime $start, \DateTime $end)
27
    {
28 15
        if ($start > $end) {
29 3
            throw new \InvalidArgumentException("Pause can't start after ending");
30
        }
31
32 12
        if ($start->format('Y-m-d') !== $end->format('Y-m-d')) {
33 3
            throw new \InvalidArgumentException("Pause must end in the same day");
34
        }
35
36 9
        $this->start = $start;
37 9
        $this->end = $end;
38 9
    }
39
40
    /**
41
     * @return \DateTime
42
     */
43 42
    public function getStart()
44
    {
45 42
        return $this->start;
46
    }
47
48
    /**
49
     * @return \DateTime
50
     */
51 39
    public function getEnd()
52
    {
53 39
        return $this->end;
54
    }
55
56
    /**
57
     * @return \DateInterval
58
     */
59 6
    public function getDuration()
60
    {
61 6
        return $this->getStart()->diff($this->getEnd());
62
    }
63
64
    /**
65
     * @param Pause $pause
66
     * @return bool
67
     */
68 21
    public function isOverlapping(Pause $pause)
69
    {
70 21
        if ($this->getEnd() <= $pause->getStart()) {
71 6
            return false;
72
        }
73
74 21
        if ($this->getStart() >= $pause->getEnd()) {
75 12
            return false;
76
        }
77
78 9
        return true;
79
    }
80
81
    /**
82
     * @return string
83
     */
84 6
    public function exportBlock()
85
    {
86 6
        return '(' . $this->getStart()->format('H:i') . '-' . $this->getEnd()->format('H:i') . ')';
87
    }
88
}
89