Completed
Push — master ( 76852c...676732 )
by Sander
03:09
created

Activity   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 276
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 276
rs 6.5957
c 0
b 0
f 0
wmc 56
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getNotificationTypes() 0 9 1
A filterNotificationTypes() 0 3 2
A getDefaultTypes() 0 17 3
D translate() 0 44 20
D getSpecialParameterList() 0 36 19
B getTypeIcon() 0 12 5
A getGroupParameter() 0 3 1
A getNavigation() 0 13 1
A isFilterValid() 0 3 1
A getQueryForFilter() 0 10 2

How to fix   Complexity   

Complex Class

Complex classes like Activity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Activity, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Nextcloud - passman
4
 *
5
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
6
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
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\Passman;
25
26
use OCP\IURLGenerator;
27
28
class Activity implements \OCP\Activity\IExtension {
29
	const FILTER_PASSMAN = 'passman';
30
	const APP_NAME = 'passman';
31
	const TYPE_ITEM_ACTION = 'passman_item_action';
32
	const TYPE_ITEM_EXPIRED = 'passman_item_expired';
33
	const TYPE_ITEM_SHARED = 'passman_item_shared';
34
	const TYPE_ITEM_RENAMED = 'passman_item_renamed';
35
36
	const SUBJECT_ITEM_CREATED = 'item_created';
37
	const SUBJECT_ITEM_CREATED_SELF = 'item_created_self';
38
	const SUBJECT_ITEM_EDITED = 'item_edited';
39
	const SUBJECT_ITEM_EDITED_SELF = 'item_edited_self';
40
	const SUBJECT_APPLY_REV = 'item_apply_revision';
41
	const SUBJECT_APPLY_REV_SELF = 'item_apply_revision_self';
42
	const SUBJECT_ITEM_DELETED = 'item_deleted';
43
	const SUBJECT_ITEM_DELETED_SELF = 'item_deleted_self';
44
	const SUBJECT_ITEM_RECOVERED = 'item_recovered';
45
	const SUBJECT_ITEM_RECOVERED_SELF = 'item_recovered_self';
46
	const SUBJECT_ITEM_DESTROYED = 'item_destroyed';
47
	const SUBJECT_ITEM_DESTROYED_SELF = 'item_destroyed_self';
48
	const SUBJECT_ITEM_EXPIRED = 'item_expired';
49
	const SUBJECT_ITEM_SHARED = 'item_shared';
50
	const SUBJECT_ITEM_SHARE_RECEIVED = 'item_share_received';
51
	const SUBJECT_ITEM_SHARED_PUBLICLY = 'item_shared_publicly';
52
	const SUBJECT_ITEM_RENAMED = 'item_renamed';
53
	const SUBJECT_ITEM_RENAMED_SELF = 'item_renamed_self';
54
55
56
	protected $URLGenerator;
57
58
	public function __construct( IURLGenerator $URLGenerator) {
59
		$this->URLGenerator = $URLGenerator;
60
	}
61
62
63
	/**
64
	 * The extension can return an array of additional notification types.
65
	 * If no additional types are to be added false is to be returned
66
	 *
67
	 * @param string $languageCode
68
	 * @return array|false
69
	 */
70
	public function getNotificationTypes($languageCode) {
71
		$l = \OC::$server->getL10N(self::APP_NAME, $languageCode);
72
		return array(
73
			self::TYPE_ITEM_ACTION => $l->t('A Passman item has been created, modified or deleted'),
74
			self::TYPE_ITEM_EXPIRED => $l->t('A Passman item has expired'),
75
			self::TYPE_ITEM_SHARED => $l->t('A Passman item has been shared'),
76
			self::TYPE_ITEM_RENAMED => $l->t('A Passman item has been renamed')
77
		);
78
	}
79
80
	/**
81
	 * The extension can filter the types based on the filter if required.
82
	 * In case no filter is to be applied false is to be returned unchanged.
83
	 *
84
	 * @param array $types
85
	 * @param string $filter
86
	 * @return array|false
87
	 */
88
	public function filterNotificationTypes($types, $filter) {
89
		return $filter === self::FILTER_PASSMAN ? [self::TYPE_ITEM_ACTION, self::TYPE_ITEM_EXPIRED, self::TYPE_ITEM_SHARED, self::TYPE_ITEM_RENAMED] : $types;
90
	}
91
92
	/**
93
	 * For a given method additional types to be displayed in the settings can be returned.
94
	 * In case no additional types are to be added false is to be returned.
95
	 *
96
	 * @param string $method
97
	 * @return array|false
98
	 */
99
	public function getDefaultTypes($method) {
100
		if ($method === 'stream') {
101
			return array(
102
				self::TYPE_ITEM_ACTION,
103
				self::TYPE_ITEM_EXPIRED,
104
				self::TYPE_ITEM_SHARED,
105
				self::TYPE_ITEM_EXPIRED,
106
				self::TYPE_ITEM_RENAMED,
107
			);
108
		}
109
		if ($method === 'email') {
110
			return array(
111
				self::TYPE_ITEM_EXPIRED,
112
			);
113
		}
114
		return false;
115
	}
116
117
	/**
118
	 * The extension can translate a given message to the requested languages.
119
	 * If no translation is available false is to be returned.
120
	 *
121
	 * @param string $app
122
	 * @param string $text
123
	 * @param array $params
124
	 * @param boolean $stripPath
125
	 * @param boolean $highlightParams
126
	 * @param string $languageCode
127
	 * @return string|false
128
	 */
129
	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
130
		$l = \OC::$server->getL10NFactory()->get(self::APP_NAME, $languageCode);
131
		if ($app === self::APP_NAME) {
132
			switch ($text) {
133
				case self::SUBJECT_ITEM_CREATED:
134
					return $l->t('%1$s has been created by %2$s', $params);
135
				case self::SUBJECT_ITEM_CREATED_SELF:
136
					return $l->t('You created %1$s', $params);
137
				case self::SUBJECT_ITEM_EDITED:
138
					return $l->t('%1$s has been updated by %2$s', $params);
139
				case self::SUBJECT_ITEM_EDITED_SELF:
140
					return $l->t('You updated %1$s', $params);
141
				case self::SUBJECT_APPLY_REV:
142
					return $l->t('%2$s has revised %1$s to the revision of %3$s', $params);
143
				case self::SUBJECT_APPLY_REV_SELF:
144
					return $l->t('You reverted %1$s back to the revision of %3$s', $params);
145
				case self::SUBJECT_ITEM_RENAMED:
146
					return $l->t('%3$s has renamed %1$s to %2$s', $params);
147
				case self::SUBJECT_ITEM_RENAMED_SELF:
148
					return $l->t('You renamed %1$s to %2$s', $params);
149
				case self::SUBJECT_ITEM_DELETED:
150
					return $l->t('%1$s has been deleted by %2$s', $params);
151
				case self::SUBJECT_ITEM_DELETED_SELF:
152
					return $l->t('You deleted %1$s', $params);
153
				case self::SUBJECT_ITEM_RECOVERED:
154
					return $l->t('%1$s has been recovered by %2$s', $params);
155
				case self::SUBJECT_ITEM_RECOVERED_SELF:
156
					return $l->t('You recovered %1$s', $params);
157
				case self::SUBJECT_ITEM_DESTROYED:
158
					return $l->t('%1$s has been permanently deleted by %2$s', $params);
159
				case self::SUBJECT_ITEM_DESTROYED_SELF:
160
					return $l->t('You permanently deleted %1$s', $params);
161
				case self::SUBJECT_ITEM_EXPIRED:
162
					return $l->t('The password of %1$s has expired, renew it now.', $params);
163
				case self::SUBJECT_ITEM_SHARED:
164
					return $l->t('%1$s has been shared with %2$s', $params);
165
				case self::SUBJECT_ITEM_SHARE_RECEIVED:
166
					return $l->t('You received a share request for %1$s from %2$s', $params);
167
				case self::SUBJECT_ITEM_SHARED_PUBLICLY:
168
					return $l->t('%s has been shared with a link', $params);
169
			}
170
		}
171
		return false;
172
	}
