Passed
Push — master ( 3a4c47...5a7636 )
by René
04:16 queued 10s
created

Notifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 4
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 * @author René Gieling <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 *  This program is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU Affero General Public License as
12
 *  published by the Free Software Foundation, either version 3 of the
13
 *  License, or (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Affero General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Affero General Public License
21
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Polls\Notification;
26
27
use OCP\IURLGenerator;
28
use OCP\IUserManager;
29
use OCP\L10N\IFactory;
30
use OCP\Notification\INotification;
31
use OCP\Notification\INotifier;
32
use OCA\Polls\Db\PollMapper;
33
34
class Notifier implements INotifier {
35
	/** @var IFactory */
36
	protected $l10nFactory;
37
	/** @var IURLGenerator */
38
	protected $url;
39
	/** @var IUserManager */
40
	protected $userManager;
41
	/** @var PollMapper */
42
	protected $pollMapper;
43
44
	public function __construct(
45
		IFactory $l10nFactory,
46
		IURLGenerator $url,
47
		IUserManager $userManager,
48
		PollMapper $pollMapper
49
	) {
50
		$this->l10nFactory = $l10nFactory;
51
		$this->url = $url;
52
		$this->userManager = $userManager;
53
		$this->pollMapper = $pollMapper;
54
	}
55
56
	/**
57
	 * Identifier of the notifier, only use [a-z0-9_]
58
	 */
59
	public function getID(): string {
60
		return 'polls';
61
	}
62
63
	/**
64
	 * Human readable name describing the notifier
65
	 */
66
	public function getName(): string {
67
		return $this->l10nFactory->get('polls')->t('Polls');
68
	}
69
70
	public function prepare(INotification $notification, string $languageCode): INotification {
71
		$l = $this->l10nFactory->get('polls', $languageCode);
72
		if ($notification->getApp() !== 'polls') {
73
			throw new \InvalidArgumentException();
74
		}
75
		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('polls', 'polls-black.svg')));
76
		$parameters = $notification->getSubjectParameters();
77
78
		switch ($notification->getSubject()) {
79
			case 'invitation':
80
				$poll = $this->pollMapper->find(intval($notification->getObjectId()));
81
				$owner = $this->userManager->get($poll->getOwner());
82
83
				$notification->setParsedSubject(
84
					$l->t('%s invited you to a poll', [$owner->getDisplayName()])
85
				);
86
87
				$notification->setRichSubject(
88
					$l->t('{user} has invited you to the poll "%s".', [$poll->getTitle()]),
89
					[
90
						'user' => [
91
							'type' => 'user',
92
							'id' => $poll->getOwner(),
93
							'name' => $owner->getDisplayName(),
94
						]
95
					]
96
				);
97
				$notification->setLink($this->url->linkToRouteAbsolute(
98
					'polls.page.vote',
99
					['id' => $poll->getId()]
100
				));
101
				break;
102
103
			case 'takeOverPoll':
104
				$poll = $this->pollMapper->find(intval($notification->getObjectId()));
105
				$newOwner = $this->userManager->get($parameters['actor']);
106
107
				$notification->setParsedSubject(
108
					$l->t('%s took over your poll', [$newOwner->getDisplayName()])
109
				);
110
111
				$notification->setRichSubject(
112
					$l->t('{user} took over your poll "%s" and is the new owner.', [$poll->getTitle()]),
113
					[
114
						'user' => [
115
							'type' => 'user',
116
							'id' =>  $newOwner->getUID(),
117
							'name' => $newOwner->getDisplayName(),
118
						]
119
					]
120
				);
121
				$notification->setLink($this->url->linkToRouteAbsolute(
122
					'polls.page.vote',
123
					['id' => $poll->getId()]
124
				));
125
				break;
126
127
			case 'deletePollByOther':
128
				$poll = $this->pollMapper->find(intval($notification->getObjectId()));
129
				$actor = $this->userManager->get($parameters['actor']);
130
131
				$notification->setParsedSubject(
132
					$l->t('%s permanently deleted your poll', [$actor->getDisplayName()])
133
				);
134
135
				$notification->setRichSubject(
136
					$l->t('{user} permanently deleted your poll "%s".', $parameters['pollTitle']),
137
					[
138
						'user' => [
139
							'type' => 'user',
140
							'id' =>  $actor->getUID(),
141
							'name' => $actor->getDisplayName(),
142
						]
143
					]
144
				);
145
				$notification->setLink($this->url->linkToRouteAbsolute(
146
					'polls.page.vote',
147
					['id' => $poll->getId()]
148
				));
149
				break;
150
151
			case 'softDeletePollByOther':
152
				$poll = $this->pollMapper->find(intval($notification->getObjectId()));
153
				$actor = $this->userManager->get($parameters['actor']);
154
155
				$notification->setParsedSubject(
156
					$l->t('%s changed the deleted status of your poll', [$actor->getDisplayName()])
157
				);
158
				$notification->setRichSubject(
159
					$l->t('{user} changed the deleted status of your poll "%s".', $parameters['pollTitle']),
160
					[
161
						'user' => [
162
							'type' => 'user',
163
							'id' =>  $actor->getUID(),
164
							'name' => $actor->getDisplayName(),
165
						]
166
					]
167
				);
168
				$notification->setLink($this->url->linkToRouteAbsolute(
169
					'polls.page.vote',
170
					['id' => $poll->getId()]
171
				));
172
				break;
173
			default:
174
				// Unknown subject => Unknown notification => throw
175
				throw new \InvalidArgumentException();
176
		}
177
		return $notification;
178
	}
179
}
180