Completed
Push — master ( a6da1d...5371a0 )
by Julius
03:46 queued 46s
created

DeckProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
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
		$board = null;
83
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_BOARD) {
84
			$board = [
85
				'type' => 'highlight',
86
				'id' => $event->getObjectId(),
87
				'name' => $event->getObjectName(),
88
				'link' => $this->deckUrl('/board/' . $event->getObjectId()),
89
			];
90
		}
91
92
		$card = null;
93
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_CARD) {
94
			$card = [
95
				'type' => 'highlight',
96
				'id' => $event->getObjectId(),
97
				'name' => $event->getObjectName(),
98
			];
99
100
			if (array_key_exists('board', $subjectParams)) {
101
				$archivedParam = $subjectParams['card']['archived'] ? 'archived' : '';
102
				$card['link'] = $this->deckUrl('/board/' . $subjectParams['board']['id'] . '/' . $archivedParam . '/card/' . $event->getObjectId());
103
			}
104
		}
105
106
		$author = $event->getAuthor();
107
		$user = $this->userManager->get($author);
108
		$params = [
109
			'board' => $board,
110
			'card' => $card,
111
			'user' => [
112
				'type' => 'user',
113
				'id' => $author,
114
				'name' => $user !== null ? $user->getDisplayName() : $author
115
			],
116
		];
117
118
		$params = $this->parseParamForBoard('board', $subjectParams, $params);
119
		$params = $this->parseParamForStack('stack', $subjectParams, $params);
120
		$params = $this->parseParamForStack('stackBefore', $subjectParams, $params);
121
		$params = $this->parseParamForAttachment('attachment', $subjectParams, $params);
122
		$params = $this->parseParamForLabel($subjectParams, $params);
123
		$params = $this->parseParamForAssignedUser($subjectParams, $params);
124
		$params = $this->parseParamForAcl($subjectParams, $params);
125
		$params = $this->parseParamForChanges($subjectParams, $params, $event);
126
		$params = $this->parseParamForComment($subjectParams, $params, $event);
127
128
		try {
129
			$subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity);
130
			$this->setSubjects($event, $subject, $params);
131
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
132
		}
133
		return $event;
134
	}
135
136
	/**
137
	 * @param IEvent $event
138
	 * @param string $subject
139
	 * @param array $parameters
140
	 */
141
	protected function setSubjects(IEvent $event, $subject, array $parameters) {
142
		$placeholders = $replacements = [];
143
		foreach ($parameters as $placeholder => $parameter) {
144
			$placeholders[] = '{' . $placeholder . '}';
145
			$replacements[] = $parameter['name'];
146
		}
147
148
		$event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
149
			->setRichSubject($subject, $parameters);
150
	}
151
152
	private function getIcon(IEvent $event) {
153
		$event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg'));
154 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...
155
			$event->setIcon($this->urlGenerator->imagePath('files', 'change.svg'));
156
		}
157 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...
158
			$event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg'));
159
		}
160 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...
161
			$event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg'));
162
		}
163 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...
164
			$event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg'));
165
		}
166 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...
167
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg'));
168
		}
169 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...
170
			$event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg'));
171
		}
172
		if (strpos($event->getSubject(), 'comment_') !== false) {
173
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/comment.svg'));
174
		}
175
		return $event;
176
	}
177
178
	private function parseParamForBoard($paramName, $subjectParams, $params) {
179
		if (array_key_exists($paramName, $subjectParams)) {
180
			$params[$paramName] = [
181
				'type' => 'highlight',
182
				'id' => $subjectParams[$paramName]['id'],
183
				'name' => $subjectParams[$paramName]['title'],
184
				'link' => $this->deckUrl('/board/' . $subjectParams[$paramName]['id'] . '/'),
185
			];
186
		}
187
		return $params;
188
	}
