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 ( fccba7...4794e6 )
by Ruben
01:28
created

Event::attendee()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\ParticipationStatus;
10
use Spatie\IcalendarGenerator\PropertyTypes\CalendarAddressPropertyType;
11
use Spatie\IcalendarGenerator\PropertyTypes\CoordinatesPropertyType;
12
use Spatie\IcalendarGenerator\PropertyTypes\Parameter;
13
use Spatie\IcalendarGenerator\ValueObjects\CalendarAddress;
14
15
final class Event extends Component
16
{
17
    /** @var array */
18
    private $alerts = [];
19
20
    /** @var \DateTimeInterface */
21
    private $starts;
22
23
    /** @var \DateTimeInterface */
24
    private $ends;
25
26
    /** @var string */
27
    private $name;
28
29
    /** @var string|null */
30
    private $description;
31
32
    /** @var string|null */
33
    private $address;
34
35
    /** @var string|null */
36
    private $addressName;
37
38
    /** @var float|null */
39
    private $lat;
40
41
    /** @var float|null */
42
    private $lng;
43
44
    /** @var string */
45
    private $uuid;
46
47
    /** @var \DateTimeInterface */
48
    private $created;
49
50
    /** @var bool */
51
    private $withTimezone = false;
52
53
    /** @var bool */
54
    private $isFullDay = false;
55
56
    /** @var \Spatie\IcalendarGenerator\Enums\Classification|null */
57
    private $classification = null;
58
59
    /** @var bool|null */
60
    private $transparent = null;
61
62
    /** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress[] */
63
    private $attendees = [];
64
65
    /** @var \Spatie\IcalendarGenerator\ValueObjects\CalendarAddress|null */
66
    private $organizer = null;
67
68
    public static function create(string $name = null): Event
69
    {
70
        return new self($name);
71
    }
72
73
    public function __construct(string $name = null)
74
    {
75
        $this->name = $name;
76
        $this->uuid = uniqid();
77
        $this->created = new DateTimeImmutable();
78
    }
79
80
    public function getComponentType(): string
81
    {
82
        return 'EVENT';
83
    }
84
85
    public function getRequiredProperties(): array
86
    {
87
        return [
88
            'UID',
89
            'DTSTAMP',
90
            'DTSTART',
91
        ];
92
    }
93
94
    public function startsAt(DateTimeInterface $starts): Event
95
    {
96
        $this->starts = $starts;
97
98
        return $this;
99
    }
100
101
    public function endsAt(DateTimeInterface $ends): Event
102
    {
103
        $this->ends = $ends;
104
105
        return $this;
106
    }
107
108
    public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event
109
    {
110
        $this->starts = $starts;
111
        $this->ends = $ends;
112
113
        return $this;
114
    }
115
116
    public function name(string $name): Event
117
    {
118
        $this->name = $name;
119
120
        return $this;
121
    }
122
123
    public function description(string $description): Event
124
    {
125
        $this->description = $description;
126
127
        return $this;
128
    }
129
130
    public function address(string $address, string $name = null): Event
131
    {
132
        $this->address = $address;
133
134
        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...
135
            $this->addressName = $name;
136
        }
137
138
        return $this;
139
    }
140
141
    public function addressName(string $name): Event
142
    {
143
        $this->addressName = $name;
144
145
        return $this;
146
    }
147
148
    public function coordinates(float $lat, float $lng): Event
149
    {
150
        $this->lat = $lat;
151
        $this->lng = $lng;
152
153
        return $this;
154
    }
155
156
    public function uniqueIdentifier(string $uid): Event
157
    {
158
        $this->uuid = $uid;
159
160
        return $this;
161
    }
162
163
    public function createdAt(DateTimeInterface $created): Event
164
    {
165
        $this->created = $created;
166
167
        return $this;
168
    }
169
170
    public function withTimezone(): Event
171
    {
172
        $this->withTimezone = true;
173
174
        return $this;
175
    }
176
177
    public function fullDay(): Event
178
    {
179
        $this->isFullDay = true;
180
181
        return $this;
182
    }
183
184
    public function alert(Alert $alert): Event
185
    {
186
        $this->alerts[] = $alert;
187
188
        return $this;
189
    }
190
191
    public function alertMinutesBefore(int $minutes, string $message = null): Event
192
    {
193
        $this->alerts[] = Alert::minutesBeforeStart($minutes, $message);
194
195
        return $this;
196
    }
197
198
    public function alertMinutesAfter(int $minutes, string $message = null): Event
199
    {
200
        $this->alerts[] = Alert::minutesAfterEnd($minutes, $message);
201
202
        return $this;
203
    }
204
205
    public function classification(?Classification $classification): Event
206
    {
207
        $this->classification = $classification;
208
209
        return $this;
210
    }
211
212
    public function transparent(): Event
213
    {
214
        $this->transparent = true;
215
216
        return $this;
217
    }
218
219
    public function attendee(
220
        string $email,
221
        string $name = null,
222
        ParticipationStatus $participationStatus = null
223
    ): Event {
224
        $this->attendees[] = new CalendarAddress($email, $name, $participationStatus);
225
226
        return $this;
227
    }
228
229
    public function organizer(string $email, string $name = null): Event
230
    {
231
        $this->organizer = new CalendarAddress($email, $name);
232
233
        return $this;
234
    }
235
236
    protected function payload(): ComponentPayload
237
    {
238
        $payload = ComponentPayload::create($this->getComponentType())
239
            ->textProperty('UID', $this->uuid)
240
            ->textProperty('SUMMARY', $this->name)
241
            ->textProperty('DESCRIPTION', $this->description)
242
            ->textProperty('LOCATION', $this->address)
243
            ->textProperty('CLASS', $this->classification)
0 ignored issues
show
Bug introduced by
It seems like $this->classification can also be of type object<Spatie\IcalendarG...r\Enums\Classification>; however, Spatie\IcalendarGenerato...Payload::textProperty() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
244
            ->textProperty('TRANSP', $this->transparent ? 'TRANSPARENT' : null)
245
            ->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone)
246
            ->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone)
247
            ->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone)
248
            ->subComponent(...$this->alerts);
249
250
        if ($this->organizer) {
251
            $payload->property(CalendarAddressPropertyType::create('ORGANIZER', $this->organizer));
252
        }
253
254
        foreach ($this->attendees as $attendee) {
255
            $payload->property(CalendarAddressPropertyType::create('ATTENDEE', $attendee));
256
        }
257
258
        $payload = $this->resolveLocationProperties($payload);
259
260
        return $payload;
261
    }
262
263
    private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload
264
    {
265
        if (is_null($this->lng) && is_null($this->lat)) {
266
            return $payload;
267
        }
268
269
        $payload->property(CoordinatesPropertyType::create('GEO', $this->lat, $this->lng));
270
271
        if (is_null($this->address)) {
272
            return $payload;
273
        }
274
275
        $property = CoordinatesPropertyType::create(
276
            'X-APPLE-STRUCTURED-LOCATION',
277
            $this->lat,
278
            $this->lng
279
        )->addParameter(Parameter::create('VALUE', 'URI'))
280
            ->addParameter(Parameter::create('X-ADDRESS', $this->address))
281
            ->addParameter(Parameter::create('X-APPLE-RADIUS', 72))
282
            ->addParameter(Parameter::create('X-TITLE', $this->addressName));
283
284
        $payload->property($property);
285
286
        return $payload;
287
    }
288
}
289