TimeSlot   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 20
lcom 2
cbo 2
dl 0
loc 180
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A addTimeSlot() 0 4 1
A getDuration() 0 3 1
A getEndDate() 0 3 1
A getStartDate() 0 3 1
A getTimeSlots() 0 3 1
B leftJoinWithout() 0 39 7
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\Utility;
13
14
use DateTime;
15
use InvalidArgumentException;
16
use WBW\Library\Core\Argument\Helper\DateTimeHelper;
17
18
/**
19
 * Time slot.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Utility
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 InvalidArgumentException Throws an illegal argument exception.
53
     */
54
    public function __construct(DateTime $startDate, DateTime $endDate) {
55
        if (false === DateTimeHelper::isLessThan($startDate, $endDate)) {
56
            throw new InvalidArgumentException("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): TimeSlot {
70
        $this->timeSlots[] = $timeSlot;
71
        return $this;
72
    }
73
74
    /**
75
     * Get the duration.
76
     *
77
     * @return int Returns the duration.
78
     */
79
    public function getDuration(): int {
80
        return DateTimeHelper::getDuration($this->getStartDate(), $this->getEndDate());
81
    }
82
83
    /**
84
     * Get the end date.
85
     *
86
     * @return DateTime Returns the end date.
87
     */
88
    public function getEndDate(): DateTime {
89
        return $this->endDate;
90
    }
91
92
    /**
93
     * Get the start date.
94
     *
95
     * @return DateTime Returns the start date.
96
     */
97
    public function getStartDate(): DateTime {
98
        return $this->startDate;
99
    }
100
101
    /**
102
     * Get the time slots.
103
     *
104
     * @return TimeSlot[] Returns the time slots.
105
     */
106
    public function getTimeSlots(): array {
107
        return $this->timeSlots;
108
    }
109
110
    /**
111
     * Left join without.
112
     *
113
     * @return TimeSlot[] Returns the time slots.
114
     */
115
    public function leftJoinWithout(): array {
116
117
        // Sort and count the time slots.
118
        $buffer = TimeSlotHelper::merge($this->getTimeSlots());
119
        $number = count($buffer);
120
121
        // Check the time slots count.
122
        if (0 === $number) {
123
            return [$this];
124
        }
125
126
        // Initialize the output.
127
        $output = [$this];
128
129
        // Handle each time slot.
130
        for ($i = 0; $i < $number; ++$i) {
131
132
            //
133
            $j = count($output) - 1;
134
135
            // Left join without.
136
            $res = TimeSlotHelper::leftJoinWithout($output[$j], $buffer[$i]);
137
            if (null === $res) {
138
                continue;
139
            }
140
141
            //
142
            $output[$j] = $res[0];
143
            if (2 === count($res)) {
144
                $output[] = $res[1];
145
            }
146
        }
147
148
        // Return the output.
149
        if (1 === count($output) && $this === $output[0]) {
150
            return [];
151
        }
152
        return $output;
153
    }
154
155
    /**
156
     * Remove a time slot.
157
     *
158
     * @param TimeSlot $timeSlot The time slot.
159
     * @return TimeSlot Returns this time slot.
160
     */
161
    public function removeTimeSlot(TimeSlot $timeSlot): TimeSlot {
162
        for ($i = count($this->timeSlots) - 1; 0 <= $i; --$i) {
163
            if (true !== TimeSlotHelper::equals($timeSlot, $this->timeSlots[$i])) {
164
                continue;
165
            }
166
            unset($this->timeSlots[$i]);
167
        }
168
        return $this;
169
    }
170
171
    /**
172
     * Set the end date.
173
     *
174
     * @param DateTime $endDate The end date.
175
     * @return TimeSlot Returns this time slot.
176
     */
177
    protected function setEndDate(DateTime $endDate): TimeSlot {
178
        $this->endDate = $endDate;
179
        return $this;
180
    }
181
182
    /**
183
     * Set the start date.
184
     *
185
     * @param DateTime $startDate The start date.
186
     * @return TimeSlot Returns this time slot.
187
     */
188
    protected function setStartDate(DateTime $startDate): TimeSlot {
189
        $this->startDate = $startDate;
190
        return $this;
191
    }
192
193
    /**
194
     * Set the time slots.
195
     *
196
     * @param TimeSlot[] $timeSlots The time slots.
197
     * @return TimeSlot Returns this time slot.
198
     */
199
    protected function setTimeSlots(array $timeSlots): TimeSlot {
200
        $this->timeSlots = $timeSlots;
201
        return $this;
202
    }
203
}
204