Completed
Push — master ( c8219c...a2e3fa )
by Joas
10s
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\IURLGenerator;
29
use OCP\IUser;
30
use OCP\IUserManager;
31
use OCP\L10N\IFactory;
32
33
class Provider implements IProvider {
34
35
	/** @var IFactory */
36
	protected $languageFactory;
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 IFactory $languageFactory
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(IFactory $languageFactory, 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->languageFactory = $languageFactory;
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 string $language
70
	 * @param IEvent $event
71
	 * @param IEvent|null $previousEvent
72
	 * @return IEvent
73
	 * @throws \InvalidArgumentException
74
	 * @since 11.0.0
75
	 */
76
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
77
		if ($event->getApp() !== 'announcementcenter' || (
78
			$event->getSubject() !== 'announcementsubject' && // 3.1 and later
79
			strpos($event->getSubject(), 'announcementsubject#') !== 0) // 3.0 and before
80
		) {
81
			throw new \InvalidArgumentException();
82
		}
83
84
		$l = $this->languageFactory->get('announcementcenter', $language);
85
86
		$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('announcementcenter', 'announcementcenter-dark.svg')));
87
88
		try {
89
			$announcement = $this->manager->getAnnouncement($event->getObjectId(), true, false, false);
90
91
			$parsedParameters = $this->getParameters($event, $announcement);
92 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...
93
				$subject = $l->t('You announced {announcement}');
94
				unset($parsedParameters['actor']);
95
			} else {
96
				$subject = $l->t('{actor} announced {announcement}');
97
			}
98
			$event->setParsedMessage($announcement['message']);
99
		} catch (\InvalidArgumentException $e) {
100
			$parsedParameters = $this->getParameters($event, []);
101 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...
102
				$subject = $l->t('You posted an announcement');
103
				unset($parsedParameters['actor']);
104
			} else {
105
				$subject = $l->t('{actor} posted an announcement');
106
			}
107
108
			$event->setParsedMessage($l->t('Announcement does not exist anymore'));
109
		}
110
111
112
		$this->setSubjects($event, $subject, $parsedParameters);
113
114
		return $event;
115
	}
116
117
	/**
118
	 * @param IEvent $event
119
	 * @param array $announcement
120
	 * @return array
121
	 */
122
	protected function getParameters(IEvent $event, array $announcement) {
123
		$parameters = $event->getSubjectParameters();
124
125
		if (!empty($announcement)) {
126
			return [
127
				'actor' => $this->generateUserParameter($parameters[0]),
128
				'announcement' => $this->generateAnnouncementParameter($announcement),
129
			];
130
		} else {
131
			return [
132
				'actor' => $this->generateUserParameter($parameters[0]),
133
			];
134
		}
135
	}
136
137
	/**
138
	 * @param IEvent $event
139
	 * @param string $subject
140
	 * @param array $parameters
141
	 */
142
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
143
		$placeholders = $replacements = [];
144
		foreach ($parameters as $placeholder => $parameter) {
145
			$placeholders[] = '{' . $placeholder . '}';
146
			$replacements[] = $parameter['name'];
147
		}
148
149
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
150
			->setRichSubject($subject, $parameters);
151
	}
152
153
	/**
154
	 * @param array $announcement
155
	 * @return array
156
	 */
157
	protected function generateAnnouncementParameter(array $announcement) {
158
		return [
159
			'type' => 'announcement',
160
			'id' => $announcement['id'],
161
			'name' => $announcement['subject'],
162
			'link' => $this->url->linkToRouteAbsolute('announcementcenter.page.index') . '#' . $announcement['id'],
163
		];
164
	}
165
166
	/**
167
	 * @param string $uid
168
	 * @return array
169
	 */
170
	protected function generateUserParameter($uid) {
171
		if (!isset($this->displayNames[$uid])) {
172
			$this->displayNames[$uid] = $this->getDisplayName($uid);
173
		}
174
175
		return [
176
			'type' => 'user',
177
			'id' => $uid,
178
			'name' => $this->displayNames[$uid],
179
		];
180
	}
181
182
	/**
183
	 * @param string $uid
184
	 * @return string
185
	 */
186
	protected function getDisplayName($uid) {
187
		$user = $this->userManager->get($uid);
188
		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...
189
			return $user->getDisplayName();
190
		} else {
191
			return $uid;
192
		}
193
	}
194
}
195