Passed
Push — master ( a48043...a4b34a )
by Morris
38:55 queued 34s
created

GroupProvider   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 82
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 26

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateUserParameter() 0 9 2
C parse() 0 55 15
A __construct() 0 10 1
A getGroupDisplayName() 0 6 2
A generateGroupParameter() 0 9 2
A setSubjects() 0 9 2
A getDisplayName() 0 6 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Christoph Wurst <[email protected]>
4
 *
5
 * @author Christoph Wurst <[email protected]>
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 InvalidArgumentException;
28
use OCP\Activity\IEvent;
29
use OCP\Activity\IManager;
30
use OCP\Activity\IProvider;
31
use OCP\IGroup;
32
use OCP\IGroupManager;
33
use OCP\IURLGenerator;
34
use OCP\IUser;
35
use OCP\IUserManager;
36
use OCP\L10N\IFactory as L10nFactory;
37
38
class GroupProvider implements IProvider {
39
40
	public const ADDED_TO_GROUP = 'group_added';
41
	public const REMOVED_FROM_GROUP = 'group_removed';
42
43
	/** @var L10nFactory */
44
	private $l10n;
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
	/** @var IManager */
48
	private $activityManager;
49
	/** @var IUserManager */
50
	protected $userManager;
51
	/** @var IGroupManager */
52
	protected $groupManager;
53
54
	/** @var string[] */
55
	protected $groupDisplayNames = [];
56
	/** @var string[] */
57
	protected $userDisplayNames = [];
58
59
60
	public function __construct(L10nFactory $l10n,
61
								IURLGenerator $urlGenerator,
62
								IManager $activityManager,
63
								IUserManager $userManager,
64
								IGroupManager $groupManager) {
65
		$this->urlGenerator = $urlGenerator;
66
		$this->l10n = $l10n;
67
		$this->activityManager = $activityManager;
68
		$this->userManager = $userManager;
69
		$this->groupManager = $groupManager;
70
	}
71
72
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
73
		if ($event->getType() !== 'group_settings') {
74
			throw new InvalidArgumentException();
75
		}
76
77
		$l = $this->l10n->get('settings', $language);
78
79
		$params = $event->getSubjectParameters();
80
		$parsedParameters = [
81
			'user' => $this->generateUserParameter($params['user']),
82
			'group' => $this->generateGroupParameter($params['group']),
83
		];
84
85
		if (isset($params['actor'])) {
86
			$parsedParameters['actor'] = $this->generateUserParameter($params['actor']);
87
		}
88
89
		switch ($event->getSubject()) {
90
			case self::ADDED_TO_GROUP:
91
				if (isset($parsedParameters['actor'])) {
92
					if ($this->activityManager->getCurrentUserId() === $params['user']) {
93
						$subject = $l->t('{actor} added you to group {group}');
94
					} elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
95
						$subject = $l->t('You added {user} to group {group}');
96
					} else {
97
						$subject = $l->t('{actor} added {user} to group {group}');
98
					}
99
				} else if ($this->activityManager->getCurrentUserId() === $params['user']) {
100
					$subject = $l->t('An administrator added you to group {group}');
101
				} else {
102
					$subject = $l->t('An administrator added {user} to group {group}');
103
				}
104
				break;
105
			case self::REMOVED_FROM_GROUP:
106
				if (isset($parsedParameters['actor'])) {
107
					if ($this->activityManager->getCurrentUserId() === $params['user']) {
108
						$subject = $l->t('{actor} removed you from group {group}');
109
					} elseif (isset($params['actor']) && $this->activityManager->getCurrentUserId() === $params['actor']) {
110
						$subject = $l->t('You removed {user} from group {group}');
111
					} else {
112
						$subject = $l->t('{actor} removed {user} from group {group}');
113
					}
114
				} else if ($this->activityManager->getCurrentUserId() === $params['user']) {
115
					$subject = $l->t('An administrator removed you from group {group}');
116
				} else {
117
					$subject = $l->t('An administrator removed {user} from group {group}');
118
				}
119
				break;
120
			default:
121
				throw new InvalidArgumentException();
122
		}
123
124
		$this->setSubjects($event, $subject, $parsedParameters);
125
126
		return $event;
127
	}
128
129
	/**
130
	 * @param IEvent $event
131
	 * @param string $subject
132
	 * @param array $parameters
133
	 * @throws \InvalidArgumentException
134
	 */
135
	protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
136
		$placeholders = $replacements = [];
137
		foreach ($parameters as $placeholder => $parameter) {
138
			$placeholders[] = '{' . $placeholder . '}';
139
			$replacements[] = $parameter['name'];
140
		}
141
142
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
143
			->setRichSubject($subject, $parameters);
144
	}
145
146
	/**
147
	 * @param string $gid
148
	 * @return array
149
	 */
150
	protected function generateGroupParameter(string $gid): array {
151
		if (!isset($this->groupDisplayNames[$gid])) {
152
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
153
		}
154
155
		return [
156
			'type' => 'user-group',
157
			'id' => $gid,
158
			'name' => $this->groupDisplayNames[$gid],
159
		];
160
	}
161
162
	/**
163
	 * @param string $gid
164
	 * @return string
165
	 */
166
	protected function getGroupDisplayName(string $gid): string {
167
		$group = $this->groupManager->get($gid);
168
		if ($group instanceof IGroup) {
0 ignored issues
show
introduced by
$group is always a sub-type of OCP\IGroup.
Loading history...
169
			return $group->getDisplayName();
170
		}
171
		return $gid;
172
	}
173
174
	/**
175
	 * @param string $uid
176
	 * @return array
177
	 */
178
	protected function generateUserParameter(string $uid): array {
179
		if (!isset($this->displayNames[$uid])) {
0 ignored issues
show
Bug Best Practice introduced by
The property displayNames does not exist on OC\Settings\Activity\GroupProvider. Did you maybe forget to declare it?
Loading history...
180
			$this->userDisplayNames[$uid] = $this->getDisplayName($uid);
181
		}
182
183
		return [
184
			'type' => 'user',
185
			'id' => $uid,
186
			'name' => $this->userDisplayNames[$uid],
187
		];
188
	}
189
190
	/**
191
	 * @param string $uid
192
	 * @return string
193
	 */
194
	protected function getDisplayName(string $uid): string {
195
		$user = $this->userManager->get($uid);
196
		if ($user instanceof IUser) {
197
			return $user->getDisplayName();
198
		} else {
199
			return $uid;
200
		}
201
	}
202
}
203