Passed
Pull Request — master (#1234)
by René
04:40
created

NotificationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
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
37
	/** @var string */
38
	protected $userId;
39
40
	public function __construct(
41
		IManager $notificationManager,
42
		$UserId
43
	) {
44
		$this->notificationManager = $notificationManager;
45
		$this->userId = $UserId;
46
	}
47
48
	public function removeNotification($pollId) {
49
		$notification = $this->notificationManager->createNotification();
50
		$notification->setApp('polls')
51
			->setObject('poll', $pollId)
52
			->setUser($this->userId);
53
		$this->notificationManager->markProcessed($notification);
54
	}
55
56
	/**
57
	 * sendInvitation
58
	 *
59
	 * @param int $pollId
60
	 * @param string $recipient
61
	 */
62
	public function sendInvitation($pollId, $recipient) {
63
		$notification = $this->notificationManager->createNotification();
64
		$notification->setApp('polls')
65
			->setUser($recipient)
66
			->setDateTime(new DateTime())
67
			->setObject('poll', $pollId)
68
			->setSubject('invitation', ['pollId' => $pollId, 'recipient' => $recipient]);
69
		$this->notificationManager->notify($notification);
70
		return true;
71
	}
72
}
73