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 (#7)
by Ankur
01:04
created

Event::recurrenceRule()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\IcalendarGenerator\Components;
4
5
use DateInterval;
6
use DateTimeImmutable;
7
use DateTimeInterface;
8
use Spatie\IcalendarGenerator\ComponentPayload;
9
use Spatie\IcalendarGenerator\PropertyTypes\CoordinatesPropertyType;
10
use Spatie\IcalendarGenerator\PropertyTypes\Parameter;
11
12
final class Event extends Component
13
{
14
    /** @var array */
15
    private $alerts = [];
16
17
    /** @var \DateTimeInterface */
18
    private $starts;
19
20
    /** @var \DateTimeInterface */
21
    private $ends;
22
23
    /** @var string */
24
    private $name;
25
26
    /** @var string|null */
27
    private $description;
28
29
    /** @var string|null */
30
    private $address;
31
32
    /** @var string|null */
33
    private $addressName;
34
35
    /** @var float|null */
36
    private $lat;
37
38
    /** @var float|null */
39
    private $lng;
40
41
    /** @var string */
42
    private $uuid;
43
44
    /** @var \DateTimeInterface */
45
    private $created;
46
47
    /** @var bool */
48
    private $withTimezone = false;
49
50
    /** @var bool */
51
    private $isFullDay = false;
52
53
    /** @var string */
54
    private $recurrenceRule;
55
56
    public static function create(string $name = null): Event
57
    {
58
        return new self($name);
59
    }
60
61
    public function __construct(string $name = null)
62
    {
63
        $this->name = $name;
64
        $this->uuid = uniqid();
65
        $this->created = new DateTimeImmutable();
66
    }
67
68
    public function getComponentType(): string
69
    {
70
        return 'EVENT';
71
    }
72
73
    public function getRequiredProperties(): array
74
    {
75
        return [
76
            'UID',
77
            'DTSTAMP',
78
            'DTSTART',
79
        ];
80
    }
81
82
    public function startsAt(DateTimeInterface $starts): Event
83
    {
84
        $this->starts = $starts;
85
86
        return $this;
87
    }
88
89
    public function endsAt(DateTimeInterface $ends): Event
90
    {
91
        $this->ends = $ends;
92
93
        return $this;
94
    }
95
96
    public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event
97
    {
98
        $this->starts = $starts;
99
        $this->ends = $ends;
100
101
        return $this;
102
    }
103
104
    public function name(string $name): Event
105
    {
106
        $this->name = $name;
107
108
        return $this;
109
    }
110
111
    public function description(string $description): Event
112
    {
113
        $this->description = $description;
114
115
        return $this;
116
    }
117
118
    public function address(string $address, string $name = null): Event
119
    {
120
        $this->address = $address;
121
122
        if ($name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
123
            $this->addressName = $name;
124
        }
125
126
        return $this;
127
    }
128
129
    public function addressName(string $name): Event
130
    {
131
        $this->addressName = $name;
132
133
        return $this;
134
    }
135
136
    public function coordinates(float $lat, float $lng): Event
137
    {
138
        $this->lat = $lat;
139
        $this->lng = $lng;
140
141
        return $this;
142
    }
143
144
    public function uniqueIdentifier(string $uid): Event
145
    {
146
        $this->uuid = $uid;
147
148
        return $this;
149
    }
150
151
    public function createdAt(DateTimeInterface $created): Event
152
    {
153
        $this->created = $created;
154
155
        return $this;
156
    }
157
158
    public function withTimezone(): Event
159
    {
160
        $this->withTimezone = true;
161
162
        return $this;
163
    }
164
165
    public function fullDay(): Event
166
    {
167
        $this->isFullDay = true;
168
169
        return $this;
170
    }
171
172
    public function alertMinutesBefore(int $minutes, string $message = null): Event
173
    {
174
        $trigger = clone $this->starts;
175
176
        $this->alerts[] = Alert::create(
177
            $trigger->sub(new DateInterval("PT{$minutes}M")),
178
            $message ?? $this->name
179
        );
180
181
        return $this;
182
    }
183
184
    public function recurrenceRule(string $rule): Event
185
    {
186
        $this->recurrenceRule = $rule;
187
188
        return $this;
189
    }
190
191
    public function getPayload(): ComponentPayload
192
    {
193
        $payload = ComponentPayload::create($this->getComponentType())
194
            ->textProperty('UID', $this->uuid)
195
            ->textProperty('SUMMARY', $this->name)
196
            ->textProperty('DESCRIPTION', $this->description)
197
            ->textProperty('LOCATION', $this->address)
198
            ->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone)
199
            ->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone)
200
            ->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone)
201
            ->textProperty('RRULE', $this->recurrenceRule)
202
            ->subComponent(...$this->alerts);
203
204
        $payload = $this->resolveLocationProperties($payload);
205
206
        return $payload;
207
    }
208
209
    private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload
210
    {
211
        if (is_null($this->lng) && is_null($this->lat)) {
212
            return $payload;
213
        }
214
215
        $payload->property(CoordinatesPropertyType::create('GEO', $this->lat, $this->lng));
216
217
        if (is_null($this->address)) {
218
            return $payload;
219
        }
220
221
        $property = CoordinatesPropertyType::create(
222
            'X-APPLE-STRUCTURED-LOCATION', $this->lat, $this->lng
223
        )->addParameter(Parameter::create('VALUE', 'URI'))
224
            ->addParameter(Parameter::create('X-ADDRESS', $this->address))
225
            ->addParameter(Parameter::create('X-APPLE-RADIUS', 72))
226
            ->addParameter(Parameter::create('X-TITLE', $this->addressName));
227
228
        $payload->property($property);
229
230
        return $payload;
231
    }
232
}
233