Passed
Push — master ( cf0915...d7495f )
by Roeland
17:33 queued 07:53
created

Provider::getParameters()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 42
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 32
nc 12
nop 1
dl 0
loc 42
rs 6.9666
c 0
b 0
f 0

How to fix   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
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[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 OC\Settings\Activity;
26
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IManager;
29
use OCP\Activity\IProvider;
30
use OCP\IL10N;
31
use OCP\IURLGenerator;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
use OCP\L10N\IFactory;
35
36
class Provider implements IProvider {
37
38
	public const PASSWORD_CHANGED_BY = 'password_changed_by';
39
	public const PASSWORD_CHANGED_SELF = 'password_changed_self';
40
	public const PASSWORD_RESET = 'password_changed';
41
	public const EMAIL_CHANGED_BY = 'email_changed_by';
42
	public const EMAIL_CHANGED_SELF = 'email_changed_self';
43
	public const EMAIL_CHANGED = 'email_changed';
44
	public const APP_TOKEN_CREATED = 'app_token_created';
45
	public const APP_TOKEN_DELETED = 'app_token_deleted';
46
	public const APP_TOKEN_RENAMED = 'app_token_renamed';
47
	public const APP_TOKEN_FILESYSTEM_GRANTED = 'app_token_filesystem_granted';
48
	public const APP_TOKEN_FILESYSTEM_REVOKED = 'app_token_filesystem_revoked';
49
50
	/** @var IFactory */
51
	protected $languageFactory;
52
53
	/** @var IL10N */
54
	protected $l;
55
56
	/** @var IURLGenerator */
57
	protected $url;
58
59
	/** @var IUserManager */
60
	protected $userManager;
61
62
	/** @var IManager */
63
	private $activityManager;
64
65
	/** @var string[] cached displayNames - key is the UID and value the displayname */
66
	protected $displayNames = [];
67
68
	public function __construct(IFactory $languageFactory,
69
								IURLGenerator $url,
70
								IUserManager $userManager,
71
								IManager $activityManager) {
72
		$this->languageFactory = $languageFactory;
73
		$this->url = $url;
74
		$this->userManager = $userManager;
75
		$this->activityManager = $activityManager;
76
	}
77
78
	/**
79
	 * @param string $language
80
	 * @param IEvent $event
81
	 * @param IEvent|null $previousEvent
82
	 * @return IEvent
83
	 * @throws \InvalidArgumentException
84
	 * @since 11.0.0
85
	 */
86
	public function parse($language, IEvent $event, IEvent $previousEvent = null): IEvent {
87
		if ($event->getApp() !== 'settings') {
88
			throw new \InvalidArgumentException('Unknown app');
89
		}
90
91
		$this->l = $this->languageFactory->get('settings', $language);
92
93
		if ($this->activityManager->getRequirePNG()) {
94
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
95
		} else {
96
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
97
		}
98
99
		if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
100
			$subject = $this->l->t('{actor} changed your password');
101
		} else if ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
102
			$subject = $this->l->t('You changed your password');
103
		} else if ($event->getSubject() === self::PASSWORD_RESET) {
104
			$subject = $this->l->t('Your password was reset by an administrator');
105
106
		} else if ($event->getSubject() === self::EMAIL_CHANGED_BY) {
107
			$subject = $this->l->t('{actor} changed your email address');
108
		} else if ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
109
			$subject = $this->l->t('You changed your email address');
110
		} else if ($event->getSubject() === self::EMAIL_CHANGED) {
111
			$subject = $this->l->t('Your email address was changed by an administrator');
112
113
		} else if ($event->getSubject() === self::APP_TOKEN_CREATED) {
114
			$subject = $this->l->t('You created app password "{token}"');
115
		} else if ($event->getSubject() === self::APP_TOKEN_DELETED) {
116
			$subject = $this->l->t('You deleted app password "{token}"');
117
		} else if ($event->getSubject() === self::APP_TOKEN_RENAMED) {
118
			$subject = $this->l->t('You renamed app password "{token}" to "{newToken}"');
119
		} else if ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_GRANTED) {
120
			$subject = $this->l->t('You granted filesystem access to app password "{token}"');
121
		} else if ($event->getSubject() === self::APP_TOKEN_FILESYSTEM_REVOKED) {
122
			$subject = $this->l->t('You revoked filesystem access from app password "{token}"');
123
124
		} else {
125
			throw new \InvalidArgumentException('Unknown subject');
126
		}
127
128
		$parsedParameters = $this->getParameters($event);
129
		$this->setSubjects($event, $subject, $parsedParameters);
130
131
		return $event;
132
	}
133
134
	/**
135
	 * @param IEvent $event
136
	 * @return array
137
	 * @throws \InvalidArgumentException
138
	 */
139
	protected function getParameters(IEvent $event): array {
140
		$subject = $event->getSubject();
141
		$parameters = $event->getSubjectParameters();
142
143
		switch ($subject) {
144
			case self::PASSWORD_CHANGED_SELF:
145
			case self::PASSWORD_RESET:
146
			case self::EMAIL_CHANGED_SELF:
147
			case self::EMAIL_CHANGED:
148
				return [];
149
			case self::PASSWORD_CHANGED_BY:
150
			case self::EMAIL_CHANGED_BY:
151
				return [
152
					'actor' => $this->generateUserParameter($parameters[0]),
153
				];
154
			case self::APP_TOKEN_CREATED:
155
			case self::APP_TOKEN_DELETED:
156
			case self::APP_TOKEN_FILESYSTEM_GRANTED:
157
			case self::APP_TOKEN_FILESYSTEM_REVOKED:
158
				return [
159
					'token' => [
160
						'type' => 'highlight',
161
						'id' => $event->getObjectId(),
162
						'name' => $parameters['name'],
163
					]
164
				];
165
			case self::APP_TOKEN_RENAMED:
166
				return [
167
					'token' => [
168
						'type' => 'highlight',
169
						'id' => $event->getObjectId(),
170
						'name' => $parameters['name'],
171
					],
172
					'newToken' => [
173
						'type' => 'highlight',
174
						'id' => $event->getObjectId(),
175
						'name' => $parameters['newName'],
176
					]
177
				];
178
		}
179
180
		throw new \InvalidArgumentException('Unknown subject');
181
	}
182
183
	/**
184
	 * @param IEvent $event
185
	 * @param string $subject
186
	 * @param array $parameters
187
	 * @throws \InvalidArgumentException
188
	 */
189
	protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
190
		$placeholders = $replacements = [];
191
		foreach ($parameters as $placeholder => $parameter) {
192
			$placeholders[] = '{' . $placeholder . '}';
193
			$replacements[] = $parameter['name'];
194
		}
195
196
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
197
			->setRichSubject($subject, $parameters);
198
	}
199
200
	protected function generateUserParameter(string $uid): array {
201
		if (!isset($this->displayNames[$uid])) {
202
			$this->displayNames[$uid] = $this->getDisplayName($uid);
203
		}
204
205
		return [
206
			'type' => 'user',
207
			'id' => $uid,
208
			'name' => $this->displayNames[$uid],
209
		];
210
	}
211
212
	protected function getDisplayName(string $uid): string {
213
		$user = $this->userManager->get($uid);
214
		if ($user instanceof IUser) {
215
			return $user->getDisplayName();
216
		}
217
218
		return $uid;
219
	}
220
}
221