Completed
Push — master ( 44b4d6...c54f73 )
by WEBEWEB
06:15
created

TimeSlot   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 162
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addTimeSlot() 0 4 1
B equals() 0 27 6
A getEndDate() 0 3 1
A getStartDate() 0 3 1
A getTimeSlots() 0 3 1
A removeTimeSlot() 0 9 3
A setEndDate() 0 4 1
A setStartDate() 0 4 1
A setTimeSlots() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Model;
13
14
use DateTime;
15
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
16
use WBW\Library\Core\Helper\Argument\DateTimeHelper;
17
18
/**
19
 * Time slot.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Model
23
 */
24
class TimeSlot {
25
26
    /**
27
     * End date.
28
     *
29
     * @var DateTime
30
     */
31
    private $endDate;
32
33
    /**
34
     * StartDate.
35
     *
36
     * @var DateTime
37
     */
38
    private $startDate;
39
40
    /**
41
     * Time slots.
42
     *
43
     * @var TimeSlot[]
44
     */
45
    private $timeSlots;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param DateTime $startDate The start date.
51
     * @param DateTime $endDate The end date.
52
     * @throws IllegalArgumentException Throws an illegal argument exception.
53
     */
54
    public function __construct(DateTime $startDate, DateTime $endDate) {
55
        if ($endDate < $startDate) {
56
            throw new IllegalArgumentException("The end date must be greater than start date");
57
        }
58
        $this->setEndDate($endDate);
59
        $this->setStartDate($startDate);
60
        $this->setTimeSlots([]);
61
    }
62
63
    /**
64
     * Add a time slot.
65
     *
66
     * @param TimeSlot $timeSlot The time slot.
67
     * @return TimeSlot Returns this time slot.
68
     */
69
    public function addTimeSlot(TimeSlot $timeSlot) {
70
        $this->timeSlots[] = $timeSlot;
71
        return $this;
72
    }
73
74
    /**
75
     * Determines if two time slots are equals.
76
     *
77
     * @param TimeSlot $a The time slot.
78
     * @param TimeSlot $b The time slot.
79
     * @return boolean Returns true in case of success, false otherwise.
80
     */
81
    public static function equals(TimeSlot $a, TimeSlot $b) {
82
83
        // Compare the start dates.
84
        if (false === DateTimeHelper::equals($a->getStartDate(), $b->getStartDate())) {
85
            return false;
86
        }
87
88
        // Compare the end dates.
89
        if (false === DateTimeHelper::equals($a->getEndDate(), $b->getEndDate())) {
90
            return false;
91
        }
92
93
        // Compare the time slots count.
94
        if (count($a->getTimeSlots()) !== count($b->getTimeSlots())) {
95
            return false;
96
        }
97
98
        // Handle each time slot.
99
        for ($i = count($a->getTimeSlots()) - 1; 0 <= $i; --$i) {
100
            if (false === self::equals($a->getTimeSlots()[$i], $b->getTimeSlots()[$i])) {
101
                return false;
102
            }
103
        }
104
105
        //
106
        return true;
107
    }
108
109
    /**
110
     * Get the end date.
111
     *
112
     * @return DateTime Returns the end date.
113
     */
114
    public function getEndDate() {
115
        return $this->endDate;
116
    }
117
118
    /**
119
     * Get the start date.
120
     *
121
     * @return DateTime Returns the start date.
122
     */
123
    public function getStartDate() {
124
        return $this->startDate;
125
    }
126
127
    /**
128
     * Get the time slots.
129
     *
130
     * @return TimeSlot[] Returns the time slots.
131
     */
132
    public function getTimeSlots() {
133
        return $this->timeSlots;
134
    }
135
136
    /**
137
     * Remove a time slot.
138
     *
139
     * @param TimeSlot $timeSlot The time slot.
140
     * @return TimeSlot Returns this time slot.
141
     */
142
    public function removeTimeSlot(TimeSlot $timeSlot) {
143
        for ($i = count($this->timeSlots) - 1; 0 <= $i; --$i) {
144
            if (true === self::equals($timeSlot, $this->timeSlots[$i])) {
145
                unset($this->timeSlots[$i]);
146
                break;
147
            }
148
        }
149
        return $this;
150
    }
151
152
    /**
153
     * Set the end date.
154
     *
155
     * @param DateTime $endDate The end date.
156
     * @return TimeSlot Returns this time slot.
157
     */
158
    protected function setEndDate(DateTime $endDate) {
159
        $this->endDate = $endDate;
160
        return $this;
161
    }
162
163
    /**
164
     * Set the start date.
165
     *
166
     * @param DateTime $startDate The start date.
167
     * @return TimeSlot Returns this time slot.
168
     */
169
    protected function setStartDate(DateTime $startDate) {
170
        $this->startDate = $startDate;
171
        return $this;
172
    }
173
174
    /**
175
     * Set the time slots.
176
     *
177
     * @param TimeSlot[] $timeSlots The time slots.
178
     * @return TimeSlot Returns this time slot.
179
     */
180
    protected function setTimeSlots(array $timeSlots) {
181
        $this->timeSlots = $timeSlots;
182
        return $this;
183
    }
184
185
}
186