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

AbstractDay::__clone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Speicher210\BusinessHours\Day;
4
5
use Speicher210\BusinessHours\Day\Time\Time;
6
use Speicher210\BusinessHours\Day\Time\TimeInterval;
7
use Speicher210\BusinessHours\Day\Time\TimeIntervalInterface;
8
9
/**
10
 * Abstract day class.
11
 */
12
abstract class AbstractDay implements DayInterface
13
{
14
    /**
15
     * The days of the week.
16
     *
17
     * @var array
18
     */
19
    private $daysOfWeek = array(
20
        DayInterface::WEEK_DAY_MONDAY => 'Monday',
21
        DayInterface::WEEK_DAY_TUESDAY => 'Tuesday',
22
        DayInterface::WEEK_DAY_WEDNESDAY => 'Wednesday',
23
        DayInterface::WEEK_DAY_THURSDAY => 'Thursday',
24
        DayInterface::WEEK_DAY_FRIDAY => 'Friday',
25
        DayInterface::WEEK_DAY_SATURDAY => 'Saturday',
26
        DayInterface::WEEK_DAY_SUNDAY => 'Sunday',
27
    );
28
29
    /**
30
     * The day of week.
31
     *
32
     * @var integer
33
     */
34
    protected $dayOfWeek;
35
36
    /**
37
     * The time intervals.
38
     *
39
     * @var TimeIntervalInterface[]
40
     */
41
    protected $openingHoursIntervals;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param integer $dayOfWeek The day of week.
47
     * @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals.
48
     */
49
    public function __construct($dayOfWeek, array $openingHoursIntervals)
50
    {
51
        $this->setDayOfWeek($dayOfWeek);
52
        $this->setOpeningHoursIntervals($openingHoursIntervals);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getDayOfWeek()
59
    {
60
        return $this->dayOfWeek;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getDayOfWeekName()
67
    {
68
        if (isset($this->daysOfWeek[$this->dayOfWeek])) {
69
            return $this->daysOfWeek[$this->dayOfWeek];
70
        }
71
72
        throw new \OutOfBoundsException('Invalid day of week.');
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getOpeningHoursIntervals()
79
    {
80
        return $this->openingHoursIntervals;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getClosestOpeningHoursInterval(Time $time)
87
    {
88
        foreach ($this->openingHoursIntervals as $openingHoursInterval) {
89
            if ($openingHoursInterval->contains($time)) {
90
                return $openingHoursInterval;
91
            }
92
        }
93
94
        return $this->getNextOpeningHoursInterval($time);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getNextOpeningHoursInterval(Time $time)
101
    {
102
        $closestTime = null;
103
        $closestInterval = null;
104
105
        foreach ($this->openingHoursIntervals as $interval) {
106
            $distance = $interval->getStart()->toInteger() - $time->toInteger();
107
108
            if ($distance < 0) {
109
                continue;
110
            }
111
112
            if (null === $closestTime) {
113
                $closestTime = $interval->getStart();
114
                $closestInterval = $interval;
115
            }
116
117
            if ($distance < $closestTime->toInteger() - $time->toInteger()) {
118
                $closestTime = $interval->getStart();
119
                $closestInterval = $interval;
120
            }
121
        }
122
123
        return $closestInterval;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getOpeningTime()
130
    {
131
        return $this->openingHoursIntervals[0]->getStart();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getClosingTime()
138
    {
139
        /** @var TimeIntervalInterface $interval */
140
        $interval = end($this->openingHoursIntervals);
141
142
        return $interval->getEnd();
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function isWithinOpeningHours(Time $time)
149
    {
150
        foreach ($this->openingHoursIntervals as $interval) {
151
            if ($interval->contains($time)) {
152
                return true;
153
            }
154
        }
155
156
        return false;
157
    }
158
159
    /**
160
     * Set the day of week.
161
     *
162
     * @param int $dayOfWeek
163
     * @throws \OutOfBoundsException If the given day is invalid.
164
     */
165
    protected function setDayOfWeek($dayOfWeek)
166
    {
167
        if (!isset($this->daysOfWeek[$dayOfWeek])) {
168
            throw new \OutOfBoundsException(sprintf('Invalid day of week "%s".', $dayOfWeek));
169
        }
170
171
        $this->dayOfWeek = $dayOfWeek;
172
    }
173
174
    /**
175
     * Set the opening hours intervals.
176
     *
177
     * @param TimeIntervalInterface[] $openingHoursIntervals The opening hours intervals.
178
     * @throws \InvalidArgumentException If no days are passed or invalid interval is passed.
179
     */
180
    protected function setOpeningHoursIntervals(array $openingHoursIntervals)
181
    {
182
        if (empty($openingHoursIntervals)) {
183
            throw new \InvalidArgumentException('The day must have at least one opening interval.');
184
        }
185
186
        $intervals = array();
187
188
        foreach ($openingHoursIntervals as $interval) {
189
            if (!$interval instanceof TimeIntervalInterface) {
190
                throw new \InvalidArgumentException(sprintf('Interval must be a %s', TimeIntervalInterface::class));
191
            }
192
193
            $intervals[] = $interval;
194
        }
195
196
        $this->openingHoursIntervals = $this->flattenOpeningHoursIntervals($intervals);
197
    }
198
199
    /**
200
     * Flatten the intervals that overlap.
201
     *
202
     * @param TimeIntervalInterface[] $openingHoursIntervals
203
     * @return TimeIntervalInterface[]
204
     */
205
    protected function flattenOpeningHoursIntervals(array $openingHoursIntervals)
206
    {
207
        usort(
208
            $openingHoursIntervals,
209
            function (TimeIntervalInterface $a, TimeIntervalInterface $b) {
210
                return ($a->getStart() > $b->getStart()) ? 1 : -1;
211
            }
212
        );
213
214
        $intervals = array();
215
        $tmpInterval = reset($openingHoursIntervals);
216
        foreach ($openingHoursIntervals as $interval) {
217
            /** @var TimeInterval $tmpInterval */
218
            if ($interval->getStart() <= $tmpInterval->getEnd()) {
219
                $tmpInterval = new TimeInterval(
220
                    $tmpInterval->getStart(),
221
                    max($tmpInterval->getEnd(), $interval->getEnd())
222
                );
223
            } else {
224
                $intervals[] = $tmpInterval;
225
                $tmpInterval = $interval;
226
            }
227
        }
228
229
        $intervals[] = $tmpInterval;
230
231
        return $intervals;
232
    }
233
234
    /**
235
     * Handle cloning.
236
     */
237
    public function __clone()
238
    {
239
        $openingHoursIntervals = array();
240
241
        foreach ($this->openingHoursIntervals as $openingHoursInterval) {
242
            $openingHoursIntervals[] = clone $openingHoursInterval;
243
        }
244
245
        $this->openingHoursIntervals = $openingHoursIntervals;
246
    }
247
}
248