Completed
Pull Request — master (#64)
by Alies
01:37
created

WebOutlook::generate()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.4906
c 0
b 0
f 0
cc 7
nc 64
nop 1
1
<?php
2
3
namespace Spatie\CalendarLinks\Generators;
4
5
use DateTimeZone;
6
use Spatie\CalendarLinks\Link;
7
use Spatie\CalendarLinks\Generator;
8
9
/**
10
 * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/outlook-live.md
11
 */
12
class WebOutlook implements Generator
13
{
14
    /** {@inheritdoc} */
15
    public function generate(Link $link): string
16
    {
17
        $url = 'https://outlook.live.com/owa/?path=/calendar/action/compose&rru=addevent';
18
19
        $dateTimeFormat = $link->allDay ? 'Ymd' : "Ymd\THis";
20
        $utcStartDateTime = (clone $link->from)->setTimezone(new DateTimeZone('UTC'));
21
        $utcEndDateTime = (clone $link->to)->setTimezone(new DateTimeZone('UTC'));
22
        $url .= '&startdt='.$utcStartDateTime->format($dateTimeFormat);
23
24
        $isSingleDayEvent = $link->to->diff($link->from)->d < 2;
25
        $canOmitEndDateTime = $link->allDay && $isSingleDayEvent;
26
        if (! $canOmitEndDateTime) {
27
            $url .= '&enddt='.$utcEndDateTime->format($dateTimeFormat);
28
        }
29
30
        if ($link->allDay) {
31
            $url .= '&allday=true';
32
        }
33
34
        $url .= '&subject='.urlencode($link->title);
35
36
        if ($link->description) {
37
            $url .= '&body='.urlencode($link->description);
38
        }
39
40
        if ($link->address) {
41
            $url .= '&location='.urlencode($link->address);
42
        }
43
44
        return $url;
45
    }
46
}
47