Completed
Push — master ( a193d4...2dff4e )
by Amine
04:00
created

DayAttendance::isValidDayAttendaceLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace AmineBenHariz\Attendance;
4
5
/**
6
 * Class DayAttendance
7
 * @package AmineBenHariz\Attendance
8
 */
9
class DayAttendance
10
{
11
    /**
12
     * example: 2015-12-12|08:30 (10:00-10:30) (12:30-13:30) (16:00-16:30) 17:30
13
     */
14
    const DAY_ATTENDANCE_LINE_REGEX = '/^\d{4}-\d{2}-\d{2}\|\d{2}:\d{2}( \(\d{2}:\d{2}-\d{2}:\d{2}\))* \d{2}:\d{2}$/';
15
16
    /**
17
     * @var \DateTime
18
     */
19
    private $arrival;
20
21
    /**
22
     * @var \DateTime
23
     */
24
    private $departure;
25
26
    /**
27
     * @var Pause[]
28
     */
29
    private $pauseList = [];
30
31
    /**
32 12
     * DayAttendance constructor.
33
     * @param \DateTime $arrival
34 12
     * @param \DateTime $departure
35 4
     * @param Pause[] $pauseList
36
     */
37
    public function __construct(\DateTime $arrival, \DateTime $departure, array $pauseList = [])
38 8
    {
39 4
        if ($arrival > $departure) {
40
            throw new \InvalidArgumentException;
41
        }
42 4
43 4
        if ($arrival->format('Y-m-d') !== $departure->format('Y-m-d')) {
44 4
            throw new \InvalidArgumentException;
45 4
        }
46
47
        $this->arrival = $arrival;
48
        $this->departure = $departure;
49
50 4
        if (!empty($pauseList)) {
51
            foreach ($pauseList as $pause) {
52 4
                $this->addPause($pause);
53
            }
54
        }
55
    }
56
57
    /**
58 4
     * @return \DateTime
59
     */
60 4
    public function getArrival()
61
    {
62
        return $this->arrival;
63
    }
64
65
    /**
66 4
     * @return \DateTime
67
     */
68 4
    public function getDeparture()
69
    {
70
        return $this->departure;
71
    }
72
73
    /**
74
     * @return Pause[]
75
     */
76
    public function getPauseList()
77
    {
78
        return $this->pauseList;
79
    }
80
81
    /**
82
     * @param Pause $pause
83
     */
84
    private function addPause(Pause $pause)
85
    {
86
        if ($pause->getStart() < $this->getArrival()) {
87
            throw new \InvalidArgumentException;
88
        }
89
90
        if ($pause->getEnd() > $this->getDeparture()) {
91
            throw new \InvalidArgumentException;
92
        }
93
94
        foreach ($this->getPauseList() as $existingPause) {
95
            if ($pause->isOverlapping($existingPause)) {
96
                throw new \InvalidArgumentException;
97
            }
98
        }
99
100
        $this->pauseList[] = $pause;
101
    }
102
103
    /**
104
     * @return \DateInterval
105
     */
106
    public function getDuration()
107
    {
108
        $cursor = clone $this->getArrival();
109
110
        // PHP 5.4 : empty() can only handle variables
111
        $pauseList = $this->getPauseList();
112
113
        if (!empty($pauseList)) {
114
            foreach ($pauseList as $pause) {
115
                $cursor->add($pause->getDuration());
116
            }
117
        }
118
119
        return $cursor->diff($this->getDeparture());
120
    }
121
122
    /**
123
     * @param $dayAttendanceLine
124
     * @return DayAttendance
125
     */
126
    public static function parseDayAttendanceLine($dayAttendanceLine)
127
    {
128
        if (!self::isValidDayAttendaceLine($dayAttendanceLine)) {
129
            throw new \InvalidArgumentException;
130
        }
131
132
        list($date, $timeLine) = explode('|', $dayAttendanceLine);
133
134
        $times = explode(' ', $timeLine);
135
136
        $arrival = new \DateTime($date . ' ' . array_shift($times));
137
        $departure = new \DateTime($date . ' ' . array_pop($times));
138
139
        $pauseList = [];
140
        if (!empty($times)) {
141
            foreach ($times as $pauseBlock) {
142
                // pauseBlock = (10:00-10:30)
143
                $pauseStart = new \DateTime($date . ' ' . substr($pauseBlock, 1, 5));
144
                $pauseEnd = new \DateTime($date . ' ' . substr($pauseBlock, 7, 5));
145
                $pauseList[] = new Pause($pauseStart, $pauseEnd);
146
            }
147
        }
148
149
        $dayAttendance = new DayAttendance($arrival, $departure, $pauseList);
150
        return $dayAttendance;
151
    }
152
153
    /**
154
     * @param $dayAttendanceLine
155
     * @return int
156
     */
157
    public static function isValidDayAttendaceLine($dayAttendanceLine)
158
    {
159
        return preg_match(self::DAY_ATTENDANCE_LINE_REGEX, $dayAttendanceLine) === 1;
160
    }
161
}
162