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

Calendar::parse()   C

Complexity

Conditions 23
Paths 70

Size

Total Lines 64
Code Lines 49

Duplication

Lines 24
Ratio 37.5 %

Importance

Changes 0
Metric Value
cc 23
eloc 49
nc 70
nop 2
dl 24
loc 64
rs 5.8823
c 0
b 0
f 0

How to fix   Long Method    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
use OCP\Activity\IEventMerger;
26
use OCP\Activity\IManager;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\IUserManager;
30
31
class Calendar extends Base {
32
33
	const SUBJECT_ADD = 'calendar_add';
34
	const SUBJECT_UPDATE = 'calendar_update';
35
	const SUBJECT_DELETE = 'calendar_delete';
36
	const SUBJECT_SHARE_USER = 'calendar_user_share';
37
	const SUBJECT_SHARE_GROUP = 'calendar_group_share';
38
	const SUBJECT_UNSHARE_USER = 'calendar_user_unshare';
39
	const SUBJECT_UNSHARE_GROUP = 'calendar_group_unshare';
40
41
	/** @var IL10N */
42
	protected $l;
43
44
	/** @var IURLGenerator */
45
	protected $url;
46
47
	/** @var IManager */
48
	protected $activityManager;
49
50
	/** @var IEventMerger */
51
	protected $eventMerger;
52
53
	/**
54
	 * @param IL10N $l
55
	 * @param IURLGenerator $url
56
	 * @param IManager $activityManager
57
	 * @param IUserManager $userManager
58
	 * @param IEventMerger $eventMerger
59
	 */
60 View Code Duplication
	public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
		parent::__construct($userManager);
62
		$this->l = $l;
63
		$this->url = $url;
64
		$this->activityManager = $activityManager;
65
		$this->eventMerger = $eventMerger;
66
	}
67
68
	/**
69
	 * @param IEvent $event
70
	 * @param IEvent|null $previousEvent
71
	 * @return IEvent
72
	 * @throws \InvalidArgumentException
73
	 * @since 11.0.0
74
	 */
75
	public function parse(IEvent $event, IEvent $previousEvent = null) {
76
		if ($event->getApp() !== 'dav' || $event->getType() !== 'calendar') {
77
			throw new \InvalidArgumentException();
78
		}
79
80
		$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'places/calendar-dark.svg')));
81
82
		if ($event->getSubject() === self::SUBJECT_ADD) {
83
			$subject = $this->l->t('{actor} created calendar {calendar}');
84
		} else if ($event->getSubject() === self::SUBJECT_ADD . '_self') {
85
			$subject = $this->l->t('You created calendar {calendar}');
86
		} else if ($event->getSubject() === self::SUBJECT_DELETE) {
87
			$subject = $this->l->t('{actor} deleted calendar {calendar}');
88
		} else if ($event->getSubject() === self::SUBJECT_DELETE . '_self') {
89
			$subject = $this->l->t('You deleted calendar {calendar}');
90
		} else if ($event->getSubject() === self::SUBJECT_UPDATE) {
91
			$subject = $this->l->t('{actor} updated calendar {calendar}');
92
		} else if ($event->getSubject() === self::SUBJECT_UPDATE . '_self') {
93
			$subject = $this->l->t('You updated calendar {calendar}');
94
95
		} else if ($event->getSubject() === self::SUBJECT_SHARE_USER) {
96
			$subject = $this->l->t('{actor} shared calendar {calendar} with you');
97 View Code Duplication
		} else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_you') {
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...
98
			$subject = $this->l->t('You shared calendar {calendar} with {user}');
99
		} else if ($event->getSubject() === self::SUBJECT_SHARE_USER . '_by') {
100
			$subject = $this->l->t('{actor} shared calendar {calendar} with {user}');
101
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER) {
102
			$subject = $this->l->t('{actor} unshared calendar {calendar} from you');
103
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_you') {
104
			$subject = $this->l->t('You unshared calendar {calendar} from {user}');
105
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_by') {
106
			$subject = $this->l->t('{actor} unshared calendar {calendar} from {user}');
107
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_USER . '_self') {
108
			$subject = $this->l->t('{actor} unshared calendar {calendar} from themselves');
109
110
		} else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_you') {
