Completed
Pull Request — master (#63)
by Joas
02:22
created

Provider::getDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
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\AnnouncementCenter\Activity;
23
24
use OCA\AnnouncementCenter\Manager;
25
use OCP\Activity\IEvent;
26
use OCP\Activity\IManager;
27
use OCP\Activity\IProvider;
28
use OCP\IL10N;
29
use OCP\IURLGenerator;
30
use OCP\IUser;
31
use OCP\IUserManager;
32
33
class Provider implements IProvider {
34
35
	/** @var IL10N */
36
	protected $l;
37
38
	/** @var IURLGenerator */
39
	protected $url;
40
41
	/** @var IManager */
42
	protected $activityManager;
43
44
	/** @var IUserManager */
45
	protected $userManager;
46
47
	/** @var Manager */
48
	protected $manager;
49
50
	/** @var string[] */
51
	protected $displayNames = [];
52
53
	/**
54
	 * @param IL10N $l
55
	 * @param IURLGenerator $url
56
	 * @param IManager $activityManager
57
	 * @param IUserManager $userManager
58
	 * @param Manager $manager
59
	 */
60 1 View Code Duplication
	public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, Manager $manager) {
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...
61 1
		$this->l = $l;
62 1
		$this->url = $url;
63 1
		$this->activityManager = $activityManager;
64 1
		$this->userManager = $userManager;
65 1
		$this->manager = $manager;
66 1
	}
67
68
	/**
69
	 * @param IEvent $event
70
	 * @param IEvent|null $previousEvent
71
	 * @return IEvent
72
	 * @throws \InvalidArgumentException
73
	 * @since 11.0.0
74
	 */
75
	public function parse(IEvent $event, IEvent $previousEvent = null) {
76
		if ($event->getApp() !== 'announcementcenter' || (
77
			$event->getSubject() !== 'announcementsubject' && // 3.1 and later
78
			strpos($event->getSubject(), 'announcementsubject#') !== 0) // 3.0 and before
79
		) {
80
			throw new \InvalidArgumentException();
81
		}
82
83
		$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
84
85
		try {
86
			$announcement = $this->manager->getAnnouncement($event->getObjectId(), true, false, false);
87
88
			$parsedParameters = $this->getParameters($event, $announcement);
89 View Code Duplication
			if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90
				$subject = $this->l->t('You announced {announcement}');
91
				unset($parsedParameters['actor']);
92
			} else {
93
				$subject = $this->l->t('{actor} announced {announcement}');
94
			}
95
			$event->setParsedMessage($announcement['message']);
96
		} catch (\InvalidArgumentException $e) {
97
			$parsedParameters = $this->getParameters($event, []);
98 View Code Duplication
			if ($parsedParameters['actor']['id'] === $this->activityManager->getCurrentUserId()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
99
				$subject = $this->l->t('You posted an announcement');
100
				unset($parsedParameters['actor']);
101
			} else {
102
				$subject = $this->l->t('{actor} posted an announcement');
103
			}
104
105
			$event->setParsedMessage($this->l->t('Announcement does not exist anymore'));
106
		}
107
108
109
		$this->setSubjects($event, $subject, $parsedParameters);
110
111
		return $event;
112
	}
113
114
	/**
115
	 * @param IEvent $event
116
	 * @param array $announcement
117
	 * @return array
118
	 */
119
	protected function getParameters(IEvent $event, array $announcement) {
120
		$parameters = $event->getSubjectParameters();
121
122
		if (!empty($announcement)) {
123
			return [
124
				'actor' => $this->generateUserParameter($parameters[0]),
125
				'announcement' => $this->generateAnnouncementParameter($announcement),
126
			];
127
		} else {
128
			return [
129
				'actor' => $this->generateUserParameter($parameters[0]),
130
			];
131
		}
132
	}
133
134
	/**
135
	 * @param IEvent $event
136
	 * @param string $subject
137
	 * @param array $parameters
138
	 */
139
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
140
		$placeholders = $replacements = [];
141
		foreach ($parameters as $placeholder => $parameter) {
142
			$placeholders[] = '{' . $placeholder . '}';
143
			$replacements[] = $parameter['name'];
144
		}
145
146
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
147
			->setRichSubject($subject, $parameters);
148
	}
149
150
	/**
151
	 * @param array $announcement
152
	 * @return array
153
	 */
154
	protected function generateAnnouncementParameter(array $announcement) {
155
		return [
156
			'type' => 'announcement',
157
			'id' => $announcement['id'],
158
			'name' => $announcement['subject'],
159
			'link' => $this->url->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $announcement['id'],
160
		];
161
	}
162
163
	/**
164
	 * @param string $uid
165
	 * @return array
166
	 */
167
	protected function generateUserParameter($uid) {
168
		if (!isset($this->displayNames[$uid])) {
169
			$this->displayNames[$uid] = $this->getDisplayName($uid);
170
		}
171
172
		return [
173
			'type' => 'user',
174
			'id' => $uid,
175
			'name' => $this->displayNames[$uid],
176
		];
177
	}
178
179
	/**
180
	 * @param string $uid
181
	 * @return string
182
	 */
183
	protected function getDisplayName($uid) {
184
		$user = $this->userManager->get($uid);
185
		if ($user instanceof IUser) {
0 ignored issues
show
Bug introduced by
The class OCP\IUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
186
			return $user->getDisplayName();
187
		} else {
188
			return $uid;
189
		}
190
	}
191
}
192