Passed
Push — master ( 9c2d70...6ef7ba )
by Roeland
10:28
created

PushProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 92
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A send() 0 20 2
A extractEventDetails() 0 26 4
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2019, Thomas Citharel
5
 * @copyright Copyright (c) 2019, Georg Ehrke
6
 *
7
 * @author Thomas Citharel <[email protected]>
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
namespace OCA\DAV\CalDAV\Reminder\NotificationProvider;
27
28
use OCA\DAV\AppInfo\Application;
29
use OCP\IConfig;
30
use OCP\ILogger;
31
use OCP\IURLGenerator;
32
use OCP\L10N\IFactory as L10NFactory;
33
use OCP\Notification\IManager;
34
use OCP\IUser;
35
use OCP\Notification\INotification;
36
use OCP\AppFramework\Utility\ITimeFactory;
37
use Sabre\VObject\Component\VEvent;
38
use Sabre\VObject\Property;
39
40
/**
41
 * Class PushProvider
42
 *
43
 * @package OCA\DAV\CalDAV\Reminder\NotificationProvider
44
 */
45
class PushProvider extends AbstractProvider {
46
47
	/** @var string */
48
	public const NOTIFICATION_TYPE = 'DISPLAY';
49
50
	/** @var IManager */
51
	private $manager;
52
53
	/** @var ITimeFactory */
54
	private $timeFactory;
55
56
	/**
57
	 * @param IConfig $config
58
	 * @param IManager $manager
59
	 * @param ILogger $logger
60
	 * @param L10NFactory $l10nFactory
61
	 * @param IUrlGenerator $urlGenerator
62
	 * @param ITimeFactory $timeFactory
63
	 */
64
	public function __construct(IConfig $config,
65
								IManager $manager,
66
								ILogger $logger,
67
								L10NFactory $l10nFactory,
68
								IURLGenerator $urlGenerator,
69
								ITimeFactory $timeFactory) {
70
		parent::__construct($logger, $l10nFactory, $urlGenerator, $config);
71
		$this->manager = $manager;
72
		$this->timeFactory = $timeFactory;
73
	}
74
75
	/**
76
	 * Send push notification to all users.
77
	 *
78
	 * @param VEvent $vevent
79
	 * @param string $calendarDisplayName
80
	 * @param IUser[] $users
81
	 * @throws \Exception
82
	 */
83
	public function send(VEvent $vevent,
84
						 string $calendarDisplayName=null,
85
						 array $users=[]):void {
86
		$eventDetails = $this->extractEventDetails($vevent);
87
		$eventDetails['calendar_displayname'] = $calendarDisplayName;
88
89
		foreach($users as $user) {
90
			/** @var INotification $notification */
91
			$notification = $this->manager->createNotification();
92
			$notification->setApp(Application::APP_ID)
93
				->setUser($user->getUID())
94
				->setDateTime($this->timeFactory->getDateTime())
95
				->setObject(Application::APP_ID, (string) $vevent->UID)
96
				->setSubject('calendar_reminder', [
97
					'title' => $eventDetails['title'],
98
					'start_atom' => $eventDetails['start_atom']
99
				])
100
				->setMessage('calendar_reminder', $eventDetails);
101
102
			$this->manager->notify($notification);
103
		}
104
	}
105
106
	/**
107
	 * @var VEvent $vevent
108
	 * @return array
109
	 * @throws \Exception
110
	 */
111
	protected function extractEventDetails(VEvent $vevent):array {
112
		/** @var Property\ICalendar\DateTime $start */
113
		$start = $vevent->DTSTART;
114
		$end = $this->getDTEndFromEvent($vevent);
115
116
		return [
117
			'title' => isset($vevent->SUMMARY)
118
				? ((string) $vevent->SUMMARY)
119
				: null,
120
			'description' => isset($vevent->DESCRIPTION)
121
				? ((string) $vevent->DESCRIPTION)
122
				: null,
123
			'location' => isset($vevent->LOCATION)
124
				? ((string) $vevent->LOCATION)
125
				: null,
126
			'all_day' => $start instanceof Property\ICalendar\Date,
127
			/** @phan-suppress-next-line PhanUndeclaredClassMethod */
128
			'start_atom' => $start->getDateTime()->format(\DateTime::ATOM),
129
			'start_is_floating' => $start->isFloating(),
130
			/** @phan-suppress-next-line PhanUndeclaredClassMethod */
131
			'start_timezone' => $start->getDateTime()->getTimezone()->getName(),
132
			/** @phan-suppress-next-line PhanUndeclaredClassMethod */
133
			'end_atom' => $end->getDateTime()->format(\DateTime::ATOM),
134
			'end_is_floating' => $end->isFloating(),
135
			/** @phan-suppress-next-line PhanUndeclaredClassMethod */
136
			'end_timezone' => $end->getDateTime()->getTimezone()->getName(),
137
		];
138
	}
139
}
140