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.

Event::resolveLocationProperties()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.2088
c 0
b 0
f 0
cc 5
nc 3
nop 1
1
<?php
2
3
namespace Spatie\IcalendarGenerator\Components;
4
5
use DateTimeImmutable;
6
use DateTimeInterface;
7
use Spatie\IcalendarGenerator\ComponentPayload;
8
use Spatie\IcalendarGenerator\Enums\Classification;
9
use Spatie\IcalendarGenerator\Enums\EventStatus;
10
use Spatie\IcalendarGenerator\Enums\ParticipationStatus;
11
use Spatie\IcalendarGenerator\PropertyTypes\CalendarAddressPropertyType;
12
use Spatie\IcalendarGenerator\PropertyTypes\CoordinatesPropertyType;
13
use Spatie\IcalendarGenerator\PropertyTypes\Parameter;
14
use Spatie\IcalendarGenerator\ValueObjects\CalendarAddress;
15
16
final class Event extends Component
17
{
18
    /** @var array */
19
    private $alerts = [];
20
21
    /** @var \DateTimeInterface */
22
    private $starts;
23
24
    /** @var \DateTimeInterface */
25
    private $ends;
26
27
    /** @var string */
28
    private $name;
29
30
    /** @var string|null */
31
    private $description;
32
33
    /** @var string|null */
34
    private $address;
35
36
    /** @var string|null */
37
    private $addressName;
38
39
    /** @var float|null */
40
    private $lat;
41
42
    /** @var float|null */
43
    private $lng;
44
45
    /** @var string */
46
    private $uuid;
47
48
    /** @var \DateTimeInterface */
49
    private $created;
50
51
    /** @var bool */
52
    private $withTimezone = false;
53
54
    /** @var bool */
55
    private $isFullDay = false;
56
57
    /** @var \Spatie\IcalendarGenerator\Enums\Classification|null */
58
    private $classification = null;
59
60
    /** @var bool|null */
61
    private $transparent = null;
62
63
    /** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress[] */
64
    private $attendees = [];
65
66
    /** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress|null */
67
    private $organizer = null;
68
69
    /** @var \Spatie\IcalendarGenerator\Enums\EventStatus|null */
70
    private $status = null;
71
72
    /** @var string|null */
73
    private $url;
74
75
    public static function create(string $name = null): Event
76
    {
77
        return new self($name);
78
    }
79
80
    public function __construct(string $name = null)
81
    {
82
        $this->name = $name;
83
        $this->uuid = uniqid();
84
        $this->created = new DateTimeImmutable();
85
    }
86
87
    public function getComponentType(): string
88
    {
89
        return 'EVENT';
90
    }
91
92
    public function getRequiredProperties(): array
93
    {
94
        return [
95
            'UID',
96
            'DTSTAMP',
97
            'DTSTART',
98
        ];
99
    }
100
101
    public function startsAt(DateTimeInterface $starts): Event
102
    {
103
        $this->starts = $starts;
104
105
        return $this;
106
    }
107
108
    public function endsAt(DateTimeInterface $ends): Event
109
    {
110
        $this->ends = $ends;
111
112
        return $this;
113
    }
114
115
    public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event
116
    {
117
        $this->starts = $starts;
118
        $this->ends = $ends;
119
120
        return $this;
121
    }
122
123
    public function name(string $name): Event
124
    {
125
        $this->name = $name;
126
127
        return $this;
128
    }
129
130
    public function description(string $description): Event
131
    {
132
        $this->description = $description;
133
134
        return $this;
135
    }
136
137
    public function address(string $address, string $name = null): Event
138
    {
139
        $this->address = $address;
140
141
        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...
142
            $this->addressName = $name;
143
        }
144
145
        return $this;
146
    }
147
148
    public function addressName(string $name): Event
149
    {
150
        $this->addressName = $name;
151
152
        return $this;
153
    }
154
155
    public function coordinates(float $lat, float $lng): Event
156
    {
157
        $this->lat = $lat;
158
        $this->lng = $lng;
159
160
        return $this;
161
    }
162
163
    public function uniqueIdentifier(string $uid): Event
