Failed Conditions
Push — master ( e8410d...a45585 )
by Marcos
09:36 queued 11s
created

Notifier::prepare()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
dl 0
loc 72
rs 6.4642
c 0
b 0
f 0
nc 11
nop 2

How to fix   Long Method    Complexity   

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
 * 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
26
use OCP\L10N\IFactory;
27
use OCP\Notification\INotification;
28
use OCP\Notification\INotifier;
29
30
class Notifier implements INotifier {
31
32
	protected IFactory $factory;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
33
34
	public function __construct(IFactory $factory) {
35
		$this->factory = $factory;
36
	}
37
38
	/**
39
	 * @param INotification $notification
40
	 * @param string $languageCode The code of the language that should be used to prepare the notification
41
	 */
42
	public function prepare(INotification $notification, string $languageCode): INotification {
43
		if ($notification->getApp() !== 'passman') {
44
			// Not my app => throw
45
			throw new \InvalidArgumentException();
46
		}
47
48
		// Read the language from the notification
49
		$l = $this->factory->get('passman', $languageCode);
50
51
		switch ($notification->getSubject()) {
52
			// Deal with known subjects
53
			case 'credential_expired':
54
				$notification->setParsedSubject(
55
					(string)$l->t('Your credential "%s" expired, click here to update the credential.', $notification->getSubjectParameters())
56
				);
57
58
				// Deal with the actions for a known subject
59
				foreach ($notification->getActions() as $action) {
60
					switch ($action->getLabel()) {
61
						case 'remind':
62
							$action->setParsedLabel(
63
								(string)$l->t('Remind me later')
64
							);
65
							break;
66
67
						case 'ignore':
68
							$action->setParsedLabel(
69
								(string)$l->t('Ignore')
70
							);
71
							break;
72
					}
73
74
					$notification->addParsedAction($action);
75
				}
76
				return $notification;
77
78
79
			case 'credential_shared':
80
				$notification->setParsedSubject(
81
					(string)$l->t('%s shared "%s" with you. Click here to accept', $notification->getSubjectParameters())
82
				);
83
84
				// Deal with the actions for a known subject
85
				foreach ($notification->getActions() as $action) {
86
					switch ($action->getLabel()) {
87
						case 'decline':
88
							$action->setParsedLabel(
89
								(string)$l->t('Decline')
90
							);
91
							break;
92
					}
93
94
					$notification->addParsedAction($action);
95
				}
96
				return $notification;
97
98
			case 'credential_share_denied':
99
				$notification->setParsedSubject(
100
					(string)$l->t('%s has declined your share request for "%s".', $notification->getSubjectParameters())
101
				);
102
				return $notification;
103
104
			case 'credential_share_accepted':
105
				$notification->setParsedSubject(
106
					(string)$l->t('%s has accepted your share request for "%s".', $notification->getSubjectParameters())
107
				);
108
				return $notification;
109
			default:
110
				// Unknown subject => Unknown notification => throw
111
				throw new \InvalidArgumentException();
112
		}
113
	}
114
115
	/**
116
	 * Identifier of the notifier
117
	 *
118
	 * @return string
119
	 */
120
	public function getID(): string {
121
		return 'passman';
122
	}
123
124
	/**
125
	 * Human readable name describing the notifier
126
	 *
127
	 * @return string
128
	 */
129
	public function getName(): string {
130
		return $this->factory->get('passman')->t('Passwords');
131
	}
132
}
133