Google::generate()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 8
nop 1
1
<?php
2
3
namespace Spatie\CalendarLinks\Generators;
4
5
use Spatie\CalendarLinks\Generator;
6
use Spatie\CalendarLinks\Link;
7
8
/**
9
 * @see https://github.com/InteractionDesignFoundation/add-event-to-calendar-docs/blob/master/services/google.md
10
 */
11
class Google implements Generator
12
{
13
    /** @var string {@see https://www.php.net/manual/en/function.date.php} */
14
    protected $dateFormat = 'Ymd';
15
    protected $dateTimeFormat = 'Ymd\THis';
16
17
    /** {@inheritdoc} */
18
    public function generate(Link $link): string
19
    {
20
        $url = 'https://calendar.google.com/calendar/render?action=TEMPLATE';
21
22
        $dateTimeFormat = $link->allDay ? $this->dateFormat : $this->dateTimeFormat;
23
        $url .= '&dates='.$link->from->format($dateTimeFormat).'/'.$link->to->format($dateTimeFormat);
24
        $url .= '&ctz='.$link->from->getTimezone()->getName();
25
26
        $url .= '&text='.urlencode($link->title);
27
28
        if ($link->description) {
29
            $url .= '&details='.urlencode($link->description);
30
        }
31
32
        if ($link->address) {
33
            $url .= '&location='.urlencode($link->address);
34
        }
35
36
        $url .= '&sprop=&sprop=name:';
37
38
        return $url;
39
    }
40
}
41