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
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2021 Joas Schilling <[email protected]>
7
 *
8
 * @author Joas Schilling <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
namespace OCA\DAV\CardDAV\Activity\Provider;
27
28
use OCA\DAV\CardDAV\CardDavBackend;
29
use OCP\Activity\IEvent;
30
use OCP\Activity\IProvider;
31
use OCP\IGroup;
32
use OCP\IGroupManager;
33
use OCP\IL10N;
34
use OCP\IURLGenerator;
35
use OCP\IUser;
36
use OCP\IUserManager;
37
38
abstract class Base implements IProvider {
39
40
	/** @var IUserManager */
41
	protected $userManager;
42
43
	/** @var string[]  */
44
	protected $userDisplayNames = [];
45
46
	/** @var IGroupManager */
47
	protected $groupManager;
48
49
	/** @var string[] */
50
	protected $groupDisplayNames = [];
51
52
	/** @var IURLGenerator */
53
	protected $url;
54
55
	public function __construct(IUserManager $userManager,
56
								IGroupManager $groupManager,
57
								IURLGenerator $urlGenerator) {
58
		$this->userManager = $userManager;
59
		$this->groupManager = $groupManager;
60
		$this->url = $urlGenerator;
61
	}
62
63
	/**
64
	 * @param IEvent $event
65
	 * @param string $subject
66
	 * @param array $parameters
67
	 */
68
	protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
69
		$placeholders = $replacements = [];
70
		foreach ($parameters as $placeholder => $parameter) {
71
			$placeholders[] = '{' . $placeholder . '}';
72
			$replacements[] = $parameter['name'];
73
		}
74
75
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
76
			->setRichSubject($subject, $parameters);
77
	}
78
79
	/**
80
	 * @param array $data
81
	 * @param IL10N $l
82
	 * @return array
83
	 */
84
	protected function generateAddressbookParameter(array $data, IL10N $l): array {
85
		if ($data['uri'] === CardDavBackend::PERSONAL_ADDRESSBOOK_URI &&
86
			$data['name'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME) {
87
			return [
88
				'type' => 'addressbook',
89
				'id' => $data['id'],
90
				'name' => $l->t('Personal'),
91
			];
92
		}
93
94
		return [
95
			'type' => 'addressbook',
96
			'id' => $data['id'],
97
			'name' => $data['name'],
98
		];
99
	}
100
101
	protected function generateUserParameter(string $uid): array {
102
		return [
103
			'type' => 'user',
104
			'id' => $uid,
105
			'name' => $this->userManager->getDisplayName($uid) ?? $uid,
106
		];
107
	}
108
109
	/**
110
	 * @param string $gid
111
	 * @return array
112
	 */
113
	protected function generateGroupParameter(string $gid): array {
114
		if (!isset($this->groupDisplayNames[$gid])) {
115
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
116
		}
117
118
		return [
119
			'type' => 'user-group',
120
			'id' => $gid,
121
			'name' => $this->groupDisplayNames[$gid],
122
		];
123
	}
124
125
	/**
126
	 * @param string $gid
127
	 * @return string
128
	 */
129
	protected function getGroupDisplayName(string $gid): string {
130
		$group = $this->groupManager->get($gid);
131
		if ($group instanceof IGroup) {
132
			return $group->getDisplayName();
133
		}
134
		return $gid;
135
	}
136
}
137