Completed
Push — master ( 0ee0a5...f5691e )
by Julius
23s queued 10s
created

DeckProvider::parse()   B

Complexity

Conditions 8
Paths 65

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 7.4755
c 0
b 0
f 0
cc 8
nc 65
nop 3

How to fix   Long Method   

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\IURLGenerator;
32
use OCP\IUserManager;
33
34
class DeckProvider implements IProvider {
35
36
	/** @var string */
37
	private $userId;
38
	/** @var IURLGenerator */
39
	private $urlGenerator;
40
	/** @var ActivityManager */
41
	private $activityManager;
42
	/** @var IUserManager */
43
	private $userManager;
44
45
	public function __construct(IURLGenerator $urlGenerator, ActivityManager $activityManager, IUserManager $userManager, $userId) {
46
		$this->userId = $userId;
47
		$this->urlGenerator = $urlGenerator;
48
		$this->activityManager = $activityManager;
49
		$this->userManager = $userManager;
50
	}
51
52
	/**
53
	 * @param string $language The language which should be used for translating, e.g. "en"
54
	 * @param IEvent $event The current event which should be parsed
55
	 * @param IEvent|null $previousEvent A potential previous event which you can combine with the current one.
56
	 *                                   To do so, simply use setChildEvent($previousEvent) after setting the
57
	 *                                   combined subject on the current event.
58
	 * @return IEvent
59
	 * @throws \InvalidArgumentException Should be thrown if your provider does not know this event
60
	 * @since 11.0.0
61
	 */
62
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
63
		if ($event->getApp() !== 'deck') {
64
			throw new \InvalidArgumentException();
65
		}
66
67
		$event = $this->getIcon($event);
68
69
		$subjectIdentifier = $event->getSubject();
70
		$subjectParams = $event->getSubjectParameters();
71
		$ownActivity = ($event->getAuthor() === $this->userId);
72
73
		/**
74
		 * Map stored parameter objects to rich string types
75
		 */
76
		$board = null;
77
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_BOARD) {
78
			$board = [
79
				'type' => 'highlight',
80
				'id' => $event->getObjectId(),
81
				'name' => $event->getObjectName(),
82
				'link' => $this->deckUrl('/board/' . $event->getObjectId()),
83
			];
84
		}
85
86
		$card = null;
87
		if ($event->getObjectType() === ActivityManager::DECK_OBJECT_CARD) {
88
			$card = [
89
				'type' => 'highlight',
90
				'id' => $event->getObjectId(),
91
				'name' => $event->getObjectName(),
92
			];
93
94
			if (array_key_exists('board', $subjectParams)) {
95
				$archivedParam = $subjectParams['card']['archived'] ? 'archived' : '';
96
				$card['link'] = $this->deckUrl('/board/' . $subjectParams['board']['id'] . '/' . $archivedParam . '/card/' . $event->getObjectId());
97
			}
98
		}
99
100
		$author = $event->getAuthor();
101
		$user = $this->userManager->get($author);
102
		$params = [
103
			'board' => $board,
104
			'card' => $card,
105
			'user' => [
106
				'type' => 'user',
107
				'id' => $author,
108
				'name' => $user !== null ? $user->getDisplayName() : $author
109
			]
110
		];
111
112
		$params = $this->parseParamForBoard('board', $subjectParams, $params);
113
		$params = $this->parseParamForStack('stack', $subjectParams, $params);
114
		$params = $this->parseParamForStack('stackBefore', $subjectParams, $params);
115
		$params = $this->parseParamForAttachment('attachment', $subjectParams, $params);
116
		$params = $this->parseParamForLabel($subjectParams, $params);
117
		$params = $this->parseParamForAssignedUser($subjectParams, $params);
118
		$params = $this->parseParamForAcl($subjectParams, $params);
119
		$params = $this->parseParamForChanges($subjectParams, $params, $event);
120
121
		try {
122
			$subject = $this->activityManager->getActivityFormat($subjectIdentifier, $subjectParams, $ownActivity);
123
			$event->setParsedSubject($subject);
124
			$event->setRichSubject($subject, $params);
125
		} catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
126
		}
127
		return $event;
128
	}
129
130
	private function getIcon(IEvent $event) {
131
		$event->setIcon($this->urlGenerator->imagePath('deck', 'deck-dark.svg'));
132 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...
133
			$event->setIcon($this->urlGenerator->imagePath('files', 'change.svg'));
134
		}
135 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...
136
			$event->setIcon($this->urlGenerator->imagePath('files', 'add-color.svg'));
137
		}
138 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...
139
			$event->setIcon($this->urlGenerator->imagePath('files', 'delete-color.svg'));
140
		}
141 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...
142
			$event->setIcon($this->urlGenerator->imagePath('deck', 'archive.svg'));
