Passed
Push — master ( 7d7f7a...71065f )
by Joas
16:42 queued 13s
created

Base::getUserDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Thomas Citharel <[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
namespace OCA\DAV\CalDAV\Activity\Provider;
25
26
use OCA\DAV\CalDAV\CalDavBackend;
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IProvider;
29
use OCP\IGroup;
30
use OCP\IGroupManager;
31
use OCP\IL10N;
32
use OCP\IURLGenerator;
33
use OCP\IUser;
34
use OCP\IUserManager;
35
36
abstract class Base implements IProvider {
37
38
	/** @var IUserManager */
39
	protected $userManager;
40
41
	/** @var string[]  */
42
	protected $userDisplayNames = [];
43
44
	/** @var IGroupManager */
45
	protected $groupManager;
46
47
	/** @var string[] */
48
	protected $groupDisplayNames = [];
49
50
	/** @var IURLGenerator */
51
	protected $url;
52
53
	/**
54
	 * @param IUserManager $userManager
55
	 * @param IGroupManager $groupManager
56
	 * @param IURLGenerator $urlGenerator
57
	 */
58
	public function __construct(IUserManager $userManager, IGroupManager $groupManager, IURLGenerator $urlGenerator) {
59
		$this->userManager = $userManager;
60
		$this->groupManager = $groupManager;
61
		$this->url = $urlGenerator;
62
	}
63
64
	/**
65
	 * @param IEvent $event
66
	 * @param string $subject
67
	 * @param array $parameters
68
	 */
69
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
70
		$placeholders = $replacements = [];
71
		foreach ($parameters as $placeholder => $parameter) {
72
			$placeholders[] = '{' . $placeholder . '}';
73
			$replacements[] = $parameter['name'];
74
		}
75
76
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
77
			->setRichSubject($subject, $parameters);
78
	}
79
80
	/**
81
	 * @param array $data
82
	 * @param IL10N $l
83
	 * @return array
84
	 */
85
	protected function generateCalendarParameter($data, IL10N $l) {
86
		if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
87
			$data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
88
			return [
89
				'type' => 'calendar',
90
				'id' => $data['id'],
91
				'name' => $l->t('Personal'),
92
			];
93
		}
94
95
		return [
96
			'type' => 'calendar',
97
			'id' => $data['id'],
98
			'name' => $data['name'],
99
		];
100
	}
101
102
	/**
103
	 * @param int $id
104
	 * @param string $name
105
	 * @return array
106
	 */
107
	protected function generateLegacyCalendarParameter($id, $name) {
108
		return [
109
			'type' => 'calendar',
110
			'id' => $id,
111
			'name' => $name,
112
		];
113
	}
114
115
	protected function generateUserParameter(string $uid): array {
116
		return [
117
			'type' => 'user',
118
			'id' => $uid,
119
			'name' => $this->userManager->getDisplayName($uid) ?? $uid,
120
		];
121
	}
122
123
	/**
124
	 * @param string $gid
125
	 * @return array
126
	 */
127
	protected function generateGroupParameter($gid) {
128
		if (!isset($this->groupDisplayNames[$gid])) {
129
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
130
		}
131
132
		return [
133
			'type' => 'user-group',
134
			'id' => $gid,
135
			'name' => $this->groupDisplayNames[$gid],
136
		];
137
	}
138
139
	/**
140
	 * @param string $gid
141
	 * @return string
142
	 */
143
	protected function getGroupDisplayName($gid) {
144
		$group = $this->groupManager->get($gid);
145
		if ($group instanceof IGroup) {
146
			return $group->getDisplayName();
147
		}
148
		return $gid;
149
	}
150
}
151