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 ( c772cd...2be121 )
by Ruben
01:17
created

Event::coordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Parameter;
10
use Spatie\IcalendarGenerator\PropertyTypes\TextPropertyType;
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
    public static function create(string $name = null): Event
54
    {
55
        return new self($name);
56
    }
57
58
    public function __construct(string $name = null)
59
    {
60
        $this->name = $name;
61
        $this->uuid = uniqid();
62
        $this->created = new DateTimeImmutable();
63
    }
64
65
    public function getComponentType(): string
66
    {
67
        return 'EVENT';
68
    }
69
70
    public function getRequiredProperties(): array
71
    {
72
        return [
73
            'UID',
74
            'DTSTAMP',
75
            'DTSTART',
76
        ];
77
    }
78
79
    public function startsAt(DateTimeInterface $starts): Event
80
    {
81
        $this->starts = $starts;
82
83
        return $this;
84
    }
85
86
    public function endsAt(DateTimeInterface $ends): Event
87
    {
88
        $this->ends = $ends;
89
90
        return $this;
91
    }
92
93
    public function period(DateTimeInterface $starts, DateTimeInterface $ends): Event
94
    {
95
        $this->starts = $starts;
96
        $this->ends = $ends;
97
98
        return $this;
99
    }
100
101
    public function name(string $name): Event
102
    {
103
        $this->name = $name;
104
105
        return $this;
106
    }
107
108
    public function description(string $description): Event
109
    {
110
        $this->description = $description;
111
112
        return $this;
113
    }
114
115
    public function address(string $address, string $name = null): Event
116
    {
117
        $this->address = $address;
118
119
        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...
120
            $this->addressName = $name;
121
        }
122
123
        return $this;
124
    }
125
126
    public function addressName(string $name): Event
127
    {
128
        $this->addressName = $name;
129
130
        return $this;
131
    }
132
133
    public function coordinates(float $lat, float $lng): Event
134
    {
135
        $this->lat = $lat;
136
        $this->lng = $lng;
137
138
        return $this;
139
    }
140
141
    public function uniqueIdentifier(string $uid): Event
142
    {
143
        $this->uuid = $uid;
144
145
        return $this;
146
    }
147
148
    public function createdAt(DateTimeInterface $created): Event
149
    {
150
        $this->created = $created;
151
152
        return $this;
153
    }
154
155
    public function withTimezone(): Event
156
    {
157
        $this->withTimezone = true;
158
159
        return $this;
160
    }
161
162
    public function fullDay(): Event
163
    {
164
        $this->isFullDay = true;
165
166
        return $this;
167
    }
168
169
    public function alertMinutesBefore(int $minutes, string $message = null): Event
170
    {
171
        $trigger = clone $this->starts;
172
173
        $this->alerts[] = Alert::create(
174
            $trigger->sub(new DateInterval("PT{$minutes}M")),
175
            $message ?? $this->name
176
        );
177
178
        return $this;
179
    }
180
181
    public function getPayload(): ComponentPayload
182
    {
183
        $payload = ComponentPayload::create($this->getComponentType())
184
            ->textProperty('UID', $this->uuid)
185
            ->textProperty('SUMMARY', $this->name)
186
            ->textProperty('DESCRIPTION', $this->description)
187
            ->textProperty('LOCATION', $this->address)
188
            ->dateTimeProperty('DTSTART', $this->starts, ! $this->isFullDay, $this->withTimezone)
189
            ->dateTimeProperty('DTEND', $this->ends, ! $this->isFullDay, $this->withTimezone)
190
            ->dateTimeProperty('DTSTAMP', $this->created, true, $this->withTimezone)
191
            ->subComponent(...$this->alerts);
192
193
        $payload = $this->resolveLocationProperties($payload);
194
195
        return $payload;
196
    }
197
198
    private function resolveLocationProperties(ComponentPayload $payload): ComponentPayload
199
    {
200
        if (is_null($this->lng) && is_null($this->lat)) {
201
            return $payload;
202
        }
203
204
        $payload->textProperty('GEO', "{$this->lat};{$this->lng}");
205
206
        if (is_null($this->address)) {
207
            return $payload;
208
        }
209
210
        $property = TextPropertyType::create(
211
            'X-APPLE-STRUCTURED-LOCATION', "{$this->lat};{$this->lng}"
212
        )->addParameter(Parameter::create('VALUE', 'URI'))
213
            ->addParameter(Parameter::create('X-ADDRESS', $this->address))
214
            ->addParameter(Parameter::create('X-APPLE-RADIUS', 72))
215
            ->addParameter(Parameter::create('X-TITLE', $this->addressName));
216
217
        $payload->property($property);
218
219
        return $payload;
220
    }
221
}
222