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
Pull Request — master (#131)
by Kyle
01:13
created

OpeningHoursForDay::findNextCloseRangeInFreeTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use Countable;
6
use ArrayAccess;
7
use ArrayIterator;
8
use Generator;
9
use IteratorAggregate;
10
use Spatie\OpeningHours\Helpers\Arr;
11
use Spatie\OpeningHours\Helpers\DataTrait;
12
use Spatie\OpeningHours\Exceptions\NonMutableOffsets;
13
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges;
14
15
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate
16
{
17
    use DataTrait;
18
19
    /** @var \Spatie\OpeningHours\TimeRange[] */
20
    protected $openingHours = [];
21
22
    public static function fromStrings(array $strings)
23
    {
24
        if (isset($strings['hours'])) {
25
            return static::fromStrings($strings['hours'])->setData($strings['data'] ?? null);
26
        }
27
28
        $openingHoursForDay = new static();
29
30
        if (isset($strings['data'])) {
31
            $openingHoursForDay->setData($strings['data'] ?? null);
32
            unset($strings['data']);
33
        }
34
35
        uasort($strings, function ($a, $b) {
36
            return strcmp(static::getHoursFromRange($a), static::getHoursFromRange($b));
37
        });
38
39
        $timeRanges = Arr::map($strings, function ($string) {
40
            return TimeRange::fromDefinition($string);
41
        });
42
43
        $openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges);
44
45
        $openingHoursForDay->openingHours = $timeRanges;
46
47
        return $openingHoursForDay;
48
    }
49
50
    public function isOpenAt(Time $time)
51
    {
52
        foreach ($this->openingHours as $timeRange) {
53
            if ($timeRange->containsTime($time)) {
54
                return true;
55
            }
56
        }
57
58
        return false;
59
    }
60
61
    public function isOpenAtNight(Time $time)
62
    {
63
        foreach ($this->openingHours as $timeRange) {
64
            if ($timeRange->containsNightTime($time)) {
65
                return true;
66
            }
67
        }
68
69
        return false;
70
    }
71
72
    /**
73
     * @param callable[] $filters
74
     *
75
     * @return Time|bool
76
     */
77
    public function openingHoursFilter(array $filters)
78
    {
79
        foreach ($this->openingHours as $timeRange) {
80
            foreach ($filters as $filter) {
81
                if ($result = $filter($timeRange)) {
82
                    reset($timeRange);
83
84
                    return $result;
85
                }
86
            }
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * @param Time $time
94
     *
95
     * @return bool|Time
96
     */
97
    public function nextOpen(Time $time)
98
    {
99
        return $this->openingHoursFilter([
100
            function ($timeRange) use ($time) {
101
                return $this->findNextOpenInFreeTime($time, $timeRange);
102
            },
103
        ]);
104
    }
105
106
    /**
107
     * @param Time $time
108
     *
109
     * @return bool|TimeRange
110
     */
111
    public function nextOpenRange(Time $time)
112
    {
113
        return $this->openingHoursFilter([
114
            function ($timeRange) use ($time) {
115
                return $this->findNextOpenRangeInFreeTime($time, $timeRange);
116
            },
117
        ]);
118
    }
119
120
    /**
121
     * @param Time $time
122
     *
123
     * @return bool|Time
124
     */
125 View Code Duplication
    public function nextClose(Time $time)
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...
126
    {
127
        return $this->openingHoursFilter([
128
            function ($timeRange) use ($time) {
129
                return $this->findNextCloseInWorkingHours($time, $timeRange);
130
            },
131
            function ($timeRange) use ($time) {
132
                return $this->findNextCloseInFreeTime($time, $timeRange);
133
            },
134
        ]);
135
    }
136
137
    /**
138
     * @param Time $time
139
     *
140
     * @return bool|TimeRange
141
     */
142 View Code Duplication
    public function nextCloseRange(Time $time)
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...
143
    {
144
        return $this->openingHoursFilter([
145
            function ($timeRange) use ($time) {
146
                return $this->findNextCloseInWorkingHours($time, $timeRange);
147
            },
148
            function ($timeRange) use ($time) {
149
                return $this->findNextCloseRangeInFreeTime($time, $timeRange);
150
            },
151
        ]);
152
    }
153
154
    protected function findNextOpenRangeInFreeTime(Time $time, TimeRange $timeRange)
155
    {
156
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
157
            return $timeRange;
158
        }
159
    }
160
161
    protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange)
162
    {
163
        $range = $this->findNextOpenRangeInFreeTime($time, $timeRange);
164
165
        if ($range) {
166
            return $range->start();
167
        }
168
    }
169
170
    protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange)
171
    {
172
        if ($timeRange->containsTime($time)) {
173
            return next($timeRange);
174
        }
175
    }
176
177
    protected function findNextCloseRangeInFreeTime(Time $time, TimeRange $timeRange)
178
    {
179
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
180
            return $timeRange;
181
        }
182
    }
183
184
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange)
185
    {
186
        $range = $this->findNextCloseRangeInFreeTime($time, $timeRange);
187
188
        if ($range) {
189
            return $range->end();
190
        }
191
    }
192
193
    protected static function getHoursFromRange($range)
194
    {
195
        return strval((is_array($range)
196
            ? ($range['hours'] ?? array_values($range)[0] ?? null)
197
            : null
198
        ) ?: $range);
199
    }
200
201
    public function offsetExists($offset): bool
202
    {
203
        return isset($this->openingHours[$offset]);
204
    }
205
206
    public function offsetGet($offset)
207
    {
208
        return $this->openingHours[$offset];
209
    }
210
211
    public function offsetSet($offset, $value)
212
    {
213
        throw NonMutableOffsets::forClass(static::class);
214
    }
215
216
    public function offsetUnset($offset)
217
    {
218
        unset($this->openingHours[$offset]);
219
    }
220
221
    public function count(): int
222
    {
223
        return count($this->openingHours);
224
    }
225
226
    public function getIterator()
227
    {
228
        return new ArrayIterator($this->openingHours);
229
    }
230
231
    /**
232
     * @param Time $time
233
     *
234
     * @return TimeRange[]
235
     */
236
    public function forTime(Time $time): Generator
237
    {
238
        foreach ($this as $range) {
239
            /* @var TimeRange $range */
240
241
            if ($range->containsTime($time)) {
242
                yield $range;
243
            }
244
        }
245
    }
246
247
    /**
248
     * @param Time $time
249
     *
250
     * @return TimeRange[]
251
     */
252
    public function forNightTime(Time $time): Generator
253
    {
254
        foreach ($this as $range) {
255
            /* @var TimeRange $range */
256
257
            if ($range->containsNightTime($time)) {
258
                yield $range;
259
            }
260
        }
261
    }
262
263
    public function getReversedIterator()
264
    {
265
        return new ArrayIterator(array_reverse($this->openingHours));
266
    }
267
268
    public function isEmpty(): bool
269
    {
270
        return empty($this->openingHours);
271
    }
272
273
    public function map(callable $callback): array
274
    {
275
        return Arr::map($this->openingHours, $callback);
276
    }
277
278
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
279
    {
280
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
281
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
282
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
283
            }
284
        }
285
    }
286
287
    public function __toString()
288
    {
289
        $values = [];
290
        foreach ($this->openingHours as $openingHour) {
291
            $values[] = (string) $openingHour;
292
        }
293
294
        return implode(',', $values);
295
    }
296
}
297