Completed
Push — master ( fde08a...208e38 )
by Blizzz
17:49
created

Base::getDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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
 *
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\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\IUser;
33
use OCP\IUserManager;
34
35
abstract class Base implements IProvider {
36
37
	/** @var IUserManager */
38
	protected $userManager;
39
40
	/** @var string[]  */
41
	protected $userDisplayNames = [];
42
43
	/** @var IGroupManager */
44
	protected $groupManager;
45
46
	/** @var string[] */
47
	protected $groupDisplayNames = [];
48
49
	/**
50
	 * @param IUserManager $userManager
51
	 * @param IGroupManager $groupManager
52
	 */
53
	public function __construct(IUserManager $userManager, IGroupManager $groupManager) {
54
		$this->userManager = $userManager;
55
		$this->groupManager = $groupManager;
56
	}
57
58
	/**
59
	 * @param IEvent $event
60
	 * @param string $subject
61
	 * @param array $parameters
62
	 */
63 View Code Duplication
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
64
		$placeholders = $replacements = [];
65
		foreach ($parameters as $placeholder => $parameter) {
66
			$placeholders[] = '{' . $placeholder . '}';
67
			$replacements[] = $parameter['name'];
68
		}
69
70
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
71
			->setRichSubject($subject, $parameters);
72
	}
73
74
	/**
75
	 * @param array $eventData
76
	 * @return array
77
	 */
78
	protected function generateObjectParameter($eventData) {
79
		if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) {
80
			throw new \InvalidArgumentException();
81
		}
82
83
		return [
84
			'type' => 'calendar-event',
85
			'id' => $eventData['id'],
86
			'name' => $eventData['name'],
87
		];
88
	}
89
90
	/**
91
	 * @param array $data
92
	 * @param IL10N $l
93
	 * @return array
94
	 */
95
	protected function generateCalendarParameter($data, IL10N $l) {
96
		if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
97
			$data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
98
			return [
99
				'type' => 'calendar',
100
				'id' => $data['id'],
101
				'name' => $l->t('Personal'),
102
			];
103
		}
104
105
		return [
106
			'type' => 'calendar',
107
			'id' => $data['id'],
108
			'name' => $data['name'],
109
		];
110
	}
111
112
	/**
113
	 * @param int $id
114
	 * @param string $name
115
	 * @return array
116
	 */
117
	protected function generateLegacyCalendarParameter($id, $name) {
118
		return [
119
			'type' => 'calendar',
120
			'id' => $id,
121
			'name' => $name,
122
		];
123
	}
124
125
	/**
126
	 * @param string $uid
127
	 * @return array
128
	 */
129
	protected function generateUserParameter($uid) {
130
		if (!isset($this->userDisplayNames[$uid])) {
131
			$this->userDisplayNames[$uid] = $this->getUserDisplayName($uid);
132
		}
133
134
		return [
135
			'type' => 'user',
136
			'id' => $uid,
137
			'name' => $this->userDisplayNames[$uid],
138
		];
139
	}
140
141
	/**
142
	 * @param string $uid
143
	 * @return string
144
	 */
145
	protected function getUserDisplayName($uid) {
146
		$user = $this->userManager->get($uid);
147
		if ($user instanceof IUser) {
148
			return $user->getDisplayName();
149
		}
150
		return $uid;
151
	}
152
153
	/**
154
	 * @param string $gid
155
	 * @return array
156
	 */
157 View Code Duplication
	protected function generateGroupParameter($gid) {
158
		if (!isset($this->groupDisplayNames[$gid])) {
159
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
160
		}
161
162
		return [
163
			'type' => 'group',
164
			'id' => $gid,
165
			'name' => $this->groupDisplayNames[$gid],
166
		];
167
	}
168
169
	/**
170
	 * @param string $gid
171
	 * @return string
172
	 */
173
	protected function getGroupDisplayName($gid) {
174
		$group = $this->groupManager->get($gid);
175
		if ($group instanceof IGroup) {
176
			return $group->getDisplayName();
177
		}
178
		return $gid;
179
	}
180
}
181