Completed
Push — master ( 6ddda3...b3ff9a )
by Blizzz
79:33 queued 57:29
created

Base::generateLegacyCalendarParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCA\DAV\CalDAV\Activity\Provider;
23
24
use OCA\DAV\CalDAV\CalDavBackend;
25
use OCP\Activity\IEvent;
26
use OCP\Activity\IProvider;
27
use OCP\IL10N;
28
use OCP\IUser;
29
use OCP\IUserManager;
30
31
abstract class Base implements IProvider {
32
33
	/** @var IUserManager */
34
	protected $userManager;
35
36
	/** @var string[] cached displayNames - key is the UID and value the displayname */
37
	protected $displayNames = [];
38
39
	/**
40
	 * @param IUserManager $userManager
41
	 */
42
	public function __construct(IUserManager $userManager) {
43
		$this->userManager = $userManager;
44
	}
45
46
	/**
47
	 * @param IEvent $event
48
	 * @param string $subject
49
	 * @param array $parameters
50
	 */
51 View Code Duplication
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
52
		$placeholders = $replacements = [];
53
		foreach ($parameters as $placeholder => $parameter) {
54
			$placeholders[] = '{' . $placeholder . '}';
55
			$replacements[] = $parameter['name'];
56
		}
57
58
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
59
			->setRichSubject($subject, $parameters);
60
	}
61
62
	/**
63
	 * @param array $eventData
64
	 * @return array
65
	 */
66
	protected function generateObjectParameter($eventData) {
67
		if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) {
68
			throw new \InvalidArgumentException();
69
		}
70
71
		return [
72
			'type' => 'calendar-event',
73
			'id' => $eventData['id'],
74
			'name' => $eventData['name'],
75
		];
76
	}
77
78
	/**
79
	 * @param array $data
80
	 * @param IL10N $l
81
	 * @return array
82
	 */
83
	protected function generateCalendarParameter($data, IL10N $l) {
84
		if ($data['uri'] === CalDavBackend::PERSONAL_CALENDAR_URI &&
85
			$data['name'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
86
			return [
87
				'type' => 'calendar',
88
				'id' => $data['id'],
89
				'name' => $l->t('Personal'),
90
			];
91
		}
92
93
		return [
94
			'type' => 'calendar',
95
			'id' => $data['id'],
96
			'name' => $data['name'],
97
		];
98
	}
99
100
	/**
101
	 * @param int $id
102
	 * @param string $name
103
	 * @return array
104
	 */
105
	protected function generateLegacyCalendarParameter($id, $name) {
106
		return [
107
			'type' => 'calendar',
108
			'id' => $id,
109
			'name' => $name,
110
		];
111
	}
112
113
	/**
114
	 * @param string $id
115
	 * @return array
116
	 */
117
	protected function generateGroupParameter($id) {
118
		return [
119
			'type' => 'group',
120
			'id' => $id,
121
			'name' => $id,
122
		];
123
	}
124
125
	/**
126
	 * @param string $uid
127
	 * @return array
128
	 */
129 View Code Duplication
	protected function generateUserParameter($uid) {
130
		if (!isset($this->displayNames[$uid])) {
131
			$this->displayNames[$uid] = $this->getDisplayName($uid);
132
		}
133
134
		return [
135
			'type' => 'user',
136
			'id' => $uid,
137
			'name' => $this->displayNames[$uid],
138
		];
139
	}
140
141
	/**
142
	 * @param string $uid
143
	 * @return string
144
	 */
145
	protected function getDisplayName($uid) {
146
		$user = $this->userManager->get($uid);
147
		if ($user instanceof IUser) {
148
			return $user->getDisplayName();
149
		} else {
150
			return $uid;
151
		}
152
	}
153
}
154