Passed
Push — master ( 3c1d1d...773375 )
by Julius
02:57
created

DeckProvider::parse()   D

Complexity

Conditions 12
Paths 253

Size

Total Lines 76

Duplication

Lines 6
Ratio 7.89 %

Importance

Changes 0
Metric Value
dl 6
loc 76
rs 4.9003
c 0
b 0
f 0
cc 12
nc 253
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Activity;
25
26
27
use cogpowered\FineDiff\Diff;
28
use OCA\Deck\Db\Acl;
29
use OCP\Activity\IEvent;
30
use OCP\Activity\IProvider;
31
use OCP\Comments\IComment;
32
use OCP\Comments\ICommentsManager;
33
use OCP\Comments\NotFoundException;
34
use OCP\IURLGenerator;
35
use OCP\IUserManager;
36
37
class DeckProvider implements IProvider {
38
39
	/** @var string */
40
	private $userId;
41
	/** @var IURLGenerator */
42
	private $urlGenerator;
43
	/** @var ActivityManager */
44
	private $activityManager;
45
	/** @var IUserManager */
46
	private $userManager;
47
	/** @var ICommentsManager */
48
	private $commentsManager;
49
50
	public function __construct(IURLGenerator $urlGenerator, ActivityManager $activityManager, IUserManager $userManager, ICommentsManager $commentsManager, $userId) {
51
		$this->userId = $userId;
52
		$this->urlGenerator = $urlGenerator;
53
		$this->activityManager = $activityManager;
54
		$this->commentsManager = $commentsManager;
55
		$this->userManager = $userManager;
56
	}
57
58
	/**
59
	 * @param string $language The language which should be used for translating, e.g. "en"
60
	 * @param IEvent $event The current event which should be parsed
61
	 * @param IEvent|null $previousEvent A potential previous event which you can combine with the current one.
62
	 *                                   To do so, simply use setChildEvent($previousEvent) after setting the
63
	 *                                   combined subject on the current event.
64
	 * @return IEvent
65
	 * @throws \InvalidArgumentException Should be thrown if your provider does not know this event
66
	 * @since 11.0.0
67
	 */
68
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
69
		if ($event->getApp() !== 'deck') {
70
			throw new \InvalidArgumentException();
71
		}
72
73
		$event = $this->getIcon($event);
74
75
		$subjectIdentifier = $event->getSubject();
76
		$subjectParams = $event->getSubjectParameters();
77
		$ownActivity = ($event->getAuthor() === $this->userId);
78
79
		/**
80
		 * Map stored parameter objects to rich string types
81
		 */
82
83
		$author = $event->getAuthor();
84
		// get author if
85
		if ($author === '' && array_key_exists('author', $subjectParams)) {
86
			$author = $subjectParams['author'];
87
			unset($subjectParams['author']);
88
		}
89
		$user = $this->userManager->get($author);
90
		$params = [
91
			'user' => [
92
				'type' => 'user',
93
				'id' => $author,
94
				'name' => $user !== null ? $user->getDisplayName() : $author
95
			],
96
		];
97
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_BOARD) {
98 View Code Duplication
			if ($event->getObjectName() === '') {
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
				$event->setObject($event->getObjectType(), $event->getObjectId(), $subjectParams['board']['title']);
100
			}
101
			$board = [
102
				'type' => 'highlight',
103
				'id' => $event->getObjectId(),
104
				'name' => $event->getObjectName(),
105
				'link' => $this->deckUrl('/board/' . $event->getObjectId()),
106
			];
107
			$params['board'] = $board;
108
		}
109
110
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_CARD) {
111 View Code Duplication
			if ($event->getObjectName() === '') {
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...
112
				$event->setObject($event->getObjectType(), $event->getObjectId(), $subjectParams['card']['title']);
113
			}
114
			$card = [
115
				'type' => 'highlight',
116
				'id' => $event->getObjectId(),
117
				'name' => $event->getObjectName(),
118
			];
119
120
			if (array_key_exists('board', $subjectParams)) {
121
				$archivedParam = $subjectParams['card']['archived'] ? 'archived' : '';
122
				$card['link'] = $this->deckUrl('/board/' . $subjectParams['board']['id'] . '/' . $archivedParam . '/card/' . $event->getObjectId());
123
			}
124
			$params['card'] = $card;
125
		}
