Notifier::prepare()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 113
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 70
dl 0
loc 113
rs 7.7212
c 1
b 0
f 0
cc 7
nc 7
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AppFramework\Db\DoesNotExistException;
28
29
use OCP\IURLGenerator;
30
use OCP\IUserManager;
31
use OCP\L10N\IFactory;
32
use OCP\Notification\INotification;
33
use OCP\Notification\INotifier;
34
use OCA\Polls\Db\PollMapper;
35
use OCA\Polls\Service\NotificationService;
36
37
class Notifier implements INotifier {
38
39
	/** @var IFactory */
40
	protected $l10nFactory;
41
42
	/** @var IURLGenerator */
43
	protected $url;
44
45
	/** @var IUserManager */
46
	protected $userManager;
47
48
	/** @var PollMapper */
49
	protected $pollMapper;
50
51
	/** @var NotificationService */
52
	private $notificationService;
53
54
	public function __construct(
55
		IFactory $l10nFactory,
56
		IURLGenerator $url,
57
		IUserManager $userManager,
58
		PollMapper $pollMapper,
59
		NotificationService $notificationService
60
	) {
61
		$this->l10nFactory = $l10nFactory;
62
		$this->url = $url;
63
		$this->userManager = $userManager;
64
		$this->pollMapper = $pollMapper;
65
		$this->notificationService = $notificationService;
66
	}
67
68
	/**
69
	 * Identifier of the notifier, only use [a-z0-9_]
70
	 */
71
	public function getID(): string {
72
		return 'polls';
73
	}
74
75
	/**
76
	 * Human readable name describing the notifier
77
	 */
78
	public function getName(): string {
79
		return $this->l10nFactory->get('polls')->t('Polls');
80
	}
81
82
	public function prepare(INotification $notification, string $languageCode): INotification {
83
		$l = $this->l10nFactory->get('polls', $languageCode);
84
		if ($notification->getApp() !== 'polls') {
85
			throw new \InvalidArgumentException();
86
		}
87
88
		try {
89
			$poll = $this->pollMapper->find(intval($notification->getObjectId()));
90
		} catch (DoesNotExistException $e) {
91
			$this->notificationService->removeNotification(intval($notification->getObjectId()));
92
			throw new \InvalidArgumentException();
93
		}
94
95
		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('polls', 'polls-black.svg')));
96
		$parameters = $notification->getSubjectParameters();
97
98
		switch ($notification->getSubject()) {
99
			case 'invitation':
100
101
				$owner = $this->userManager->get($poll->getOwner());
102
103
				$notification->setParsedSubject(
104
					$l->t('%s invited you to a poll', [$owner->getDisplayName()])
105
				);
106
107
				$notification->setRichSubject(
108
					$l->t('{user} has invited you to the poll "%s".', [$poll->getTitle()]),
109
					[
110
						'user' => [
111
							'type' => 'user',
112
							'id' => $poll->getOwner(),
113
							'name' => $owner->getDisplayName(),
114
						]
115
					]
116
				);
117
				$notification->setLink($this->url->linkToRouteAbsolute(
118
					'polls.page.vote',
119
					['id' => $poll->getId()]
120
				));
121
				break;
122
123
			case 'takeOverPoll':
124
				$newOwner = $this->userManager->get($parameters['actor']);
125
126
				$notification->setParsedSubject(
127
					$l->t('%s took over your poll', [$newOwner->getDisplayName()])
128
				);
129
130
				$notification->setRichSubject(
131
					$l->t('{user} took over your poll "%s" and is the new owner.', [$poll->getTitle()]),
132
					[
133
						'user' => [
134
							'type' => 'user',
135
							'id' => $newOwner->getUID(),
136
							'name' => $newOwner->getDisplayName(),
137
						]
138
					]
139
				);
140
				$notification->setLink($this->url->linkToRouteAbsolute(
141
					'polls.page.vote',
142
					['id' => $poll->getId()]
143
				));
144
				break;
145
146
			case 'deletePollByOther':
147
				$actor = $this->userManager->get($parameters['actor']);
148
149
				$notification->setParsedSubject(
150
					$l->t('%s permanently deleted your poll', [$actor->getDisplayName()])
151
				);
152
153
				$notification->setRichSubject(
154
					$l->t('{user} permanently deleted your poll "%s".', $parameters['pollTitle']),
155
					[
156
						'user' => [
157
							'type' => 'user',
158
							'id' => $actor->getUID(),
159
							'name' => $actor->getDisplayName(),
160
						]
161
					]
162
				);
163
				$notification->setLink($this->url->linkToRouteAbsolute(
164
					'polls.page.vote',
165
					['id' => $poll->getId()]
166
				));
167
				break;
168
169
			case 'softDeletePollByOther':
170
				$actor = $this->userManager->get($parameters['actor']);
171
172
				$notification->setParsedSubject(
173
					$l->t('%s changed the deleted status of your poll.', [$actor->getDisplayName()])
174
				);
175
				$notification->setRichSubject(
176
					$l->t('{user} changed the deleted status of your poll "%s".', $parameters['pollTitle']),
177
					[
178
						'user' => [
179
							'type' => 'user',
180
							'id' => $actor->getUID(),
181
							'name' => $actor->getDisplayName(),
182
						]
183
					]
184
				);
185
				$notification->setLink($this->url->linkToRouteAbsolute(
186
					'polls.page.vote',
187
					['id' => $poll->getId()]
188
				));
189
				break;
190
			default:
191
				// Unknown subject => Unknown notification => throw
192
				throw new \InvalidArgumentException();
193
		}
194
		return $notification;
195
	}
196
}
197