Completed
Pull Request — master (#638)
by
unknown
03:07
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
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\Passman;
25
use OCP\Notification\INotification;
26
use OCP\Notification\INotifier;
27
28
class Notifier implements INotifier {
29
30
	protected $factory;
31
32
	public function __construct(\OCP\L10N\IFactory $factory) {
33
		$this->factory = $factory;
34
	}
35
36
	/**
37
	 * @param INotification $notification
38
	 * @param string $languageCode The code of the language that should be used to prepare the notification
39
	 */
40
public function prepare(INotification $notification, string $languageCode): INotification {
41
		if ($notification->getApp() !== 'passman') {
42
			// Not my app => throw
43
			throw new \InvalidArgumentException();
44
		}
45
46
		// Read the language from the notification
47
		$l = $this->factory->get('passman', $languageCode);
48
49
		switch ($notification->getSubject()) {
50
			// Deal with known subjects
51
			case 'credential_expired':
52
				$notification->setParsedSubject(
53
					(string) $l->t('Your credential "%s" expired, click here to update the credential.', $notification->getSubjectParameters())
54
				);
55
56
				// Deal with the actions for a known subject
57
				foreach ($notification->getActions() as $action) {
58
					switch ($action->getLabel()) {
59
						case 'remind':
60
							$action->setParsedLabel(
61
								(string) $l->t('Remind me later')
62
							);
63
							break;
64
65
						case 'ignore':
66
							$action->setParsedLabel(
67
								(string) $l->t('Ignore')
68
							);
69
							break;
70
					}
71
72
					$notification->addParsedAction($action);
73
				}
74
				return $notification;
75
76
77
			case 'credential_shared':
78
				$notification->setParsedSubject(
79
					(string) $l->t('%s shared "%s" with you. Click here to accept', $notification->getSubjectParameters())
80
				);
81
82
				// Deal with the actions for a known subject
83
				foreach ($notification->getActions() as $action) {
84
					switch ($action->getLabel()) {
85
						case 'decline':
86
							$action->setParsedLabel(
87
								(string) $l->t('Decline')
88
							);
89
							break;
90
					}
91
92
					$notification->addParsedAction($action);
93
				}
94
				return $notification;
95
96
			case 'credential_share_denied':
97
				$notification->setParsedSubject(
98
					(string) $l->t('%s has declined your share request for "%s".', $notification->getSubjectParameters())
99
				);
100
				return $notification;
101
102
			case 'credential_share_accepted':
103
				$notification->setParsedSubject(
104
					(string) $l->t('%s has accepted your share request for "%s".', $notification->getSubjectParameters())
105
				);
106
				return $notification;
107
			default:
108
				// Unknown subject => Unknown notification => throw
109
				throw new \InvalidArgumentException();
110
		}
111
	}
112
113
	/**
114
	 * Identifier of the notifier
115
	 *
116
	 * @return string
117
	 */
118
	public function getID(): string {
119
		return 'passman';
120
	}
121
	/**
122
	 * Human readable name describing the notifier
123
	 *
124
	 * @return string
125
	 */
126
	public function getName(): string {
127
		return $this->factory->get('passman')->t('Passwords');
128
	}
129
}
130