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

Todo   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 82
Duplicated Lines 35.37 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 29
loc 82
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 24 39 13
C getParameters() 5 28 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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