Passed
Pull Request — master (#1281)
by René
03:47
created

NotificationService::createNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Polls\Service;
25
26
use DateTime;
27
use OCP\Notification\IManager;
28
29
class NotificationService {
30
	public const APP_ID = 'polls';
31
	public const EVENT_INVITTION = 'invitation';
32
33
	/** @var IManager */
34
	protected $notificationManager;
35
36
	/** @var string */
37
	protected $userId;
38
39
	public function __construct(
40
		IManager $notificationManager,
41
		$UserId
42
	) {
43
		$this->notificationManager = $notificationManager;
44
		$this->userId = $UserId;
45
	}
46
47
	public function removeNotification(int $pollId): void {
48
		$notification = $this->notificationManager->createNotification();
49
		$notification->setApp(self::APP_ID)
50
			->setObject('poll', strval($pollId))
51
			->setUser($this->userId);
52
		$this->notificationManager->markProcessed($notification);
53
	}
54
55
	public function sendInvitation(int $pollId, $recipient): bool {
56
		$notification = $this->notificationManager->createNotification();
57
		$notification->setApp(self::APP_ID)
58
			->setUser($recipient)
59
			->setDateTime(new DateTime())
60
			->setObject('poll', strval($pollId))
61
			->setSubject(self::EVENT_INVITTION, ['pollId' => $pollId, 'recipient' => $recipient]);
62
		$this->notificationManager->notify($notification);
63
		return true;
64
	}
65
66
	/**
67
	 * create a notification
68
	 *
69
	 * @param array $params
70
	 * 				List of parameters sent to the notification
71
	 * 				following types MUST be defined in the §params array:
72
	 * 				msgId => Type for setSubject
73
	 * 				objectType => Type for setObject
74
	 * 				objectValue => Value for setObject
75
	 * 				recipient => the recipient of the notification
76
	 * 				$params will be set as Subject value
77
	 */
78
79
	public function createNotification(array $params = []): bool {
80
		$notification = $this->notificationManager->createNotification();
81
		$notification->setApp(self::APP_ID)
82
			->setUser($params['recipient'])
83
			->setDateTime(new DateTime())
84
			->setObject($params['objectType'], strval($params['objectValue']))
85
			->setSubject($params['msgId'], $params);
86
		$this->notificationManager->notify($notification);
87
		return true;
88
	}
89
}
90