111
			$subject = $this->l->t('You shared calendar {calendar} with group {group}');
112
		} else if ($event->getSubject() === self::SUBJECT_SHARE_GROUP . '_by') {
113
			$subject = $this->l->t('{actor} shared calendar {calendar} with group {group}');
114
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_you') {
115
			$subject = $this->l->t('You unshared calendar {calendar} from group {group}');
116
		} else if ($event->getSubject() === self::SUBJECT_UNSHARE_GROUP . '_by') {
117
			$subject = $this->l->t('{actor} unshared calendar {calendar} from group {group}');
118
		} else {
119
			throw new \InvalidArgumentException();
120
		}
121
122
		$parsedParameters = $this->getParameters($event);
123
		$this->setSubjects($event, $subject, $parsedParameters);
124
125
		$event = $this->eventMerger->mergeEvents('calendar', $event, $previousEvent);
126
127
		if ($event->getChildEvent() === null) {
128
			if (isset($parsedParameters['user'])) {
129
				// Couldn't group by calendar, maybe we can group by users
130
				$event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
131
			} else if (isset($parsedParameters['group'])) {
132
				// Couldn't group by calendar, maybe we can group by groups
133
				$event = $this->eventMerger->mergeEvents('group', $event, $previousEvent);
134
			}
135
		}
136
137
		return $event;
138
	}
139
140
	/**
141
	 * @param IEvent $event
142
	 * @return array
143
	 */
144
	protected function getParameters(IEvent $event) {
145
		$subject = $event->getSubject();
146
		$parameters = $event->getSubjectParameters();
147
148
		switch ($subject) {
149
			case self::SUBJECT_ADD:
150
			case self::SUBJECT_ADD . '_self':
151
			case self::SUBJECT_DELETE:
152
			case self::SUBJECT_DELETE . '_self':
153
			case self::SUBJECT_UPDATE:
154
			case self::SUBJECT_UPDATE . '_self':
155
			case self::SUBJECT_SHARE_USER:
156
			case self::SUBJECT_UNSHARE_USER:
157
			case self::SUBJECT_UNSHARE_USER . '_self':
158
				return [
159
					'actor' => $this->generateUserParameter($parameters[0]),
160
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
161
				];
162
			case self::SUBJECT_SHARE_USER . '_you':
163 View Code Duplication
			case self::SUBJECT_UNSHARE_USER . '_you':
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
				return [
165
					'user' => $this->generateUserParameter($parameters[0]),
166
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
167
				];
168
			case self::SUBJECT_SHARE_USER . '_by':
169 View Code Duplication
			case self::SUBJECT_UNSHARE_USER . '_by':
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
				return [
171
					'user' => $this->generateUserParameter($parameters[0]),
172
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
173
					'actor' => $this->generateUserParameter($parameters[2]),
174
				];
175
			case self::SUBJECT_SHARE_GROUP . '_you':
176 View Code Duplication
			case self::SUBJECT_UNSHARE_GROUP . '_you':
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...
177
				return [
178
					'group' => $this->generateGroupParameter($parameters[0]),
179
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
180
				];
181
			case self::SUBJECT_SHARE_GROUP . '_by':
182 View Code Duplication
			case self::SUBJECT_UNSHARE_GROUP . '_by':
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...
183
				return [
184
					'group' => $this->generateGroupParameter($parameters[0]),
185
					'calendar' => $this->generateCalendarParameter($event->getObjectId(), $parameters[1]),
186
					'actor' => $this->generateUserParameter($parameters[2]),
187
				];
188
		}
189
190
		throw new \InvalidArgumentException();
191
	}
192
}
193