Passed
Pull Request — master (#632)
by Julius
04:14
created

Notifier   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 126
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
C prepare() 0 92 9
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[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\Deck\Notification;
25
26
use OCA\Deck\Db\BoardMapper;
27
use OCA\Deck\Db\CardMapper;
28
use OCP\IURLGenerator;
29
use OCP\IUserManager;
30
use OCP\L10N\IFactory;
31
use OCP\Notification\INotification;
32
use OCP\Notification\INotifier;
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 CardMapper */
42
	protected $cardMapper;
43
	/** @var BoardMapper */
44
	protected $boardMapper;
45
46
	public function __construct(
47
		IFactory $l10nFactory,
48
		IURLGenerator $url,
49
		IUserManager $userManager,
50
		CardMapper $cardMapper,
51
		BoardMapper $boardMapper
52
	) {
53
		$this->l10nFactory = $l10nFactory;
54
		$this->url = $url;
55
		$this->userManager = $userManager;
56
		$this->cardMapper = $cardMapper;
57
		$this->boardMapper = $boardMapper;
58
	}
59
60
	/**
61
	 * @param INotification $notification
62
	 * @param string $languageCode The code of the language that should be used to prepare the notification
63
	 * @return INotification
64
	 * @throws \InvalidArgumentException When the notification was not prepared by a notifier
65
	 * @since 9.0.0
66
	 */
67
	public function prepare(INotification $notification, $languageCode) {
68
		$l = $this->l10nFactory->get('deck', $languageCode);
69
		if ($notification->getApp() !== 'deck') {
70
			throw new \InvalidArgumentException();
71
		}
72
		$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('deck', 'deck-dark.svg')));
73
		$params = $notification->getSubjectParameters();
74
75
		switch ($notification->getSubject()) {
76
			case 'card-assigned':
77
				$cardId = $notification->getObjectId();
78
				$boardId = $this->cardMapper->findBoardId($cardId);
79
				$initiator = $this->userManager->get($params[2]);
80
				if ($initiator !== null) {
81
					$dn = $initiator->getDisplayName();
82
				} else {
83
					$dn = $params[2];
84
				}
85
				$notification->setParsedSubject(
86
					(string) $l->t('The card "%s" on "%s" has been assigned to you by %s.', [$params[0], $params[1], $dn])
87
				);
88
				$notification->setRichSubject(
89
					(string) $l->t('{user} has assigned the card "%s" on "%s" to you.', [$params[0], $params[1]]),
90
					[
91
						'user' => [
92
							'type' => 'user',
93
							'id' => $params[2],
94
							'name' => $dn,
95
						]
96
					]
97
				);
98
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
99
				break;
100
			case 'card-overdue':
101
				$cardId = $notification->getObjectId();
102
				$boardId = $this->cardMapper->findBoardId($cardId);
103
				$notification->setParsedSubject(
104
					(string) $l->t('The card "%s" on "%s" has reached its due date.', $params)
105
				);
106
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
107
				break;
108
			case 'card-comment-mentioned':
109
				$cardId = $notification->getObjectId();
110
				$boardId = $this->cardMapper->findBoardId($cardId);
111
				$initiator = $this->userManager->get($params[2]);
112
				if ($initiator !== null) {
113
					$dn = $initiator->getDisplayName();
114
				} else {
115
					$dn = $params[2];
116
				}
117
				$notification->setParsedSubject(
118
					(string) $l->t('%s has mentioned in a comment on "%s".', [$dn, $params[0]])
119
				);
120
				$notification->setRichSubject(
121
					(string) $l->t('{user} has mentioned in a comment on "%s".', [$params[0]]),
122
					[
123
						'user' => [
124
							'type' => 'user',
125
							'id' => $params[2],
126
							'name' => $dn,
127
						]
128
					]
129
				);
130
				$notification->setParsedMessage($notification->getMessage());
131
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '//card/' . $cardId . '');
132
				break;
133
			case 'board-shared':
134
				$boardId = $notification->getObjectId();
135
				$initiator = $this->userManager->get($params[1]);
136
				if ($initiator !== null) {
137
					$dn = $initiator->getDisplayName();
138
				} else {
139
					$dn = $params[1];
140
				}
141
				$notification->setParsedSubject(
142
					(string) $l->t('The board "%s" has been shared with you by %s.', [$params[0], $dn])
143
				);
144
				$notification->setRichSubject(
145
					(string) $l->t('{user} has shared the board %s with you.', [$params[0]]),
146
					[
147
						'user' => [
148
							'type' => 'user',
149
							'id' => $params[1],
150
							'name' => $dn,
151
						]
152
					]
153
				);
154
				$notification->setLink($this->url->linkToRouteAbsolute('deck.page.index') . '#!/board/' . $boardId . '/');
155
				break;
156
		}
157
		return $notification;
158
	}
159
}
160