Passed
Push — master ( a1c1b3...a1f329 )
by Roeland
31:31 queued 18:39
created

Base::generateObjectParameter()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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