143
		}
144 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...
145
			$event->setIcon($this->urlGenerator->imagePath('core', 'actions/history.svg'));
146
		}
147 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...
148
			$event->setIcon($this->urlGenerator->imagePath('core', 'places/files.svg'));
149
		}
150
		return $event;
151
	}
152
153
	private function parseParamForBoard($paramName, $subjectParams, $params) {
154
		if (array_key_exists($paramName, $subjectParams)) {
155
			$params[$paramName] = [
156
				'type' => 'highlight',
157
				'id' => $subjectParams[$paramName]['id'],
158
				'name' => $subjectParams[$paramName]['title'],
159
				'link' => $this->deckUrl('/board/' . $subjectParams[$paramName]['id'] . '/'),
160
			];
161
		}
162
		return $params;
163
	}
164
	private function parseParamForStack($paramName, $subjectParams, $params) {
165
		if (array_key_exists($paramName, $subjectParams)) {
166
			$params[$paramName] = [
167
				'type' => 'highlight',
168
				'id' => $subjectParams[$paramName]['id'],
169
				'name' => $subjectParams[$paramName]['title'],
170
			];
171
		}
172
		return $params;
173
	}
174
175
	private function parseParamForAttachment($paramName, $subjectParams, $params) {
176
		if (array_key_exists($paramName, $subjectParams)) {
177
			$params[$paramName] = [
178
				'type' => 'highlight',
179
				'id' => $subjectParams[$paramName]['id'],
180
				'name' => $subjectParams[$paramName]['data'],
181
				'link' => $this->urlGenerator->linkToRoute('deck.attachment.display', ['cardId' => $subjectParams['card']['id'], 'attachmentId' => $subjectParams['attachment']['id']]),
182
			];
183
		}
184
		return $params;
185
	}
186
187
	private function parseParamForAssignedUser($subjectParams, $params) {
188
		if (array_key_exists('assigneduser', $subjectParams)) {
189
			$user = $this->userManager->get($subjectParams['assigneduser']);
190
			$params['assigneduser'] = [
191
				'type' => 'user',
192
				'id' => $subjectParams['assigneduser'],
193
				'name' => $user !== null ? $user->getDisplayName() : $subjectParams['assigneduser']
194
			];
195
		}
196
		return $params;
197
	}
198
199
	private function parseParamForLabel($subjectParams, $params) {
200 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...
201
			$params['label'] = [
202
				'type' => 'highlight',
203
				'id' => $subjectParams['label']['id'],
204
				'name' => $subjectParams['label']['title']
205
			];
206
		}
207
		return $params;
208
	}
209
210
	private function parseParamForAcl($subjectParams, $params) {
211
		if (array_key_exists('acl', $subjectParams)) {
212
			if ($subjectParams['acl']['type'] === Acl::PERMISSION_TYPE_USER) {
213
				$user = $this->userManager->get($subjectParams['acl']['participant']);
214
				$params['acl'] = [
215
					'type' => 'user',
216
					'id' => $subjectParams['acl']['participant'],
217
					'name' => $user !== null ? $user->getDisplayName() : $subjectParams['acl']['participant']
218
				];
219 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...
220
				$params['acl'] = [
221
					'type' => 'highlight',
222
					'id' => $subjectParams['acl']['participant'],
223
					'name' => $subjectParams['acl']['participant']
224
				];
225
			}
226
		}
227
		return $params;
228
	}
229
230
	/**
231
	 * Add diff to message if the subject parameter 'diff' is set, otherwise
232
	 * the changed values are added to before/after
233
	 *
234
	 * @param $subjectParams
235
	 * @param $params
236
	 * @return mixed
237
	 */
238
	private function parseParamForChanges($subjectParams, $params, $event) {
239
		if (array_key_exists('diff', $subjectParams) && $subjectParams['diff']) {
240
			$diff = new Diff();
241
			$event->setMessage($subjectParams['after']);
242
			$event->setParsedMessage('<pre class="visualdiff">' . $diff->render($subjectParams['before'], $subjectParams['after']) . '</pre>');
243
			return $params;
244
		}
245 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...
246
			$params['before'] = [
247
				'type' => 'highlight',
248
				'id' => $subjectParams['before'],
249
				'name' => $subjectParams['before']
250
			];
251
		}
252 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...
253
			$params['after'] = [
254
				'type' => 'highlight',
255
				'id' => $subjectParams['after'],
256
				'name' => $subjectParams['after']
257
			];
258
		}
259
		return $params;
260
	}
261
262
	public function deckUrl($endpoint) {
263
		return $this->urlGenerator->linkToRoute('deck.page.index') . '#!' . $endpoint;
264
	}
265
}
266