126
127
		$params = $this->parseParamForBoard('board', $subjectParams, $params);
128
		$params = $this->parseParamForStack('stack', $subjectParams, $params);
129
		$params = $this->parseParamForStack('stackBefore', $subjectParams, $params);
130
		$params = $this->parseParamForAttachment('attachment', $subjectParams, $params);
131
		$params = $this->parseParamForLabel($subjectParams, $params);
132
		$params = $this->parseParamForAssignedUser($subjectParams, $params);
133
		$params = $this->parseParamForAcl($subjectParams, $params);
134
		$params = $this->parseParamForChanges($subjectParams, $params, $event);
135
		$params = $this->parseParamForComment($subjectParams, $params, $event);
136
137
		try {
138
			$subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity);
139
			$this->setSubjects($event, $subject, $params);
140
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
141
		}
142
		return $event;
143
	}
144
145
	/**
146
	 * @param IEvent $event
147
	 * @param string $subject
148
	 * @param array $parameters
149
	 */
150
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
151
		$placeholders = $replacements = [];
152
		foreach ($parameters as $placeholder => $parameter) {
153
			$placeholders[] = '{' . $placeholder . '}';
154
			if (is_array($parameter) && array_key_exists('name', $parameter)) {
155
				$replacements[] = $parameter['name'];
156
			} else {
157
				$replacements[] = '';
158
			}
159
		}
160
161
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
162
			->setRichSubject($subject, $parameters);
163
		$event->setSubject($subject, $parameters);
164
	}
165
166
	private function getIcon(IEvent $event) {
167
		$event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg'));
168 View Code Duplication
		if (strpos($event->getSubject(), '_update') !== false) {
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...
169
			$event->setIcon($this->urlGenerator->imagePath('files', 'change.svg'));
170
		}
171 View Code Duplication
		if (strpos($event->getSubject(), '_create') !== false) {
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...
172
			$event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg'));
173
		}
174 View Code Duplication
		if (strpos($event->getSubject(), '_delete') !== false) {
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...
175
			$event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg'));
176
		}
177 View Code Duplication
		if (strpos($event->getSubject(), 'archive') !== false) {
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...
178
			$event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg'));
179
		}
180 View Code Duplication
		if (strpos($event->getSubject(), '_restore') !== false) {
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...
181
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg'));
182
		}
183 View Code Duplication
		if (strpos($event->getSubject(), 'attachment_') !== false) {
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...
184
			$event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg'));
185
		}
186
		if (strpos($event->getSubject(), 'comment_') !== false) {
187
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/comment.svg'));
188
		}
189
		return $event;
190
	}
191
192
	private function parseParamForBoard($paramName, $subjectParams, $params) {
193
		if (array_key_exists($paramName, $subjectParams)) {
194
			$params[$paramName] = [
195
				'type' => 'highlight',
196
				'id' => $subjectParams[$paramName]['id'],
197
				'name' => $subjectParams[$paramName]['title'],
198
				'link' => $this->deckUrl('/board/' . $subjectParams[$paramName]['id'] . '/'),
199
			];
200
		}
201
		return $params;
202
	}
203
	private function parseParamForStack($paramName, $subjectParams, $params) {
204
		if (array_key_exists($paramName, $subjectParams)) {
205
			$params[$paramName] = [
206
				'type' => 'highlight',
207
				'id' => $subjectParams[$paramName]['id'],
208
				'name' => $subjectParams[$paramName]['title'],
209
			];
210
		}
211
		return $params;
212
	}