189
	private function parseParamForStack($paramName, $subjectParams, $params) {
190
		if (array_key_exists($paramName, $subjectParams)) {
191
			$params[$paramName] = [
192
				'type' => 'highlight',
193
				'id' => $subjectParams[$paramName]['id'],
194
				'name' => $subjectParams[$paramName]['title'],
195
			];
196
		}
197
		return $params;
198
	}
199
200
	private function parseParamForAttachment($paramName, $subjectParams, $params) {
201
		if (array_key_exists($paramName, $subjectParams)) {
202
			$params[$paramName] = [
203
				'type' => 'highlight',
204
				'id' => $subjectParams[$paramName]['id'],
205
				'name' => $subjectParams[$paramName]['data'],
206
				'link' => $this->urlGenerator->linkToRoute('deck.attachment.display', ['cardId' => $subjectParams['card']['id'], 'attachmentId' => $subjectParams['attachment']['id']]),
207
			];
208
		}
209
		return $params;
210
	}
211
212
	private function parseParamForAssignedUser($subjectParams, $params) {
213
		if (array_key_exists('assigneduser', $subjectParams)) {
214
			$user = $this->userManager->get($subjectParams['assigneduser']);
215
			$params['assigneduser'] = [
216
				'type' => 'user',
217
				'id' => $subjectParams['assigneduser'],
218
				'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser']
219
			];
220
		}
221
		return $params;
222
	}
223
224
	private function parseParamForLabel($subjectParams, $params) {
225 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...
226
			$params['label'] = [
227
				'type' => 'highlight',
228
				'id' => $subjectParams['label']['id'],
229
				'name' => $subjectParams['label']['title']
230
			];
231
		}
232
		return $params;
233
	}
234
235
	private function parseParamForAcl($subjectParams, $params) {
236
		if (array_key_exists('acl', $subjectParams)) {
237
			if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) {
238
				$user = $this->userManager->get($subjectParams['acl']['participant']);
239
				$params['acl'] = [
240
					'type' => 'user',
241
					'id' => $subjectParams['acl']['participant'],
242
					'name' => $user !== null ? $user->getDisplayName() : $subjectParams['acl']['participant']
243
				];
244 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...
245
				$params['acl'] = [
246
					'type' => 'highlight',
247
					'id' => $subjectParams['acl']['participant'],
248
					'name' => $subjectParams['acl']['participant']
249
				];
250
			}
251
		}
252
		return $params;
253
	}
254
255
	private function parseParamForComment($subjectParams, $params, IEvent $event) {
256
		if (array_key_exists('comment', $subjectParams)) {
257
			/** @var IComment $comment */
258
			try {
259
				$comment = $this->commentsManager->get((int)$subjectParams['comment']);
260
				$event->setParsedMessage($comment->getMessage());
261
			} catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
262
			}
263
			$params['comment'] = $subjectParams['comment'];
264
		}
265
		return $params;
266
	}
267
268
	/**
269
	 * Add diff to message if the subject parameter 'diff' is set, otherwise
270
	 * the changed values are added to before/after
271
	 *
272
	 * @param $subjectParams
273
	 * @param $params
274
	 * @return mixed
275
	 */
276
	private function parseParamForChanges($subjectParams, $params, $event) {
277
		if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) {
278
			$diff = new Diff();
279
			$event->setMessage($subjectParams['after']);
280
			$event->setParsedMessage('<pre class="visualdiff">' . $diff->render($subjectParams['before'], $subjectParams['after']) . '</pre>');
281
			return $params;
282
		}
283 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...
284
			$params['before'] = [
285
				'type' => 'highlight',
286
				'id' => $subjectParams['before'],
287
				'name' => $subjectParams['before']
288
			];
289
		}
290 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...
291
			$params['after'] = [
292
				'type' => 'highlight',
293
				'id' => $subjectParams['after'],
294
				'name' => $subjectParams['after']
295
			];
296
		}
297
		return $params;
298
	}
299
300
	public function deckUrl($endpoint) {
301
		return $this->urlGenerator->linkToRoute('deck.page.index') . '#!' . $endpoint;
302
	}
303
}
304