173
174
	/**
175
	 * The extension can define the type of parameters for translation
176
	 *
177
	 * Currently known types are:
178
	 * * file => will strip away the path of the file and add a tooltip with it
179
	 * * username => will add the avatar of the user
180
	 *
181
	 * @param string $app
182
	 * @param string $text
183
	 * @return array|false
184
	 */
185
	public function getSpecialParameterList($app, $text) {
186
		if ($app === self::APP_NAME) {
187
			switch ($text) {
188
				case self::SUBJECT_ITEM_CREATED:
189
				case self::SUBJECT_ITEM_CREATED_SELF:
190
				case self::SUBJECT_ITEM_EDITED:
191
				case self::SUBJECT_ITEM_EDITED_SELF:
192
				case self::SUBJECT_ITEM_DELETED:
193
				case self::SUBJECT_ITEM_DELETED_SELF:
194
				case self::SUBJECT_ITEM_RECOVERED:
195
				case self::SUBJECT_ITEM_RECOVERED_SELF:
196
				case self::SUBJECT_ITEM_DESTROYED:
197
				case self::SUBJECT_ITEM_DESTROYED_SELF:
198
					return array(
199
						0 => 'passman',
200
						1 => 'username',
201
					);
202
				case self::SUBJECT_APPLY_REV:
203
				case self::SUBJECT_APPLY_REV_SELF:
204
					return array(
205
						0 => 'passman',
206
						1 => 'username',
207
						2 => '', //unknown
208
					);
209
				case self::SUBJECT_ITEM_EXPIRED:
210
				case self::SUBJECT_ITEM_RENAMED_SELF:
211
				case self::SUBJECT_ITEM_RENAMED:
212
				case self::SUBJECT_ITEM_SHARED:
213
				case self::SUBJECT_ITEM_SHARED_PUBLICLY:
214
					return array(
215
						0 => 'passman',
216
					);
217
			}
218
		}
219
		return false;
220
	}
