Completed
Push — master ( defb96...17c7f8 )
by Amine
02:12
created

DayAttendance::addPause()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
crap 30
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