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 (#91)
by Kyle
01:22
created

OpeningHoursForDay::findNextOpenInWorkingHours()   A

Complexity

Conditions 4
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 4
nc 2
nop 2
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
    /**
57
     * @param callable[] $filters
58
     *
59
     * @return Time|bool
60
     */
61
    public function openingHoursFilter(array $filters)
62
    {
63
        foreach ($this->openingHours as $timeRange) {
64
            foreach ($filters as $filter) {
65
                if ($result = $filter($timeRange)) {
66
                    reset($timeRange);
67
68
                    return $result;
69
                }
70
            }
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * @param Time $time
78
     *
79
     * @return bool|Time
80
     */
81
    public function nextOpen(Time $time)
82
    {
83
        return $this->openingHoursFilter([
84
            function ($timeRange) use ($time) {
85
                return $this->findNextOpenInFreeTime($time, $timeRange);
86
            },
87
        ]);
88
    }
89
90
    /**
91
     * @param Time $time
92
     *
93
     * @return bool|Time
94
     */
95
    public function nextClose(Time $time)
96
    {
97
        return $this->openingHoursFilter([
98
            function ($timeRange) use ($time) {
99
                return $this->findNextCloseInWorkingHours($time, $timeRange);
100
            },
101
            function ($timeRange) use ($time) {
102
                return $this->findNextCloseInFreeTime($time, $timeRange);
103
            },
104
        ]);
105
    }
106
107
    protected function findNextOpenInFreeTime(Time $time, TimeRange $timeRange)
108
    {
109
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
110
            return $timeRange->start();
111
        }
112
    }
113
114
    protected function findNextCloseInWorkingHours(Time $time, TimeRange $timeRange)
115
    {
116
        if ($timeRange->containsTime($time)) {
117
            return next($timeRange);
118
        }
119
    }
120
121
    protected function findNextCloseInFreeTime(Time $time, TimeRange $timeRange)
122
    {
123
        if (TimeRange::fromString('00:00-'.$timeRange->start())->containsTime($time)) {
124
            return $timeRange->end();
125
        }
126
    }
127
128
    public function offsetExists($offset): bool
129
    {
130
        return isset($this->openingHours[$offset]);
131
    }
132
133
    public function offsetGet($offset)
134
    {
135
        return $this->openingHours[$offset];
136
    }
137
138
    public function offsetSet($offset, $value)
139
    {
140
        throw NonMutableOffsets::forClass(static::class);
141
    }
142
143
    public function offsetUnset($offset)
144
    {
145
        unset($this->openingHours[$offset]);
146
    }
147
148
    public function count(): int
149
    {
150
        return count($this->openingHours);
151
    }
152
153
    public function getIterator()
154
    {
155
        return new ArrayIterator($this->openingHours);
156
    }
157
158
    public function isEmpty(): bool
159
    {
160
        return empty($this->openingHours);
161
    }
162
163
    public function map(callable $callback): array
164
    {
165
        return Arr::map($this->openingHours, $callback);
166
    }
167
168
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
169
    {
170
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
171
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
172
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
173
            }
174
        }
175
    }
176
177
    public function __toString()
178
    {
179
        $values = [];
180
        foreach ($this->openingHours as $openingHour) {
181
            $values[] = (string) $openingHour;
182
        }
183
184
        return implode(',', $values);
185
    }
186
}
187