Passed
Push — master ( bce941...17d0da )
by John
67:35 queued 55:34
created

Base   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateGroupParameter() 0 9 2
A __construct() 0 6 1
A generateUserParameter() 0 9 2
A setSubjects() 0 9 2
A getGroupDisplayName() 0 6 2
A generateAddressbookParameter() 0 14 3
A getUserDisplayName() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2021 Joas Schilling <[email protected]>
6
 *
7
 * @author Joas Schilling <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
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
	/**
102
	 * @param string $uid
103
	 * @return array
104
	 */
105
	protected function generateUserParameter(string $uid): array {
106
		if (!isset($this->userDisplayNames[$uid])) {
107
			$this->userDisplayNames[$uid] = $this->getUserDisplayName($uid);
108
		}
109
110
		return [
111
			'type' => 'user',
112
			'id' => $uid,
113
			'name' => $this->userDisplayNames[$uid],
114
		];
115
	}
116
117
	/**
118
	 * @param string $uid
119
	 * @return string
120
	 */
121
	protected function getUserDisplayName(string $uid): string {
122
		$user = $this->userManager->get($uid);
123
		if ($user instanceof IUser) {
124
			return $user->getDisplayName();
125
		}
126
		return $uid;
127
	}
128
129
	/**
130
	 * @param string $gid
131
	 * @return array
132
	 */
133
	protected function generateGroupParameter(string $gid): array {
134
		if (!isset($this->groupDisplayNames[$gid])) {
135
			$this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid);
136
		}
137
138
		return [
139
			'type' => 'user-group',
140
			'id' => $gid,
141
			'name' => $this->groupDisplayNames[$gid],
142
		];
143
	}
144
145
	/**
146
	 * @param string $gid
147
	 * @return string
148
	 */
149
	protected function getGroupDisplayName(string $gid): string {
150
		$group = $this->groupManager->get($gid);
151
		if ($group instanceof IGroup) {
152
			return $group->getDisplayName();
153
		}
154
		return $gid;
155
	}
156
}
157