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 ( e31ec7...148746 )
by Kyle
12s
created

OpeningHoursForDay::isOpenAtNight()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use Countable;
6
use ArrayAccess;
7
use ArrayIterator;
8
use IteratorAggregate;
9
use Spatie\OpeningHours\Helpers\Arr;
10
use Spatie\OpeningHours\Helpers\DataTrait;
11
use Spatie\OpeningHours\Exceptions\NonMutableOffsets;
12
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges;
13
14
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate
15
{
16
    use DataTrait;
17
18
    /** @var \Spatie\OpeningHours\TimeRange[] */
19
    protected $openingHours = [];
20
21
    public static function fromStrings(array $strings)
22
    {
23
        if (isset($strings['hours'])) {
24
            return static::fromStrings($strings['hours'])->setData($strings['data'] ?? null);
25
        }
26
27
        $openingHoursForDay = new static();
28
29
        if (isset($strings['data'])) {
30
            $openingHoursForDay->setData($strings['data'] ?? null);
31
            unset($strings['data']);
32
        }
33
34
        $timeRanges = Arr::map($strings, function ($string) {
35
            return TimeRange::fromDefinition($string);
36
        });
37
38
        $openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges);
39
40
        $openingHoursForDay->openingHours = $timeRanges;
41
42
        return $openingHoursForDay;
43
    }
44
45
    public function isOpenAt(Time $time)
46
    {
47
        foreach ($this->openingHours as $timeRange) {
48
            if ($timeRange->containsTime($time)) {
49
                return true;
50
            }
51
        }
52
53
        return false;
54
    }
55
56
    public function isOpenAtNight(Time $time)
57
    {
58
        foreach ($this->openingHours as $timeRange) {
59
            if ($timeRange->overflowsNextDay() && TimeRange::fromMidnight($timeRange->end())->containsTime($time)) {
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * @param callable[] $filters
69
     *
70
     * @return Time|bool
71
     */
72
    public function openingHoursFilter(array $filters)
73
    {
74
        foreach ($this->openingHours as $timeRange) {
75
            foreach ($filters as $filter) {
76
                if ($result = $filter($timeRange)) {
77
                    reset($timeRange);
78
79
                    return $result;
80
                }
81
            }
82
        }
83
84
        return false;
85
    }
86
87
    /**
88
     * @param Time $time
89
     *
90
     * @return bool|Time
91
     */
92
    public function nextOpen(Time $time)
93
    {
94
        return $this->openingHoursFilter([
95
            function ($timeRange) use ($time) {
96
                return $this->findNextOpenInFreeTime($time, $timeRange);
97
            },
98
        ]);
99
    }
100
101
    /**
102
     * @param Time $time
103
     *
104
     * @return bool|Time
105
     */
106
    public function nextClose(Time $time)
107
    {
108
        return $this->openingHoursFilter([
109
            function ($timeRange) use ($time) {
110
                return $this->findNextCloseInWorkingHours($time, $timeRange);
111
            },
112
            function ($timeRange) use ($time) {
113
                return $this->findNextCloseInFreeTime($time, $timeRange);
114
            },
115
        ]);
116
    }
117
118
    protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange)
119
    {
120
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
121
            return $timeRange->start();
122
        }
123
    }
124
125
    protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange)
126
    {
127
        if ($timeRange->containsTime($time)) {
128
            return next($timeRange);
129
        }
130
    }
131
132
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange)
133
    {
134
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
135
            return $timeRange->end();
136
        }
137
    }
138
139
    public function offsetExists($offset): bool
140
    {
141
        return isset($this->openingHours[$offset]);
142
    }
143
144
    public function offsetGet($offset)
145
    {
146
        return $this->openingHours[$offset];
147
    }
148
149
    public function offsetSet($offset, $value)
150
    {
151
        throw NonMutableOffsets::forClass(static::class);
152
    }
153
154
    public function offsetUnset($offset)
155
    {
156
        unset($this->openingHours[$offset]);
157
    }
158
159
    public function count(): int
160
    {
161
        return count($this->openingHours);
162
    }
163
164
    public function getIterator()
165
    {
166
        return new ArrayIterator($this->openingHours);
167
    }
168
169
    public function isEmpty(): bool
170
    {
171
        return empty($this->openingHours);
172
    }
173
174
    public function map(callable $callback): array
175
    {
176
        return Arr::map($this->openingHours, $callback);
177
    }
178
179
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
180
    {
181
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
182
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
183
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
184
            }
185
        }
186
    }
187
188
    public function __toString()
189
    {
190
        $values = [];
191
        foreach ($this->openingHours as $openingHour) {
192
            $values[] = (string) $openingHour;
193
        }
194
195
        return implode(',', $values);
196
    }
197
}
198