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 ( 8a1645...aea5ce )
by Kyle
17s queued 10s
created

OpeningHoursForDay::nextCloseRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use Countable;
6
use Generator;
7
use ArrayAccess;
8
use ArrayIterator;
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->findNextCloseRangeInWorkingHours($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 findNextCloseRangeInWorkingHours(Time $time, TimeRange $timeRange)
178
    {
179
        if ($timeRange->containsTime($time)) {
180
            return $timeRange;
181
        }
182
    }
183
184
    protected function findNextCloseRangeInFreeTime(Time $time, TimeRange $timeRange)
185
    {
186
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
187
            return $timeRange;
188
        }
189
    }
190
191
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange)
192
    {
193
        $range = $this->findNextCloseRangeInFreeTime($time, $timeRange);
194
195
        if ($range) {
196
            return $range->end();
197
        }
198
    }
199
200
    protected static function getHoursFromRange($range)
201
    {
202
        return strval((is_array($range)
203
            ? ($range['hours'] ?? array_values($range)[0] ?? null)
204
            : null
205
        ) ?: $range);
206
    }
207
208
    public function offsetExists($offset): bool
209
    {
210
        return isset($this->openingHours[$offset]);
211
    }
212
213
    public function offsetGet($offset)
214
    {
215
        return $this->openingHours[$offset];
216
    }
217
218
    public function offsetSet($offset, $value)
219
    {
220
        throw NonMutableOffsets::forClass(static::class);
221
    }
222
223
    public function offsetUnset($offset)
224
    {
225
        unset($this->openingHours[$offset]);
226
    }
227
228
    public function count(): int
229
    {
230
        return count($this->openingHours);
231
    }
232
233
    public function getIterator()
234
    {
235
        return new ArrayIterator($this->openingHours);
236
    }
237
238
    /**
239
     * @param Time $time
240
     *
241
     * @return TimeRange[]
242
     */
243
    public function forTime(Time $time): Generator
244
    {
245
        foreach ($this as $range) {
246
            /* @var TimeRange $range */
247
248
            if ($range->containsTime($time)) {
249
                yield $range;
250
            }
251
        }
252
    }
253
254
    /**
255
     * @param Time $time
256
     *
257
     * @return TimeRange[]
258
     */
259
    public function forNightTime(Time $time): Generator
260
    {
261
        foreach ($this as $range) {
262
            /* @var TimeRange $range */
263
264
            if ($range->containsNightTime($time)) {
265
                yield $range;
266
            }
267
        }
268
    }
269
270
    public function isEmpty(): bool
271
    {
272
        return empty($this->openingHours);
273
    }
274
275
    public function map(callable $callback): array
276
    {
277
        return Arr::map($this->openingHours, $callback);
278
    }
279
280
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
281
    {
282
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
283
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
284
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
285
            }
286
        }
287
    }
288
289
    public function __toString()
290
    {
291
        $values = [];
292
        foreach ($this->openingHours as $openingHour) {
293
            $values[] = (string) $openingHour;
294
        }
295
296
        return implode(',', $values);
297
    }
298
}
299