Completed
Push — master ( 6ddda3...b3ff9a )
by Blizzz
79:33 queued 57:29
created

Todo   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 119
Duplicated Lines 34.45 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 41
loc 119
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 24 45 14
C getParameters() 17 58 22

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 string $language
30
	 * @param IEvent $event
31
	 * @param IEvent|null $previousEvent
32
	 * @return IEvent
33
	 * @throws \InvalidArgumentException
34
	 * @since 11.0.0
35
	 */
36
	public function parse($language, IEvent $event, IEvent $previousEvent = null) {
37
		if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar_todo') {
38
			throw new \InvalidArgumentException();
39
		}
40
41
		$this->l = $this->languageFactory->get('dav', $language);
42
43
		if ($this->activityManager->getRequirePNG()) {
44
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.png')));
45
		} else {
46
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/checkmark.svg')));
47
		}
48
49 View Code Duplication
		if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo') {
50
			$subject = $this->l->t('{actor} created todo {todo} in list {calendar}');
51
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_ADD . '_todo_self') {
52
			$subject = $this->l->t('You created todo {todo} in list {calendar}');
53
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo') {
54
			$subject = $this->l->t('{actor} deleted todo {todo} from list {calendar}');
55
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_DELETE . '_todo_self') {
56
			$subject = $this->l->t('You deleted todo {todo} from list {calendar}');
57
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo') {
58
			$subject = $this->l->t('{actor} updated todo {todo} in list {calendar}');
59
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_self') {
60
			$subject = $this->l->t('You updated todo {todo} in list {calendar}');
61
62
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed') {
63
			$subject = $this->l->t('{actor} solved todo {todo} in list {calendar}');
64
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self') {
65
			$subject = $this->l->t('You solved todo {todo} in list {calendar}');
66
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action') {
67
			$subject = $this->l->t('{actor} reopened todo {todo} in list {calendar}');
68
		} else if ($event->getSubject() === self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self') {
69
			$subject = $this->l->t('You reopened todo {todo} in list {calendar}');
70
		} else {
71
			throw new \InvalidArgumentException();
72
		}
73
74
		$parsedParameters = $this->getParameters($event);
75
		$this->setSubjects($event, $subject, $parsedParameters);
76
77
		$event = $this->eventMerger->mergeEvents('todo', $event, $previousEvent);
78
79
		return $event;
80
	}
81
82
	/**
83
	 * @param IEvent $event
84
	 * @return array
85
	 */
86
	protected function getParameters(IEvent $event) {
87
		$subject = $event->getSubject();
88
		$parameters = $event->getSubjectParameters();
89
90
		// Nextcloud 13+
91
		if (isset($parameters['calendar'])) {
92
			switch ($subject) {
93
				case self::SUBJECT_OBJECT_ADD . '_todo':
94
				case self::SUBJECT_OBJECT_DELETE . '_todo':
95
				case self::SUBJECT_OBJECT_UPDATE . '_todo':
96
				case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
97 View Code Duplication
				case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
98
					return [
99
						'actor' => $this->generateUserParameter($parameters['actor']),
100
						'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
101
						'todo' => $this->generateObjectParameter($parameters['object']),
102
					];
103
				case self::SUBJECT_OBJECT_ADD . '_todo_self':
104
				case self::SUBJECT_OBJECT_DELETE . '_todo_self':
105
				case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
106
				case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
107 View Code Duplication
				case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
108
					return [
109
						'calendar' => $this->generateCalendarParameter($parameters['calendar'], $this->l),
110
						'todo' => $this->generateObjectParameter($parameters['object']),
111
					];
112
			}
113
		}
114
115
		// Legacy - Do NOT Remove unless necessary
116
		// Removing this will break parsing of activities that were created on
117
		// Nextcloud 12, so we should keep this as long as it's acceptable.
118
		// Otherwise if people upgrade over multiple releases in a short period,
119
		// they will get the dead entries in their stream.
120
		switch ($subject) {
121
			case self::SUBJECT_OBJECT_ADD . '_todo':
122
			case self::SUBJECT_OBJECT_DELETE . '_todo':
123
			case self::SUBJECT_OBJECT_UPDATE . '_todo':
124
			case self::SUBJECT_OBJECT_UPDATE . '_todo_completed':
125 View Code Duplication
			case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action':
126
				return [
127
					'actor' => $this->generateUserParameter($parameters[0]),
128
					'calendar' => $this->generateLegacyCalendarParameter((int)$event->getObjectId(), $parameters[1]),
129
					'todo' => $this->generateObjectParameter($parameters[2]),
130
				];
131
			case self::SUBJECT_OBJECT_ADD . '_todo_self':
132
			case self::SUBJECT_OBJECT_DELETE . '_todo_self':
133
			case self::SUBJECT_OBJECT_UPDATE . '_todo_self':
134
			case self::SUBJECT_OBJECT_UPDATE . '_todo_completed_self':
135
			case self::SUBJECT_OBJECT_UPDATE . '_todo_needs_action_self':
136
				return [
137
					'calendar' => $this->generateLegacyCalendarParameter((int)$event->getObjectId(), $parameters[1]),
138
					'todo' => $this->generateObjectParameter($parameters[2]),
139
				];
140
		}
141
142
		throw new \InvalidArgumentException();
143
	}
144
}
145