GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 13d483...138d08 )
by Dragos
11:07
created

BusinessHours::getTimezone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Speicher210\BusinessHours;
4
5
use Speicher210\BusinessHours\Day\DayInterface;
6
use Speicher210\BusinessHours\Day\Time\TimeBuilder;
7
8
/**
9
 * Default implementation of BusinessHoursInterface.
10
 */
11
class BusinessHours implements BusinessHoursInterface
12
{
13
    /**
14
     * The days.
15
     *
16
     * @var DayInterface[]
17
     */
18
    protected $days;
19
20
    /**
21
     * The time zone.
22
     *
23
     * @var \DateTimeZone
24
     */
25
    protected $timezone;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param DayInterface[] $days
31
     * @param \DateTimeZone|null $timezone
32
     */
33
    public function __construct(array $days, \DateTimeZone $timezone = null)
34
    {
35
        $this->setDays($days);
36
        $this->timezone = $timezone ?: new \DateTimeZone(date_default_timezone_get());
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getTimezone()
43
    {
44
        return $this->timezone;
45
    }
46
47
    /**
48
     * Get the days.
49
     *
50
     * @return DayInterface[]
51
     */
52
    public function getDays()
53
    {
54
        return array_values($this->days);
55
    }
56
57
    /**
58
     * Add a set of days.
59
     *
60
     * @param DayInterface[] $days The days.
61
     * @throws \InvalidArgumentException If no days are passed.
62
     */
63
    public function setDays(array $days)
64
    {
65
        if (empty($days)) {
66
            throw new \InvalidArgumentException('At least one day must be added.');
67
        }
68
69
        $this->days = [];
70
71
        foreach ($days as $day) {
72
            $this->addDay($day);
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function within(\DateTime $date)
80
    {
81
        $tmpDate = clone $date;
82
        $tmpDate->setTimezone($this->timezone);
83
84
        if (null !== $day = $this->getDay((int)$tmpDate->format('N'))) {
85
            return $day->isWithinOpeningHours(TimeBuilder::fromDate($tmpDate));
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getNextChangeDateTime(\DateTime $date = null)
95
    {
96
        if ($date === null) {
97
            $date = new \DateTime('now', $this->timezone);
98
        }
99
100
        $dateInterval = $this->closestDateInterval($date);
101
102
        if ($this->within($date)) {
103
            return ($date == $dateInterval->getStart()) ? $dateInterval->getStart() : $dateInterval->getEnd();
104
        } else {
105
            return $dateInterval->getStart();
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function closestDateInterval(\DateTime $date)
113
    {
114
        $tmpDate = clone $date;
115
        $tmpDate->setTimezone($this->timezone);
116
117
        return $this->getClosestInterval($tmpDate);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function jsonSerialize()
124
    {
125
        return array(
126
            'days' => $this->days,
127
            'timezone' => $this->timezone->getName(),
128
        );
129
    }
130
131
    /**
132
     * Get the closest business hours date interval after the given date.
133
     *
134
     * @param \DateTime $date
135
     * @return DateTimeInterval
136
     */
137
    private function getClosestDateIntervalAfter(\DateTime $date)
138
    {
139
        $tmpDate = clone $date;
140
        $dayOfWeek = (int)$tmpDate->format('N');
141
        $time = TimeBuilder::fromDate($tmpDate);
142
143 View Code Duplication
        if (null !== $day = $this->getDay($dayOfWeek)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
            if (null !== $closestTime = $day->getClosestOpeningHoursInterval($time)) {
145
                $intervalStart = clone $tmpDate;
146
                $intervalEnd = clone $tmpDate;
147
148
                $intervalStart->setTime(
149
                    $closestTime->getStart()->getHours(),
150
                    $closestTime->getStart()->getMinutes(),
151
                    $closestTime->getStart()->getSeconds()
152
                );
153
                $intervalEnd->setTime(
154
                    $closestTime->getEnd()->getHours(),
155
                    $closestTime->getEnd()->getMinutes(),
156
                    $closestTime->getEnd()->getSeconds()
157
                );
158
159
                return new DateTimeInterval($intervalStart, $intervalEnd);
160
            }
161
        }
162
163
        $tmpDate = $this->getDateAfter($tmpDate);
164
165
        $closestDay = $this->getClosestDayBefore((int)$tmpDate->format('N'));
166
167
        $openingTime = $closestDay->getOpeningTime();
168
        $closestTime = $closestDay->getClosestOpeningHoursInterval($openingTime);
169
170
        $intervalStart = clone $tmpDate;
171
        $intervalEnd = clone $tmpDate;
172
173
        $intervalStart->setTime(
174
            $closestTime->getStart()->getHours(),
175
            $closestTime->getStart()->getMinutes(),
176
            $closestTime->getStart()->getSeconds()
177
        );
178
        $intervalEnd->setTime(
179
            $closestTime->getEnd()->getHours(),
180
            $closestTime->getEnd()->getMinutes(),
181
            $closestTime->getEnd()->getSeconds()
182
        );
183
184
        return new DateTimeInterval($intervalStart, $intervalEnd);
185
    }
186
187
    /**
188
     * Get the business hours date after the given date (excluding holidays).
189
     *
190
     * @param \DateTime $date
191
     * @return \DateTime
192
     */
193
    private function getDateAfter(\DateTime $date)
194
    {
195
        $tmpDate = clone $date;
196
        $tmpDate->modify('+1 day');
197
198
        $dayOfWeek = (int)$tmpDate->format('N');
199
        $closestDay = $this->getClosestDayAfter($dayOfWeek);
200
201
        if ($closestDay->getDayOfWeek() !== $dayOfWeek) {
202
            $tmpDate->modify(sprintf('next %s', $closestDay->getDayOfWeekName()));
203
        }
204
205
        return $tmpDate;
206
    }
207
208
    /**
209
     * Get the closest interval endpoint after the given date.
210
     *
211
     * @param \DateTime $date
212
     * @return DateTimeInterval
213
     */
214
    private function getClosestInterval(\DateTime $date)
215
    {
216
        $tmpDate = clone $date;
217
        $dayOfWeek = (int)$tmpDate->format('N');
218
        $time = TimeBuilder::fromDate($tmpDate);
219
220 View Code Duplication
        if (null !== $day = $this->getDay($dayOfWeek)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
            if (null !== $closestTime = $day->getClosestOpeningHoursInterval($time)) {
222
                $intervalStart = clone $tmpDate;
223
                $intervalEnd = clone $tmpDate;
224
225
                $intervalStart->setTime(
226
                    $closestTime->getStart()->getHours(),
227
                    $closestTime->getStart()->getMinutes(),
228
                    $closestTime->getStart()->getSeconds()
229
                );
230
                $intervalEnd->setTime(
231
                    $closestTime->getEnd()->getHours(),
232
                    $closestTime->getEnd()->getMinutes(),
233
                    $closestTime->getEnd()->getSeconds()
234
                );
235
236
                return new DateTimeInterval($intervalStart, $intervalEnd);
237
            }
238
        }
239
240
        return $this->getClosestDateIntervalAfter($date);
241
    }
242
243
    /**
244
     * Get the closest business hours day before a given day number (including it).
245
     *
246
     * @param integer $dayNumber
247
     * @return DayInterface|null
248
     */
249
    private function getClosestDayBefore($dayNumber)
250
    {
251
        if (null !== $day = $this->getDay($dayNumber)) {
252
            return $day;
253
        }
254
255
        return $this->getDayBefore($dayNumber);
256
    }
257
258
    /**
259
     * Get the closest business hours day after a given day number (including it).
260
     *
261
     * @param integer $dayNumber
262
     * @return DayInterface|null
263
     */
264
    private function getClosestDayAfter($dayNumber)
265
    {
266
        if (null !== $day = $this->getDay($dayNumber)) {
267
            return $day;
268
        }
269
270
        return $this->getDayAfter($dayNumber);
271
    }
272
273
    /**
274
     * Get the business hours day before the day number.
275
     *
276
     * @param integer $dayNumber
277
     * @return DayInterface|null
278
     */
279 View Code Duplication
    private function getDayBefore($dayNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281
        $tmpDayNumber = $dayNumber;
282
283
        for ($i = 0; $i < 6; $i++) {
284
            $tmpDayNumber = (DayInterface::WEEK_DAY_MONDAY === $tmpDayNumber) ? DayInterface::WEEK_DAY_SUNDAY : --$tmpDayNumber;
285
286
            if (null !== $day = $this->getDay($tmpDayNumber)) {
287
                return $day;
288
            }
289
        }
290
291
        return $this->getDay($dayNumber);
292
    }
293
294
    /**
295
     * Get the business hours day after the day number.
296
     *
297
     * @param integer $dayNumber
298
     * @return DayInterface|null
299
     */
300 View Code Duplication
    private function getDayAfter($dayNumber)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
    {
302
        $tmpDayNumber = $dayNumber;
303
304
        for ($i = 0; $i < 6; $i++) {
305
            $tmpDayNumber = (DayInterface::WEEK_DAY_SUNDAY === $tmpDayNumber) ? DayInterface::WEEK_DAY_MONDAY : ++$tmpDayNumber;
306
307
            if (null !== $day = $this->getDay($tmpDayNumber)) {
308
                return $day;
309
            }
310
        }
311
312
        return $this->getDay($dayNumber);
313
    }
314
315
    /**
316
     * Get the day corresponding to the day number.
317
     *
318
     * @param integer $dayNumber
319
     * @return DayInterface|null
320
     */
321
    private function getDay($dayNumber)
322
    {
323
        return isset($this->days[$dayNumber]) ? $this->days[$dayNumber] : null;
324
    }
325
326
    /**
327
     * Add a day.
328
     *
329
     * @param DayInterface $day The day.
330
     */
331
    private function addDay(DayInterface $day)
332
    {
333
        $this->days[$day->getDayOfWeek()] = $day;
334
    }
335
336
    /**
337
     * Clone.
338
     */
339
    public function __clone()
340
    {
341
        $days = array();
342
        foreach ($this->days as $key => $day) {
343
            $days[$key] = clone $day;
344
        }
345
346
        $this->days = $days;
347
    }
348
}
349