Passed
Push — master ( b1a0d4...6df6fc )
by Roeland
10:53 queued 10s
created

Users::getParsedParameters()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 11
nop 1
dl 0
loc 30
rs 7.3166
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
 * @author Joas Schilling <[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\Files_Sharing\Activity\Providers;
25
26
use OCP\Activity\IEvent;
27
28
class Users extends Base {
29
30
31
	const SUBJECT_SHARED_USER_SELF = 'shared_user_self';
32
	const SUBJECT_RESHARED_USER_BY = 'reshared_user_by';
33
	const SUBJECT_UNSHARED_USER_SELF = 'unshared_user_self';
34
	const SUBJECT_UNSHARED_USER_BY = 'unshared_user_by';
35
36
	const SUBJECT_SHARED_WITH_BY = 'shared_with_by';
37
	const SUBJECT_UNSHARED_BY = 'unshared_by';
38
	const SUBJECT_SELF_UNSHARED = 'self_unshared';
39
	const SUBJECT_SELF_UNSHARED_BY = 'self_unshared_by';
40
41
	const SUBJECT_EXPIRED_USER = 'expired_user';
42
	const SUBJECT_EXPIRED = 'expired';
43
44
	/**
45
	 * @param IEvent $event
46
	 * @return IEvent
47
	 * @throws \InvalidArgumentException
48
	 * @since 11.0.0
49
	 */
50
	public function parseShortVersion(IEvent $event) {
51
		$parsedParameters = $this->getParsedParameters($event);
52
53
		if ($event->getSubject() === self::SUBJECT_SHARED_USER_SELF) {
54
			$subject = $this->l->t('Shared with {user}');
55
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_SELF) {
56
			$subject = $this->l->t('Removed share for {user}');
57
		} else if ($event->getSubject() === self::SUBJECT_SELF_UNSHARED) {
58
			$subject = $this->l->t('You removed yourself');
59
		} else if ($event->getSubject() === self::SUBJECT_SELF_UNSHARED_BY) {
60
			$subject = $this->l->t('{actor} removed themselves');
61
		} else if ($event->getSubject() === self::SUBJECT_RESHARED_USER_BY) {
62
			$subject = $this->l->t('{actor} shared with {user}');
63
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_BY) {
64
			$subject = $this->l->t('{actor} removed share for {user}');
65
		} else if ($event->getSubject() === self::SUBJECT_SHARED_WITH_BY) {
66
			$subject = $this->l->t('Shared by {actor}');
67
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_BY) {
68
			$subject = $this->l->t('{actor} removed share');
69
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED_USER) {
70
			$subject = $this->l->t('Share for {user} expired');
71
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED) {
72
			$subject = $this->l->t('Share expired');
73
		} else {
74
			throw new \InvalidArgumentException();
75
		}
76
77
		if ($this->activityManager->getRequirePNG()) {
78
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
79
		} else {
80
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
81
		}
82
		$this->setSubjects($event, $subject, $parsedParameters);
83
84
		return $event;
85
	}
86
87
	/**
88
	 * @param IEvent $event
89
	 * @return IEvent
90
	 * @throws \InvalidArgumentException
91
	 * @since 11.0.0
92
	 */
93
	public function parseLongVersion(IEvent $event) {
94
		$parsedParameters = $this->getParsedParameters($event);
95
96
		if ($event->getSubject() === self::SUBJECT_SHARED_USER_SELF) {
97
			$subject = $this->l->t('You shared {file} with {user}');
98
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_SELF) {
99
			$subject = $this->l->t('You removed {user} from {file}');
100
		} else if ($event->getSubject() === self::SUBJECT_SELF_UNSHARED) {
101
			$subject = $this->l->t('You removed yourself from {file}');
102
		} else if ($event->getSubject() === self::SUBJECT_SELF_UNSHARED_BY) {
103
			$subject = $this->l->t('{actor} removed themselves from {file}');
104
		} else if ($event->getSubject() === self::SUBJECT_RESHARED_USER_BY) {
105
			$subject = $this->l->t('{actor} shared {file} with {user}');
106
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_USER_BY) {
107
			$subject = $this->l->t('{actor} removed {user} from {file}');
108
		} else if ($event->getSubject() === self::SUBJECT_SHARED_WITH_BY) {
109
			$subject = $this->l->t('{actor} shared {file} with you');
110
		} else if ($event->getSubject() === self::SUBJECT_UNSHARED_BY) {
111
			$subject = $this->l->t('{actor} removed you from the share named {file}');
112
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED_USER) {
113
			$subject = $this->l->t('Share for file {file} with {user} expired');
114
		} else if ($event->getSubject() === self::SUBJECT_EXPIRED) {
115
			$subject = $this->l->t('Share for file {file} expired');
116
117
		} else {
118
			throw new \InvalidArgumentException();
119
		}
120
121
		if ($this->activityManager->getRequirePNG()) {
122
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
123
		} else {
124
			$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
125
		}
126
		$this->setSubjects($event, $subject, $parsedParameters);
127
128
		return $event;
129
	}
130
131
	protected function getParsedParameters(IEvent $event) {
132
		$subject = $event->getSubject();
133
		$parameters = $event->getSubjectParameters();
134
135
		switch ($subject) {
136
			case self::SUBJECT_SHARED_USER_SELF:
137
			case self::SUBJECT_UNSHARED_USER_SELF:
138
			case self::SUBJECT_EXPIRED_USER:
139
			case self::SUBJECT_EXPIRED:
140
				return [
141
					'file' => $this->getFile($parameters[0], $event),
142
					'user' => $this->getUser($parameters[1]),
143
				];
144
			case self::SUBJECT_SHARED_WITH_BY:
145
			case self::SUBJECT_UNSHARED_BY:
146
			case self::SUBJECT_SELF_UNSHARED:
147
			case self::SUBJECT_SELF_UNSHARED_BY:
148
				return [
149
					'file' => $this->getFile($parameters[0], $event),
150
					'actor' => $this->getUser($parameters[1]),
151
				];
152
			case self::SUBJECT_RESHARED_USER_BY:
153
			case self::SUBJECT_UNSHARED_USER_BY:
154
				return [
155
					'file' => $this->getFile($parameters[0], $event),
156
					'user' => $this->getUser($parameters[2]),
157
					'actor' => $this->getUser($parameters[1]),
158
				];
159
		}
160
		return [];
161
	}
162
}
163