221
222
	/**
223
	 * A string naming the css class for the icon to be used can be returned.
224
	 * If no icon is known for the given type false is to be returned.
225
	 *
226
	 * @param string $type
227
	 * @return string|false
228
	 */
229
	public function getTypeIcon($type) {
230
		switch ($type) {
231
			case self::TYPE_ITEM_ACTION:
232
			case self::TYPE_ITEM_EXPIRED:
233
				return 'icon-password';
234
			case self::TYPE_ITEM_SHARED:
235
				return 'icon-share';
236
			case self::TYPE_ITEM_RENAMED:
237
				return 'icon-rename';
238
		}
239
		return false;
240
	}
241
242
	/**
243
	 * The extension can define the parameter grouping by returning the index as integer.
244
	 * In case no grouping is required false is to be returned.
245
	 *
246
	 * @param array $activity
247
	 * @return integer|false
248
	 */
249
	public function getGroupParameter($activity) {
250
		return false;
251
	}
252
253
	/**
254
	 * The extension can define additional navigation entries. The array returned has to contain two keys 'top'
255
	 * and 'apps' which hold arrays with the relevant entries.
256
	 * If no further entries are to be added false is no be returned.
257
	 *
258
	 * @return array|false
259
	 */
260
	public function getNavigation() {
261
		$l = \OC::$server->getL10N(self::APP_NAME);
262
		return array(
263
			'top' => array(),
264
			'apps' => array( self::FILTER_PASSMAN =>
265
				array(
266
					'id' => 'passman',
267
					'name' => (string) $l->t('Passwords'),
268
					'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_PASSMAN]),
269
				),
270
			),
271
		);
272
	}
273
274
	/**
275
	 * The extension can check if a customer filter (given by a query string like filter=abc) is valid or not.
276
	 *
277
	 * @param string $filterValue
278
	 * @return boolean
279
	 */
280
	public function isFilterValid($filterValue) {
281
		return $filterValue ===  self::FILTER_PASSMAN;
282
	}
283
284
	/**
285
	 * For a given filter the extension can specify the sql query conditions including parameters for that query.
286
	 * In case the extension does not know the filter false is to be returned.
287
	 * The query condition and the parameters are to be returned as array with two elements.
288
	 * E.g. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
289
	 *
290
	 * @param string $filter
291
	 * @return array|false
292
	 */
293
	public function getQueryForFilter($filter) {
294
		if ($filter === self::FILTER_PASSMAN) {
295
			return [
296
				'(`app` = ?)',
297
				[self::APP_NAME],
298
			];
299
		}
300
		return false;
301
302
	}
303
}