Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

TimeSlot::equals()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A TimeSlot::getStartDate() 0 3 1
A TimeSlot::getTimeSlots() 0 3 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\Organizer;
13
14
use DateTime;
15
use WBW\Library\Core\Argument\DateTimeHelper;
16
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
17
18
/**
19
 * Time slot.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Model\Organizer
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 (false === DateTimeHelper::isLessThan($startDate, $endDate)) {
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
     * Get the duration.
76
     *
77
     * @return int Returns the duration.
78
     */
79
    public function getDuration() {
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() {
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() {
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() {
107
        return $this->timeSlots;
108
    }
109
110
    /**
111
     * Remove a time slot.
112
     *
113
     * @param TimeSlot $timeSlot The time slot.
114
     * @return TimeSlot Returns this time slot.
115
     */
116
    public function removeTimeSlot(TimeSlot $timeSlot) {
117
        for ($i = count($this->timeSlots) - 1; 0 <= $i; --$i) {
118
            if (true === TimeSlotHelper::equals($timeSlot, $this->timeSlots[$i])) {
119
                unset($this->timeSlots[$i]);
120
                break;
121
            }
122
        }
123
        return $this;
124
    }
125
126
    /**
127
     * Set the end date.
128
     *
129
     * @param DateTime $endDate The end date.
130
     * @return TimeSlot Returns this time slot.
131
     */
132
    protected function setEndDate(DateTime $endDate) {
133
        $this->endDate = $endDate;
134
        return $this;
135
    }
136
137
    /**
138
     * Set the start date.
139
     *
140
     * @param DateTime $startDate The start date.
141
     * @return TimeSlot Returns this time slot.
142
     */
143
    protected function setStartDate(DateTime $startDate) {
144
        $this->startDate = $startDate;
145
        return $this;
146
    }
147
148
    /**
149
     * Set the time slots.
150
     *
151
     * @param TimeSlot[] $timeSlots The time slots.
152
     * @return TimeSlot Returns this time slot.
153
     */
154
    protected function setTimeSlots($timeSlots) {
155
        $this->timeSlots = $timeSlots;
156
        return $this;
157
    }
158
159
}
160