164
    {
165
        $this->uuid = $uid;
166
167
        return $this;
168
    }
169
170
    public function createdAt(DateTimeInterface $created): Event
171
    {
172
        $this->created = $created;
173
174
        return $this;
175
    }
176
177
    public function withTimezone(): Event
178
    {
179
        $this->withTimezone = true;
180
181
        return $this;
182
    }
183
184
    public function fullDay(): Event
185
    {
186
        $this->isFullDay = true;
187
188
        return $this;
189
    }
190
191
    public function alert(Alert $alert): Event
192
    {
193
        $this->alerts[] = $alert;
194
195
        return $this;
196
    }
197
198
    public function alertMinutesBefore(int $minutes, string $message = null): Event
199
    {
200
        $this->alerts[] = Alert::minutesBeforeStart($minutes, $message);
201
202
        return $this;
203
    }
204
205
    public function alertMinutesAfter(int $minutes, string $message = null): Event
206
    {
207
        $this->alerts[] = Alert::minutesAfterEnd($minutes, $message);
208
209
        return $this;
210
    }
211
212
    public function classification(?Classification $classification): Event
213
    {
214
        $this->classification = $classification;
215
216
        return $this;
217
    }
218
219
    public function transparent(): Event
220
    {
221
        $this->transparent = true;
222
223
        return $this;
224
    }
225
226
    public function attendee(
227
        string $email,
228
        string $name = null,
229
        ParticipationStatus $participationStatus = null
230
    ): Event {
231
        $this->attendees[] = new CalendarAddress($email, $name, $participationStatus);
232
233
        return $this;
234
    }
235
236
    public function organizer(string $email, string $name = null): Event
237
    {
238
        $this->organizer = new CalendarAddress($email, $name);
239
240
        return $this;
241
    }
242
243
    public function status(EventStatus $status): Event
244
    {
245
        $this->status = $status;
246
247
        return $this;
248
    }
249
250
    public function url(string $url): Event
251
    {
252
        $this->url = $url;
253
254
        return $this;
255
    }
256
257
    protected function payload(): ComponentPayload
258
    {
259
        $payload = ComponentPayload::create($this->getComponentType())
260
            ->textProperty('UID', $this->uuid)
261
            ->textProperty('SUMMARY', $this->name)
262
            ->textProperty('DESCRIPTION', $this->description)
263
            ->textProperty('LOCATION', $this->address)
264
            ->textProperty('CLASS', $this->classification)
265
            ->textProperty('TRANSP', $this->transparent ? 'TRANSPARENT' : null)
266
            ->textProperty('STATUS', $this->status)
267
            ->uriProperty('URL', $this->url)
268
            ->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone)
269
            ->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone)
270
            ->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone)
271
            ->subComponent(...$this->alerts);
272
273
        if ($this->organizer) {
274
            $payload->property(CalendarAddressPropertyType::create('ORGANIZER', $this->organizer));
275
        }
276
277
        foreach ($this->attendees as $attendee) {
278
            $payload->property(CalendarAddressPropertyType::create('ATTENDEE', $attendee));
279
        }
280
281
        $payload = $this->resolveLocationProperties($payload);
282
283
        return $payload;
284
    }
285
286
    private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload
287
    {
288
        if (is_null($this->lng) && is_null($this->lat)) {
289
            return $payload;
290
        }
291
292
        $payload->property(CoordinatesPropertyType::create('GEO', $this->lat, $this->lng));
293
294
        if (is_null($this->address) || is_null($this->addressName)) {
295
            return $payload;
296
        }
297
298
        $property = CoordinatesPropertyType::create(
299
            'X-APPLE-STRUCTURED-LOCATION',
300
            $this->lat,
301
            $this->lng
302
        )->addParameter(Parameter::create('VALUE', 'URI'))
303
            ->addParameter(Parameter::create('X-ADDRESS', $this->address))
304
            ->addParameter(Parameter::create('X-APPLE-RADIUS', '72'))
305
            ->addParameter(Parameter::create('X-TITLE', $this->addressName));
306
307
        $payload->property($property);
308
309
        return $payload;
310
    }
311
}
312