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 (#16)
by
unknown
02:30
created

OpeningHours::setTimezone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Spatie\OpeningHours;
4
5
use DateTime;
6
use DateTimeInterface;
7
use DateTimeZone;
8
use Spatie\OpeningHours\Exceptions\Exception;
9
use Spatie\OpeningHours\Exceptions\InvalidDate;
10
use Spatie\OpeningHours\Exceptions\InvalidDayName;
11
use Spatie\OpeningHours\Helpers\Arr;
12
13
class OpeningHours
14
{
15
    /** @var \Spatie\OpeningHours\Day[] */
16
    protected $openingHours;
17
18
    /** @var array */
19
    protected $exceptions = [];
20
21
    /** @var DateTimeZone|null */
22
    protected $timezone;
23
24
    public function __construct($timezone = null)
25
    {
26
        $this->timezone = $timezone ? new DateTimeZone($timezone) : null;
27
28
        $this->openingHours = Day::mapDays(function () {
29
            return new OpeningHoursForDay();
30
        });
31
    }
32
33
    /**
34
     * @param array $data
35
     *
36
     * @return static
37
     */
38
    public static function create(array $data)
39
    {
40
        return (new static())->fill($data);
41
    }
42
43
    /**
44
     * @param array $data
45
     *
46
     * @return bool
47
     */
48
    public static function isValid(array $data): bool
49
    {
50
        try {
51
            static::create($data);
52
53
            return true;
54
        } catch (Exception $exception) {
55
            return false;
56
        }
57
    }
58
59
    public function fill(array $data)
60
    {
61
        list($openingHours, $exceptions) = $this->parseOpeningHoursAndExceptions($data);
62
63
        foreach ($openingHours as $day => $openingHoursForThisDay) {
64
            $this->setOpeningHoursFromStrings($day, $openingHoursForThisDay);
65
        }
66
67
        $this->setExceptionsFromStrings($exceptions);
68
69
        return $this;
70
    }
71
72
    public function forWeek(): array
73
    {
74
        return $this->openingHours;
75
    }
76
77
    public function forDay(string $day): OpeningHoursForDay
78
    {
79
        $day = $this->normalizeDayName($day);
80
81
        return $this->openingHours[$day];
82
    }
83
84
    public function forDate(DateTimeInterface $date): OpeningHoursForDay
85
    {
86
        $date = $this->setTimezone($date);
87
88
        return $this->exceptions[$date->format('Y-m-d')] ?? $this->forDay(Day::onDateTime($date));
89
    }
90
91
    public function exceptions(): array
92
    {
93
        return $this->exceptions;
94
    }
95
96
    public function isOpenOn(string $day): bool
97
    {
98
        return count($this->forDay($day)) > 0;
99
    }
100
101
    public function isClosedOn(string $day): bool
102
    {
103
        return $this->isOpenOn($day);
104
    }
105
106
    public function isOpenAt(DateTimeInterface $dateTime): bool
107
    {
108
        $dateTime = $this->setTimezone($dateTime);
109
110
        $openingHoursForDay = $this->forDate($dateTime);
111
112
        return $openingHoursForDay->isOpenAt(Time::fromDateTime($dateTime));
113
    }
114
115
    public function isClosedAt(DateTimeInterface $dateTime): bool
116
    {
117
        $dateTime = $this->setTimezone($dateTime);
118
119
        return ! $this->isOpenAt($dateTime);
120
    }
121
122
    public function isOpen(): bool
123
    {
124
        return $this->isOpenAt(new DateTime());
125
    }
126
127
    public function isClosed(): bool
128
    {
129
        return $this->isClosedAt(new DateTime());
130
    }
131
132
    protected function parseOpeningHoursAndExceptions(array $data): array
133
    {
134
        $exceptions = Arr::pull($data, 'exceptions', []);
135
        $openingHours = [];
136
137
        foreach ($data as $day => $openingHoursData) {
138
            $openingHours[$this->normalizeDayName($day)] = $openingHoursData;
139
        }
140
141
        return [$openingHours, $exceptions];
142
    }
143
144
    protected function setOpeningHoursFromStrings(string $day, array $openingHours)
145
    {
146
        $day = $this->normalizeDayName($day);
147
148
        $this->openingHours[$day] = OpeningHoursForDay::fromStrings($openingHours);
149
    }
150
151
    protected function setExceptionsFromStrings(array $exceptions)
152
    {
153
        $this->exceptions = Arr::map($exceptions, function (array $openingHours, string $date) {
154
            $dateTime = DateTime::createFromFormat('Y-m-d', $date);
155
156
            if ($dateTime === false || $dateTime->format('Y-m-d') !== $date) {
157
                throw InvalidDate::invalidDate($date);
158
            }
159
160
            return OpeningHoursForDay::fromStrings($openingHours);
161
        });
162
    }
163
164
    protected function normalizeDayName(string $day)
165
    {
166
        $day = strtolower($day);
167
168
        if (! Day::isValid($day)) {
169
            throw new InvalidDayName();
170
        }
171
172
        return $day;
173
    }
174
175
    protected function setTimezone(DateTimeInterface $date)
176
    {
177
        if ($this->timezone) {
178
            $date->setTimezone($this->timezone);
179
        }
180
181
        return $date;
182
    }
183
}
184