Completed
Pull Request — master (#2406)
by Joas
27:03
created

Base::setSubjects()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 10
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 OCP\Activity\IEvent;
25
use OCP\Activity\IProvider;
26
use OCP\IUser;
27
use OCP\IUserManager;
28
29
abstract class Base implements IProvider {
30
31
	/** @var IUserManager */
32
	protected $userManager;
33
34
	/** @var string[] cached displayNames - key is the UID and value the displayname */
35
	protected $displayNames = [];
36
37
	/**
38
	 * @param IUserManager $userManager
39
	 */
40
	public function __construct(IUserManager $userManager) {
41
		$this->userManager = $userManager;
42
	}
43
44
	/**
45
	 * @param IEvent $event
46
	 * @param string $subject
47
	 * @param array $parameters
48
	 */
49
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
50
		$placeholders = $replacements = [];
51
		foreach ($parameters as $placeholder => $parameter) {
52
			$placeholders[] = '{' . $placeholder . '}';
53
			$replacements[] = $parameter['name'];
54
		}
55
56
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
57
			->setRichSubject($subject, $parameters);
58
	}
59
60
	/**
61
	 * @param array $eventData
62
	 * @return array
63
	 */
64
	protected function generateObjectParameter($eventData) {
65
		if (!is_array($eventData) || !isset($eventData['id']) || !isset($eventData['name'])) {
66
			throw new \InvalidArgumentException();
67
		};
68
69
		return [
70
			'type' => 'calendar-event',
71
			'id' => $eventData['id'],
72
			'name' => $eventData['name'],
73
		];
74
	}
75
76
	/**
77
	 * @param int $id
78
	 * @param string $name
79
	 * @return array
80
	 */
81
	protected function generateCalendarParameter($id, $name) {
82
		return [
83
			'type' => 'calendar',
84
			'id' => $id,
85
			'name' => $name,
86
		];
87
	}
88
89
	/**
90
	 * @param string $id
91
	 * @return array
92
	 */
93
	protected function generateGroupParameter($id) {
94
		return [
95
			'type' => 'group',
96
			'id' => $id,
97
			'name' => $id,
98
		];
99
	}
100
101
	/**
102
	 * @param string $uid
103
	 * @return array
104
	 */
105 View Code Duplication
	protected function generateUserParameter($uid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
		if (!isset($this->displayNames[$uid])) {
107
			$this->displayNames[$uid] = $this->getDisplayName($uid);
108
		}
109
110
		return [
111
			'type' => 'user',
112
			'id' => $uid,
113
			'name' => $this->displayNames[$uid],
114
		];
115
	}
116
117
	/**
118
	 * @param string $uid
119
	 * @return string
120
	 */
121
	protected function getDisplayName($uid) {
122
		$user = $this->userManager->get($uid);
123
		if ($user instanceof IUser) {
124
			return $user->getDisplayName();
125
		} else {
126
			return $uid;
127
		}
128
	}
129
}
130