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 ( 6da538...94ce16 )
by Sebastian
02:03
created

OpeningHours::guardAgainstInvalidDay()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use DateTime;
6
use Spatie\OpeningHours\Exceptions\InvalidDayName;
7
use Spatie\OpeningHours\Helpers\Arr;
8
9
class OpeningHours
10
{
11
    /** @var \Spatie\OpeningHours\Day[] */
12
    protected $openingHours;
13
14
    /** @var array */
15
    protected $exceptions = [];
16
17
    public function __construct()
18
    {
19
        $this->openingHours = Day::mapDays(function () {
20
            return new OpeningHoursForDay();
21
        });
22
    }
23
24
    /**
25
     * @param array $data
26
     *
27
     * @return static
28
     */
29
    public static function create(array $data)
30
    {
31
        return (new static())->fill($data);
32
    }
33
34
    public function fill(array $data)
35
    {
36
        list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data);
37
38
        foreach ($openingHours as $day => $openingHours) {
39
            $this->setOpeningHoursFromStrings($day, $openingHours);
40
        }
41
42
        $this->setExceptionsFromStrings($exceptions);
43
44
        return $this;
45
    }
46
47
    public function forWeek(): array
48
    {
49
        return $this->openingHours;
50
    }
51
52
    public function forDay(string $day): OpeningHoursForDay
53
    {
54
        $day = $this->normalizeDayName($day);
55
56
        return $this->openingHours[$day];
57
    }
58
59
    public function forDate(DateTime $date): OpeningHoursForDay
60
    {
61
        return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date));
62
    }
63
64
    public function exceptions(): array
65
    {
66
        return $this->exceptions;
67
    }
68
69
    public function isOpenOn(string $day): bool
70
    {
71
        return count($this->forDay($day)) > 0;
72
    }
73
74
    public function isClosedOn(string $day): bool
75
    {
76
        return $this->isOpenOn($day);
77
    }
78
79
    public function isOpenAt(DateTime $dateTime): bool
80
    {
81
        $openingHoursForDay = $this->forDate($dateTime);
82
83
        return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime));
84
    }
85
86
    public function isClosedAt(DateTime $dateTime): bool
87
    {
88
        return ! $this->isOpenAt($dateTime);
89
    }
90
91
    public function isOpen(): bool
92
    {
93
        return $this->isOpenAt(new DateTime());
94
    }
95
96
    public function isClosed(): bool
97
    {
98
        return $this->isClosedAt(new DateTime());
99
    }
100
101
    protected function parseOpeningHoursAndExceptions(array $data): array
102
    {
103
        $exceptions = Arr::pull($data, 'exceptions', []);
104
        $openingHours = [];
105
106
        foreach ($data as $day => $openingHoursData) {
107
            $openingHours[$this->normalizeDayName($day)] = $openingHoursData;
108
        }
109
110
        return [$openingHours, $exceptions];
111
    }
112
113
    protected function setOpeningHoursFromStrings(string $day, array $openingHours)
114
    {
115
        $day = $this->normalizeDayName($day);
116
117
        $this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours);
118
    }
119
120
    protected function setExceptionsFromStrings(array $exceptions)
121
    {
122
        $this->exceptions = array_map(function (array $openingHours) {
123
            return OpeningHoursForDay::fromStrings($openingHours);
124
        }, $exceptions);
125
    }
126
127
    protected function normalizeDayName(string $day)
128
    {
129
        $day = strtolower($day);
130
131
        if (! Day::isValid($day)) {
132
            throw new InvalidDayName();
133
        }
134
135
        return $day;
136
    }
137
}
138