Passed
Push — main ( 52a7ec...5410b3 )
by Torben
03:32
created

OnlineCalendarViewHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 47
c 4
b 0
f 0
dl 0
loc 90
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 18 2
A getMicrosoftCalendarLink() 0 18 2
A getGoogleCalendarLink() 0 17 2
A initializeArguments() 0 5 1
A getYahooCalendarLink() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\ViewHelpers\Uri;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Event;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
16
17
/**
18
 * ViewHelper to render a link to add an event to a given online calender
19
 * Currently supports the following online calendar types:
20
 *
21
 * - Google Calendar
22
 * - Outlook Calendar
23
 * - Office 365 Calendar
24
 * - Yahoo Calendar
25
 */
26
class OnlineCalendarViewHelper extends AbstractViewHelper
27
{
28
    /**
29
     * Initialize arguments
30
     */
31
    public function initializeArguments(): void
32
    {
33
        parent::initializeArguments();
34
        $this->registerArgument('type', 'string', 'The type of online calender', true, 'google');
35
        $this->registerArgument('event', 'DERHANSEN\SfEventMgt\Domain\Model\Event', 'The event');
36
    }
37
38
    public function render(): string
39
    {
40
        /** @var Event $event */
41
        $event = $this->arguments['event'];
42
        $type = strtolower($this->arguments['type']);
43
44
        // If event has no enddate, set a default enddate (startdate + 1 hour)
45
        if (!$event->getEnddate()) {
46
            $enddate = (clone $event->getStartdate())->modify('+1 hour');
47
            $event->setEnddate($enddate);
48
        }
49
50
        return match ($type) {
51
            'google' => self::getGoogleCalendarLink($event),
52
            'outlook' => self::getMicrosoftCalendarLink($event, 'live'),
53
            'office365' => self::getMicrosoftCalendarLink($event, 'office'),
54
            'yahoo' => self::getYahooCalendarLink($event),
55
            default => '',
56
        };
57
    }
58
59
    private static function getGoogleCalendarLink(Event $event): string
60
    {
61
        $baseLink = 'https://www.google.com/calendar/render?';
62
63
        $dateFormat = 'Ymd\\THi00\\ZO';
64
        $arguments = [
65
            'action' => 'TEMPLATE',
66
            'text' => $event->getTitle(),
67
            'dates' => $event->getStartdate()->format($dateFormat) . '/' . $event->getEnddate()->format($dateFormat),
68
            'details' => strip_tags($event->getDescription()),
69
        ];
70
71
        if ($event->getLocation()) {
72
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
73
        }
74
75
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
76
    }
77
78
    private static function getMicrosoftCalendarLink(Event $event, string $product): string
79
    {
80
        $baseLink = 'https://outlook.' . $product . '.com/calendar/0/deeplink/compose?';
81
82
        $dateFormat = 'Y-m-d\\TH:i:00O';
83
        $arguments = [
84
            'subject' => $event->getTitle(),
85
            'startdt' => $event->getStartdate()->format($dateFormat),
86
            'enddt' => $event->getEnddate()->format($dateFormat),
87
            'body' => strip_tags($event->getDescription()),
88
            'path' => '/calendar/action/compose&rru=addevent',
89
        ];
90
91
        if ($event->getLocation()) {
92
            $arguments['location'] = $event->getLocation()->getFullAddress(', ');
93
        }
94
95
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
96
    }
97
98
    private static function getYahooCalendarLink(Event $event): string
99
    {
100
        $baseLink = 'https://calendar.yahoo.com/?';
101
102
        $dateFormat = 'Ymd\\THi00\\ZO';
103
        $arguments = [
104
            'title' => $event->getTitle(),
105
            'st' => $event->getStartdate()->format($dateFormat),
106
            'et' => $event->getEnddate()->format($dateFormat),
107
            'desc' => strip_tags($event->getDescription()),
108
            'v' => 60,
109
        ];
110
111
        if ($event->getLocation()) {
112
            $arguments['in_loc'] = $event->getLocation()->getFullAddress(', ');
113
        }
114
115
        return $baseLink . http_build_query($arguments, '', '&', PHP_QUERY_RFC3986);
116
    }
117
}
118