Completed
Push — master ( 544410...e26f77 )
by Amine
03:56
created

DayAttendance::getArrival()   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 1
Metric Value
c 1
b 0
f 1
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 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
     * DayAttendance constructor.
33
     * @param \DateTime $arrival
34
     * @param \DateTime $departure
35
     * @param Pause[] $pauseList
36
     */
37 21
    public function __construct(\DateTime $arrival, \DateTime $departure, array $pauseList = [])
38
    {
39 21
        if ($arrival > $departure) {
40 3
            throw new \InvalidArgumentException;
41
        }
42
43 18
        if ($arrival->format('Y-m-d') !== $departure->format('Y-m-d')) {
44 3
            throw new \InvalidArgumentException;
45
        }
46
47 15
        $this->arrival = $arrival;
48 15
        $this->departure = $departure;
49
50 15
        if (!empty($pauseList)) {
51 15
            foreach ($pauseList as $pause) {
52 15
                $this->addPause($pause);
53 12
            }
54 6
        }
55 6
    }
56
57
    /**
58
     * @return \DateTime
59
     */
60 18
    public function getArrival()
61
    {
62 18
        return $this->arrival;
63
    }
64
65
    /**
66
     * @return \DateTime
67
     */
68 15
    public function getDeparture()
69
    {
70 15
        return $this->departure;
71
    }
72
73
    /**
74
     * @return Pause[]
75
     */
76 15
    public function getPauseList()
77
    {
78 15
        return $this->pauseList;
79
    }
80
81
    /**
82
     * @param Pause $pause
83
     */
84 15
    private function addPause(Pause $pause)
85
    {
86 15
        if ($pause->getStart() < $this->getArrival()) {
87 3
            throw new \InvalidArgumentException;
88
        }
89
90 12
        if ($pause->getEnd() > $this->getDeparture()) {
91 3
            throw new \InvalidArgumentException;
92
        }
93
94 12
        foreach ($this->getPauseList() as $existingPause) {
95 9
            if ($pause->isOverlapping($existingPause)) {
96 3
                throw new \InvalidArgumentException;
97
            }
98 12
        }
99
100 12
        $this->pauseList[] = $pause;
101 12
    }
102
103
    /**
104
     * @return \DateInterval
105
     */
106 3
    public function getDuration()
107
    {
108 3
        $cursor = clone $this->getArrival();
109
110
        // PHP 5.4 : empty() can only handle variables
111 3
        $pauseList = $this->getPauseList();
112
113 3
        if (!empty($pauseList)) {
114 3
            foreach ($pauseList as $pause) {
115 3
                $cursor->add($pause->getDuration());
116 3
            }
117 3
        }
118
119 3
        return $cursor->diff($this->getDeparture());
120
    }
121
122
    /**
123
     * @param $dayAttendanceLine
124
     * @return DayAttendance
125
     */
126 6
    public static function parseDayAttendanceLine($dayAttendanceLine)
127
    {
128 6
        if (!self::isValidDayAttendaceLine($dayAttendanceLine)) {
129 3
            throw new \InvalidArgumentException;
130
        }
131
132 3
        list($date, $timeLine) = explode('|', $dayAttendanceLine);
133
134 3
        $times = explode(' ', $timeLine);
135
136 3
        $arrival = new \DateTime($date . ' ' . array_shift($times));
137 3
        $departure = new \DateTime($date . ' ' . array_pop($times));
138
139 3
        $pauseList = [];
140 3
        if (!empty($times)) {
141 3
            foreach ($times as $pauseBlock) {
142
                // pauseBlock = (10:00-10:30)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
143 3
                $pauseStart = new \DateTime($date . ' ' . substr($pauseBlock, 1, 5));
144 3
                $pauseEnd = new \DateTime($date . ' ' . substr($pauseBlock, 7, 5));
145 3
                $pauseList[] = new Pause($pauseStart, $pauseEnd);
146 3
            }
147 3
        }
148
149 3
        $dayAttendance = new DayAttendance($arrival, $departure, $pauseList);
150 3
        return $dayAttendance;
151
    }
152
153
    /**
154
     * @param $dayAttendanceLine
155
     * @return int
156
     */
157 33
    public static function isValidDayAttendaceLine($dayAttendanceLine)
158
    {
159 33
        return preg_match(self::DAY_ATTENDANCE_LINE_REGEX, $dayAttendanceLine) === 1;
160
    }
161
}
162