Completed
Push — master ( 0a83c6...a0bcd3 )
by Amine
01:56
created

DayAttendance::getDuration()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4286
cc 3
eloc 6
nc 2
nop 0
crap 12
1
<?php
2
3
namespace AmineBenHariz\Attendance;
4
5
/**
6
 * Class DayAttendance
7
 * @package AmineBenHariz\Attendance
8
 */
9
class DayAttendance
10
{
11
    /**
12
     * @var \DateTime
13
     */
14
    private $arrival;
15
16
    /**
17
     * @var \DateTime
18
     */
19
    private $departure;
20
21
    /**
22
     * @var Pause[]
23
     */
24
    private $pauseList = [];
25
26
    /**
27
     * DayAttendance constructor.
28
     * @param \DateTime $arrival
29
     * @param \DateTime $departure
30
     * @param Pause[] $pauseList
31
     */
32 12
    public function __construct(\DateTime $arrival, \DateTime $departure, array $pauseList = [])
33
    {
34 12
        if ($arrival > $departure) {
35 4
            throw new \InvalidArgumentException;
36
        }
37
38 8
        if ($arrival->format('Y-m-d') !== $departure->format('Y-m-d')) {
39 4
            throw new \InvalidArgumentException;
40
        }
41
42 4
        $this->arrival = $arrival;
43 4
        $this->departure = $departure;
44 4
45 4
        if (!empty($pauseList)) {
46
            foreach ($pauseList as $pause) {
47
                $this->addPause($pause);
48
            }
49
        }
50 4
    }
51
52 4
    /**
53
     * @return \DateTime
54
     */
55
    public function getArrival()
56
    {
57
        return $this->arrival;
58 4
    }
59
60 4
    /**
61
     * @return \DateTime
62
     */
63
    public function getDeparture()
64
    {
65
        return $this->departure;
66 4
    }
67
68 4
    /**
69
     * @return Pause[]
70
     */
71
    public function getPauseList()
72
    {
73
        return $this->pauseList;
74
    }
75
76
    /**
77
     * @param Pause $pause
78
     */
79
    private function addPause(Pause $pause)
80
    {
81
        if ($pause->getStart() < $this->getArrival()) {
82
            throw new \InvalidArgumentException;
83
        }
84
85
        if ($pause->getEnd() > $this->getDeparture()) {
86
            throw new \InvalidArgumentException;
87
        }
88
89
        foreach ($this->getPauseList() as $existingPause) {
90
            if ($pause->isOverlapping($existingPause)) {
91
                throw new \InvalidArgumentException;
92
            }
93
        }
94
95
        $this->pauseList[] = $pause;
96
    }
97
98
    /**
99
     * @return \DateInterval
100
     */
101
    public function getDuration()
102
    {
103
        $cursor = clone $this->getArrival();
104
105
        if (!empty($this->getPauseList())) {
106
            foreach ($this->getPauseList() as $pause) {
107
                $cursor->add($pause->getDuration());
108
            }
109
        }
110
111
        return $cursor->diff($this->getDeparture());
112
    }
113
}
114