Completed
Pull Request — master (#386)
by Maxence
01:53
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Notification;
31
32
33
use InvalidArgumentException;
34
use OC;
35
use OCA\Circles\AppInfo\Application;
36
use OCP\Contacts\IManager;
37
use OCP\Federation\ICloudIdManager;
38
use OCP\IL10N;
39
use OCP\IURLGenerator;
40
use OCP\L10N\IFactory;
41
use OCP\Notification\INotification;
42
use OCP\Notification\INotifier;
43
44
45
/**
46
 * Class Notifier
47
 *
48
 * @package OCA\Circles\Notification
49
 */
50
class Notifier implements INotifier {
51
52
53
	/** @var IL10N */
54
	private $l10n;
55
56
57
	/** @var IFactory */
58
	protected $factory;
59
60
	/** @var IManager */
61
	protected $contactsManager;
62
63
	/** @var IURLGenerator */
64
	protected $url;
65
66
	/** @var array */
67
	protected $federatedContacts;
68
69
	/** @var ICloudIdManager */
70
	protected $cloudIdManager;
71
72
73
	public function __construct(
74
		IL10N $l10n, IFactory $factory, IManager $contactsManager, IURLGenerator $url,
75
		ICloudIdManager $cloudIdManager
76
	) {
77
		$this->l10n = $l10n;
78
		$this->factory = $factory;
79
		$this->contactsManager = $contactsManager;
80
		$this->url = $url;
81
		$this->cloudIdManager = $cloudIdManager;
82
	}
83
84
	/**
85
	 * Identifier of the notifier, only use [a-z0-9_]
86
	 *
87
	 * @return string
88
	 * @since 17.0.0
89
	 */
90
	public function getID(): string {
91
		return 'circles';
92
	}
93
94
	/**
95
	 * Human readable name describing the notifier
96
	 *
97
	 * @return string
98
	 * @since 17.0.0
99
	 */
100
	public function getName(): string {
101
		return $this->l10n->t('Circles');
102
	}
103
104
	/**
105
	 * @param INotification $notification
106
	 * @param string $languageCode The code of the language that should be used to prepare the notification
107
	 *
108
	 * @return INotification
109
	 * @throws InvalidArgumentException
110
	 */
111
	public function prepare(INotification $notification, string $languageCode): INotification {
112
		if ($notification->getApp() !== 'circles') {
113
			throw new InvalidArgumentException();
114
		}
115
116
		$l10n = OC::$server->getL10N(Application::APP_NAME, $languageCode);
117
118
		$notification->setIcon(
119
			$this->url->getAbsoluteURL($this->url->imagePath('circles', 'black_circle.svg'))
120
		);
121
		$params = $notification->getSubjectParameters();
122
123
		switch ($notification->getSubject()) {
124
			case 'invitation':
125
				$notification->setParsedSubject(
126
					$l10n->t('You have been invited by %1$s into the Circle "%2$s"', $params)
127
				);
128
				break;
129
130
			case 'member_new':
131
				$notification->setParsedSubject(
132
					$l10n->t('You are now a member of the Circle "%2$s"', $params)
133
				);
134
				break;
135
136
			case 'request_new':
137
				$notification->setParsedSubject(
138
					$l10n->t('%1$s sent a request to be a member of the Circle "%2$s"', $params)
139
				);
140
				break;
141
142
			default:
143
				throw new InvalidArgumentException();
144
		}
145
146
147
		foreach ($notification->getActions() as $action) {
148
			switch ($action->getLabel()) {
149
				case 'accept':
150
					$action->setParsedLabel((string)$l10n->t('Accept'))
151
						   ->setPrimary(true);
152
					break;
153
154
				case 'refuse':
155
					$action->setParsedLabel((string)$l10n->t('Refuse'));
156
					break;
157
158
				case 'leave':
159
					$action->setParsedLabel((string)$l10n->t('Leave the circle'));
160
					break;
161
			}
162
163
			$notification->addParsedAction($action);
164
		}
165
166
		return $notification;
167
168
	}
169
170
}
171