213
214
	private function parseParamForAttachment($paramName, $subjectParams, $params) {
215
		if (array_key_exists($paramName, $subjectParams)) {
216
			$params[$paramName] = [
217
				'type' => 'highlight',
218
				'id' => $subjectParams[$paramName]['id'],
219
				'name' => $subjectParams[$paramName]['data'],
220
				'link' => $this->urlGenerator->linkToRoute('deck.attachment.display', ['cardId' => $subjectParams['card']['id'], 'attachmentId' => $subjectParams['attachment']['id']]),
221
			];
222
		}
223
		return $params;
224
	}
225
226
	private function parseParamForAssignedUser($subjectParams, $params) {
227
		if (array_key_exists('assigneduser', $subjectParams)) {
228
			$user = $this->userManager->get($subjectParams['assigneduser']);
229
			$params['assigneduser'] = [
230
				'type' => 'user',
231
				'id' => $subjectParams['assigneduser'],
232
				'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser']
233
			];
234
		}
235
		return $params;
236
	}
237
238
	private function parseParamForLabel($subjectParams, $params) {
239 View Code Duplication
		if (array_key_exists('label', $subjectParams)) {
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...
240
			$params['label'] = [
241
				'type' => 'highlight',
242
				'id' => $subjectParams['label']['id'],
243
				'name' => $subjectParams['label']['title']
244
			];
245
		}
246
		return $params;
247
	}
248
249
	private function parseParamForAcl($subjectParams, $params) {
250
		if (array_key_exists('acl', $subjectParams)) {
251
			if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) {
252
				$user = $this->userManager->get($subjectParams['acl']['participant']);
253
				$params['acl'] = [
254
					'type' => 'user',
255
					'id' => $subjectParams['acl']['participant'],
256
					'name' => $user !== null ? $user->getDisplayName() : $subjectParams['acl']['participant']
257
				];
258 View Code Duplication
			} else {
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...
259
				$params['acl'] = [
260
					'type' => 'highlight',
261
					'id' => $subjectParams['acl']['participant'],
262
					'name' => $subjectParams['acl']['participant']
263
				];
264
			}
265
		}
266
		return $params;
267
	}
268
269
	private function parseParamForComment($subjectParams, $params, IEvent $event) {
270
		if (array_key_exists('comment', $subjectParams)) {
271
			/** @var IComment $comment */
272
			try {
273
				$comment = $this->commentsManager->get((int)$subjectParams['comment']);
274
				$event->setParsedMessage($comment->getMessage());
275
			} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
276
			}
277
			$params['comment'] = $subjectParams['comment'];
278
		}
279
		return $params;
280
	}
281
282
	/**
283
	 * Add diff to message if the subject parameter 'diff' is set, otherwise
284
	 * the changed values are added to before/after
285
	 *
286
	 * @param $subjectParams
287
	 * @param $params
288
	 * @return mixed
289
	 */
290
	private function parseParamForChanges($subjectParams, $params, $event) {
291
		if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) {
292
			$diff = new Diff();
293
			// Don't add diff as message since we are limited to 255 chars here
294
			//$event->setMessage($subjectParams['after']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
90% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
295
			$event->setParsedMessage('<pre class="visualdiff">' . $diff->render($subjectParams['before'], $subjectParams['after']) . '</pre>');
296
			return $params;
297
		}
298 View Code Duplication
		if (array_key_exists('before', $subjectParams)) {
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...
299
			$params['before'] = [
300
				'type' => 'highlight',
301
				'id' => $subjectParams['before'],
302
				'name' => $subjectParams['before']
303
			];
304
		}
305 View Code Duplication
		if (array_key_exists('after', $subjectParams)) {
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...
306
			$params['after'] = [
307
				'type' => 'highlight',
308
				'id' => $subjectParams['after'],
309
				'name' => $subjectParams['after']
310
			];
311
		}
312
		return $params;
313
	}
314
315
	public function deckUrl($endpoint) {
316
		return $this->urlGenerator->linkToRouteAbsolute('deck.page.index') . '#!' . $endpoint;
317
	}
318
}
319