Completed
Pull Request — master (#2406)
by Joas
27:03
created

Todo::getParameters()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 28
Code Lines 22

Duplication

Lines 5
Ratio 17.86 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 11
nop 1
dl 5
loc 28
rs 5.2653
c 0
b 0
f 0

How to fix   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) 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\DAV\CalDAV\Activity\Provider;
23
24
use OCP\Activity\IEvent;
25
26
class Todo extends Event {
27
28
	/**
29
	 * @param IEvent $event
30
	 * @param IEvent|null $previousEvent
31
	 * @return IEvent
32
	 * @throws \InvalidArgumentException
33
	 * @since 11.0.0
34
	 */
35
	public function parse(IEvent $event, IEvent $previousEvent = null) {
36
		if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_todo') {
37
			throw new \InvalidArgumentException();
38
		}
39
40
		$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.svg')));
41
42 View Code Duplication
		if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo') {
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...
43
			$subject = $this->l->t('{actor} created todo {todo} in list {calendar}');
44
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo_self') {
45
			$subject = $this->l->t('You created todo {todo} in list {calendar}');
46
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo') {
47
			$subject = $this->l->t('{actor} deleted todo {todo} from list {calendar}');
48
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo_self') {
49
			$subject = $this->l->t('You deleted todo {todo} from list {calendar}');
50
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo') {
51
			$subject = $this->l->t('{actor} updated todo {todo} in list {calendar}');
52
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_self') {
53
			$subject = $this->l->t('You updated todo {todo} in list {calendar}');
54
55
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed') {
56
			$subject = $this->l->t('{actor} solved todo {todo} in list {calendar}');
57
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self') {
58
			$subject = $this->l->t('You solved todo {todo} in list {calendar}');
59
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action') {
60
			$subject = $this->l->t('{actor} reopened todo {todo} in list {calendar}');
61
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self') {
62
			$subject = $this->l->t('You reopened todo {todo} in list {calendar}');
63
		} else {
64
			throw new \InvalidArgumentException();
65
		}
66
67
		$parsedParameters = $this->getParameters($event);
68
		$this->setSubjects($event, $subject, $parsedParameters);
69
70
		$event = $this->eventMerger->mergeEvents('todo', $event, $previousEvent);
71
72
		return $event;
73
	}
74
75
	/**
76
	 * @param IEvent $event
77
	 * @return array
78
	 */
79
	protected function getParameters(IEvent $event) {
80
		$subject = $event->getSubject();
81
		$parameters = $event->getSubjectParameters();
82
83
		switch ($subject) {
84
			case self::SUBJECT_OBJECT_ADD . '_todo':
85
			case self::SUBJECT_OBJECT_DELETE . '_todo':
86
			case self::SUBJECT_OBJECT_UPDATE . '_todo':
87
			case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
88
			case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
89
				return [
90
					'actor' => $this->generateUserParameter($parameters[0]),
91
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
92
					'todo' => $this->generateObjectParameter($parameters[2]),
93
				];
94
			case self::SUBJECT_OBJECT_ADD . '_todo_self':
95
			case self::SUBJECT_OBJECT_DELETE . '_todo_self':
96
			case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
97
			case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
98 View Code Duplication
			case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
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
				return [
100
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
101
					'todo' => $this->generateObjectParameter($parameters[2]),
102
				];
103
		}
104
105
		throw new \InvalidArgumentException();
106
	}
107
}
108