Completed
Pull Request — master (#5628)
by Joas
17:04
created

Provider::parse()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 15
nop 3
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Settings\Activity;
23
24
use OCP\Activity\IEvent;
25
use OCP\Activity\IManager;
26
use OCP\Activity\IProvider;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use OCP\L10N\IFactory;
32
33
class Provider implements IProvider {
34
35
	const PASSWORD_CHANGED_BY = 'password_changed_by';
36
	const PASSWORD_CHANGED_SELF = 'password_changed_self';
37
	const PASSWORD_RESET = 'password_changed';
38
	const EMAIL_CHANGED_BY = 'email_changed_by';
39
	const EMAIL_CHANGED_SELF = 'email_changed_self';
40
	const EMAIL_CHANGED = 'email_changed';
41
42
	/** @var IFactory */
43
	protected $languageFactory;
44
45
	/** @var IL10N */
46
	protected $l;
47
48
	/** @var IURLGenerator */
49
	protected $url;
50
51
	/** @var IUserManager */
52
	protected $userManager;
53
54
	/** @var IManager */
55
	private $activityManager;
56
57
	/** @var string[] cached displayNames - key is the UID and value the displayname */
58
	protected $displayNames = [];
59
60
	/**
61
	 * @param IFactory $languageFactory
62
	 * @param IURLGenerator $url
63
	 * @param IUserManager $userManager
64
	 * @param IManager $activityManager
65
	 */
66 View Code Duplication
	public function __construct(IFactory $languageFactory, IURLGenerator $url, IUserManager $userManager, IManager $activityManager) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
		$this->languageFactory = $languageFactory;
68
		$this->url = $url;
69
		$this->userManager = $userManager;
70
		$this->activityManager = $activityManager;
71
	}
72
73
	/**
74
	 * @param string $language
75
	 * @param IEvent $event
76
	 * @param IEvent|null $previousEvent
77
	 * @return IEvent
78
	 * @throws \InvalidArgumentException
79
	 * @since 11.0.0
80
	 */
81
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
82
		if ($event->getApp() !== 'settings') {
83
			throw new \InvalidArgumentException();
84
		}
85
86
		$this->l = $this->languageFactory->get('settings', $language);
87
88
		if ($this->activityManager->getRequirePNG()) {
89
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.png')));
90
		} else {
91
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('settings', 'personal.svg')));
92
		}
93
94
		if ($event->getSubject() === self::PASSWORD_CHANGED_BY) {
95
			$subject = $this->l->t('{actor} changed your password');
96
		} else if ($event->getSubject() === self::PASSWORD_CHANGED_SELF) {
97
			$subject = $this->l->t('You changed your password');
98
		} else if ($event->getSubject() === self::PASSWORD_RESET) {
99
			$subject = $this->l->t('Your password was reset by an administrator');
100
101
		} else if ($event->getSubject() === self::EMAIL_CHANGED_BY) {
102
			$subject = $this->l->t('{actor} changed your email address');
103
		} else if ($event->getSubject() === self::EMAIL_CHANGED_SELF) {
104
			$subject = $this->l->t('You changed your email address');
105
		} else if ($event->getSubject() === self::EMAIL_CHANGED) {
106
			$subject = $this->l->t('Your email address was changed by an administrator');
107
108
		} else {
109
			throw new \InvalidArgumentException();
110
		}
111
112
		$parsedParameters = $this->getParameters($event);
113
		$this->setSubjects($event, $subject, $parsedParameters);
114
115
		return $event;
116
	}
117
118
	/**
119
	 * @param IEvent $event
120
	 * @return array
121
	 * @throws \InvalidArgumentException
122
	 */
123
	protected function getParameters(IEvent $event) {
124
		$subject = $event->getSubject();
125
		$parameters = $event->getSubjectParameters();
126
127
		switch ($subject) {
128
			case self::PASSWORD_CHANGED_SELF:
129
			case self::PASSWORD_RESET:
130
			case self::EMAIL_CHANGED_SELF:
131
			case self::EMAIL_CHANGED:
132
				return [];
133
			case self::PASSWORD_CHANGED_BY:
134
			case self::EMAIL_CHANGED_BY:
135
				return [
136
					'actor' => $this->generateUserParameter($parameters[0]),
137
				];
138
		}
139
140
		throw new \InvalidArgumentException();
141
	}
142
143
	/**
144
	 * @param IEvent $event
145
	 * @param string $subject
146
	 * @param array $parameters
147
	 * @throws \InvalidArgumentException
148
	 */
149 View Code Duplication
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
		$placeholders = $replacements = [];
151
		foreach ($parameters as $placeholder => $parameter) {
152
			$placeholders[] = '{' . $placeholder . '}';
153
			$replacements[] = $parameter['name'];
154
		}
155
156
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
157
			->setRichSubject($subject, $parameters);
158
	}
159
160
	/**
161
	 * @param string $uid
162
	 * @return array
163
	 */
164 View Code Duplication
	protected function generateUserParameter($uid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
		if (!isset($this->displayNames[$uid])) {
166
			$this->displayNames[$uid] = $this->getDisplayName($uid);
167
		}
168
169
		return [
170
			'type' => 'user',
171
			'id' => $uid,
172
			'name' => $this->displayNames[$uid],
173
		];
174
	}
175
176
	/**
177
	 * @param string $uid
178
	 * @return string
179
	 */
180
	protected function getDisplayName($uid) {
181
		$user = $this->userManager->get($uid);
182
		if ($user instanceof IUser) {
183
			return $user->getDisplayName();
184
		}
185
186
		return $uid;
187
	}
188
}
189