|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OC\Calendar; |
| 11 | + |
| 12 | +use DateTimeInterface; |
| 13 | +use InvalidArgumentException; |
| 14 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 15 | +use OCP\Calendar\ICalendarEventBuilder; |
| 16 | +use OCP\Calendar\ICreateFromString; |
| 17 | +use Sabre\VObject\Component\VCalendar; |
| 18 | +use Sabre\VObject\Component\VEvent; |
| 19 | + |
| 20 | +class CalendarEventBuilder implements ICalendarEventBuilder { |
| 21 | + private ?DateTimeInterface $startDate = null; |
| 22 | + private ?DateTimeInterface $endDate = null; |
| 23 | + private ?string $summary = null; |
| 24 | + private ?string $description = null; |
| 25 | + private ?string $location = null; |
| 26 | + private ?array $organizer = null; |
| 27 | + private array $attendees = []; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly string $uid, |
| 31 | + private readonly ITimeFactory $timeFactory, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function setStartDate(DateTimeInterface $start): ICalendarEventBuilder { |
| 36 | + $this->startDate = $start; |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function setEndDate(DateTimeInterface $end): ICalendarEventBuilder { |
| 41 | + $this->endDate = $end; |
| 42 | + return $this; |
| 43 | + } |
| 44 | + |
| 45 | + public function setSummary(string $summary): ICalendarEventBuilder { |
| 46 | + $this->summary = $summary; |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + public function setDescription(string $description): ICalendarEventBuilder { |
| 51 | + $this->description = $description; |
| 52 | + return $this; |
| 53 | + } |
| 54 | + |
| 55 | + public function setLocation(string $location): ICalendarEventBuilder { |
| 56 | + $this->location = $location; |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + public function setOrganizer(string $email, ?string $commonName = null): ICalendarEventBuilder { |
| 61 | + $this->organizer = [$email, $commonName]; |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + public function addAttendee(string $email, ?string $commonName = null): ICalendarEventBuilder { |
| 66 | + $this->attendees[] = [$email, $commonName]; |
| 67 | + return $this; |
| 68 | + } |
| 69 | + |
| 70 | + public function toIcs(): string { |
| 71 | + if ($this->startDate === null) { |
| 72 | + throw new InvalidArgumentException('Event is missing a start date'); |
| 73 | + } |
| 74 | + |
| 75 | + if ($this->endDate === null) { |
| 76 | + throw new InvalidArgumentException('Event is missing an end date'); |
| 77 | + } |
| 78 | + |
| 79 | + if ($this->summary === null) { |
| 80 | + throw new InvalidArgumentException('Event is missing a summary'); |
| 81 | + } |
| 82 | + |
| 83 | + if ($this->organizer === null && $this->attendees !== []) { |
| 84 | + throw new InvalidArgumentException('Event has attendees but is missing an organizer'); |
| 85 | + } |
| 86 | + |
| 87 | + $vcalendar = new VCalendar(); |
| 88 | + $props = [ |
| 89 | + 'UID' => $this->uid, |
| 90 | + 'DTSTAMP' => $this->timeFactory->now(), |
| 91 | + 'SUMMARY' => $this->summary, |
| 92 | + 'DTSTART' => $this->startDate, |
| 93 | + 'DTEND' => $this->endDate, |
| 94 | + ]; |
| 95 | + if ($this->description !== null) { |
| 96 | + $props['DESCRIPTION'] = $this->description; |
| 97 | + } |
| 98 | + if ($this->location !== null) { |
| 99 | + $props['LOCATION'] = $this->location; |
| 100 | + } |
| 101 | + /** @var VEvent $vevent */ |
| 102 | + $vevent = $vcalendar->add('VEVENT', $props); |
| 103 | + if ($this->organizer !== null) { |
| 104 | + self::addAttendeeToVEvent($vevent, 'ORGANIZER', $this->organizer); |
| 105 | + } |
| 106 | + foreach ($this->attendees as $attendee) { |
| 107 | + self::addAttendeeToVEvent($vevent, 'ATTENDEE', $attendee); |
| 108 | + } |
| 109 | + return $vcalendar->serialize(); |
| 110 | + } |
| 111 | + |
| 112 | + public function createInCalendar(ICreateFromString $calendar): string { |
| 113 | + $fileName = $this->uid . '.ics'; |
| 114 | + $calendar->createFromString($fileName, $this->toIcs()); |
| 115 | + return $fileName; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param array{0: string, 1: ?string} $tuple A tuple of [$email, $commonName] where $commonName may be null. |
| 120 | + */ |
| 121 | + private static function addAttendeeToVEvent(VEvent $vevent, string $name, array $tuple): void { |
| 122 | + [$email, $cn] = $tuple; |
| 123 | + if (!str_starts_with($email, 'mailto:')) { |
| 124 | + $email = "mailto:$email"; |
| 125 | + } |
| 126 | + $params = []; |
| 127 | + if ($cn !== null) { |
| 128 | + $params['CN'] = $cn; |
| 129 | + } |
| 130 | + $vevent->add($name, $email, $params); |
| 131 | + } |
| 132 | +} |
0 commit comments