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 (#21)
by Ivan
02:42
created

OpeningHoursForDay::nextOpen()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 7
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use Countable;
8
use IteratorAggregate;
9
use Spatie\OpeningHours\Exceptions\OverlappingTimeRanges;
10
use Spatie\OpeningHours\Helpers\Arr;
11
12
class OpeningHoursForDay implements ArrayAccess, Countable, IteratorAggregate
13
{
14
    /** @var array */
15
    protected $openingHours = [];
16
17
    public static function fromStrings(array $strings)
18
    {
19
        $openingHoursForDay = new static();
20
21
        $timeRanges = Arr::map($strings, function ($string) {
22
            return TimeRange::fromString($string);
23
        });
24
25
        $openingHoursForDay->guardAgainstTimeRangeOverlaps($timeRanges);
26
27
        $openingHoursForDay->openingHours = $timeRanges;
28
29
        return $openingHoursForDay;
30
    }
31
32
    public function isOpenAt(Time $time)
33
    {
34
        foreach ($this->openingHours as $timeRange) {
35
            if ($timeRange->containsTime($time)) {
36
                return true;
37
            }
38
        }
39
40
        return false;
41
    }
42
43
    public function nextOpen(Time $time)
44
    {
45
        $prevTimeRange = false;
46
        $workingRange = false;
47
48
        foreach ($this->openingHours as $timeRange) {
49
            if ($workingRange) {
50
                return $timeRange->start();
51
            }
52
53
            if ($timeRange->containsTime($time)) {
54
                $workingRange = $timeRange;
55
                continue;
56
            }
57
58
            $timeOffRange = $prevTimeRange ?
59
                TimeRange::fromString($prevTimeRange->end().'-'.$timeRange->start()) :
60
                TimeRange::fromString('00:00-'.$timeRange->start())
61
            ;
62
63
            if ($timeOffRange->containsTime($time)) {
64
                return $timeRange->start();
65
            }
66
67
            $prevTimeRange = $timeRange;
68
        }
69
70
        return false;
71
    }
72
73
    public function offsetExists($offset): bool
74
    {
75
        return isset($this->openingHours[$offset]);
76
    }
77
78
    public function offsetGet($offset)
79
    {
80
        return $this->openingHours[$offset];
81
    }
82
83
    public function offsetSet($offset, $value)
84
    {
85
        throw new \Exception();
86
    }
87
88
    public function offsetUnset($offset)
89
    {
90
        unset($this->openingHours[$offset]);
91
    }
92
93
    public function count(): int
94
    {
95
        return count($this->openingHours);
96
    }
97
98
    public function getIterator()
99
    {
100
        return new ArrayIterator($this->openingHours);
101
    }
102
103
    protected function guardAgainstTimeRangeOverlaps(array $openingHours)
104
    {
105
        foreach (Arr::createUniquePairs($openingHours) as $timeRanges) {
106
            if ($timeRanges[0]->overlaps($timeRanges[1])) {
107
                throw OverlappingTimeRanges::forRanges($timeRanges[0], $timeRanges[1]);
108
            }
109
        }
110
    }
111
}
112