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

Notifier   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 35
dl 0
loc 85
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getID() 0 2 1
A getName() 0 2 1
A prepare() 0 36 3
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
	 * @return string
60
	 * @since 17.0.0
61
	 */
62
	public function getID(): string {
63
		return 'polls';
64
	}
65
66
	/**
67
	 * Human readable name describing the notifier
68
	 *
69
	 * @return string
70
	 * @since 17.0.0
71
	 */
72
	public function getName(): string {
73
		return $this->l10nFactory->get('polls')->t('Polls');
74
	}
75
76
	/**
77
	 * @param INotification $notification
78
	 * @param string $languageCode The code of the language that should be used to prepare the notification
79
	 * @return INotification
80
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
81
	 * @since 9.0.0
82
	 */
83
	public function prepare(INotification $notification, string $languageCode): INotification {
84
		$l = $this->l10nFactory->get('polls', $languageCode);
85
		if ($notification->getApp() !== 'polls') {
86
			throw new \InvalidArgumentException();
87
		}
88
		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('polls', 'app.svg')));
89
		switch ($notification->getSubject()) {
90
			case 'invitation':
91
				$pollId = $notification->getObjectId();
92
				$poll = $this->pollMapper->find($pollId);
0 ignored issues
show
Bug introduced by
$pollId of type string is incompatible with the type integer expected by parameter $id of OCA\Polls\Db\PollMapper::find(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
				$poll = $this->pollMapper->find(/** @scrutinizer ignore-type */ $pollId);
Loading history...
93
				$owner = $this->userManager->get($poll->getOwner());
94
95
				$notification->setParsedSubject(
96
					$l->t('%s invited you to a poll', [$owner->getDisplayName()])
97
				);
98
99
				$notification->setRichSubject(
100
					$l->t('{user} has invited you to the poll "%s".', [$poll->getTitle()]),
101
					[
102
						'user' => [
103
							'type' => 'user',
104
							'id' => $poll->getOwner(),
105
							'name' => $owner->getDisplayName(),
106
						]
107
					]
108
				);
109
				$notification->setLink($this->url->linkToRouteAbsolute(
110
					'polls.page.vote',
111
					['id' => $poll->getId()]
112
				));
113
				break;
114
			default:
115
				// Unknown subject => Unknown notification => throw
116
				throw new \InvalidArgumentException();
117
		}
118
		return $notification;
119
	}
120
}
121