Passed
Push — master ( 419462...e84ca8 )
by René
03:55 queued 10s
created

UserGroupClass   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 41
eloc 110
c 5
b 0
f 1
dl 0
loc 249
rs 9.1199

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A getIsNoUser() 0 2 1
A getCategories() 0 2 1
A getDisplayName() 0 2 1
A setDisplayName() 0 3 1
A setType() 0 3 1
A getDescription() 0 2 1
A getLanguage() 0 2 1
A getIcon() 0 2 1
A getOrganisation() 0 2 1
A getType() 0 2 1
A getPublicId() 0 2 1
A setDescription() 0 3 1
A getUser() 0 2 1
A getId() 0 2 1
A setLanguage() 0 3 1
A setOrganisation() 0 3 1
A getEmailAddress() 0 2 2
A setEmailAddress() 0 3 1
A jsonSerialize() 0 15 1
B getUserGroupChild() 0 20 9
A getContainer() 0 4 1
A getMembers() 0 2 1
B search() 0 40 9

How to fix   Complexity   

Complex Class

Complex classes like UserGroupClass 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.

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 UserGroupClass, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <[email protected]>
4
 *
5
 * @author René Gieling <[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\Polls\Model;
25
26
use OCA\Polls\Exceptions\InvalidShareTypeException;
27
28
use OCP\Collaboration\Collaborators\ISearch;
29
use OCP\Share\IShare;
30
use OCA\Circles\AppInfo\Application;
0 ignored issues
show
Bug introduced by
The type OCA\Circles\AppInfo\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
class UserGroupClass implements \JsonSerializable {
33
	public const TYPE = 'generic';
34
	public const TYPE_PUBLIC = 'public';
35
	public const TYPE_EXTERNAL = 'external';
36
	public const TYPE_CIRCLE = Circle::TYPE;
37
	public const TYPE_CONTACT = Contact::TYPE;
38
	public const TYPE_CONTACTGROUP = ContactGroup::TYPE;
39
	public const TYPE_EMAIL = Email::TYPE;
40
	public const TYPE_GROUP = Group::TYPE;
41
	public const TYPE_USER = User::TYPE;
42
43
	private $l10n;
44
45
	/** @var string */
46
	protected $id;
47
48
	/** @var string */
49
	protected $type;
50
51
	/** @var string */
52
	protected $displayName = '';
53
54
	/** @var string */
55
	protected $description = '';
56
57
	/** @var string */
58
	protected $emailAddress = '';
59
60
	/** @var string */
61
	protected $language = '';
62
63
	/** @var string */
64
	protected $organisation = '';
65
66
	/** @var string */
67
	protected $icon = '';
68
69
	/** @var bool */
70
	protected $isNoUser = true;
71
72
	/** @var string[] */
73
	protected $categories = [];
74
75
	public function __construct(
76
		$id,
77
		$type,
78
		$displayName = '',
79
		$emailAddress = '',
80
		$language = ''
81
	) {
82
		$this->id = $id;
83
		$this->type = $type;
84
		$this->displayName = $displayName;
85
		$this->emailAddress = $emailAddress;
86
		$this->language = $language;
87
		$this->icon = 'icon-share';
88
		$this->l10n = \OC::$server->getL10N('polls');
89
	}
90
91
	public function getId(): string {
92
		return $this->id;
93
	}
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getPublicId(): string {
99
		return $this->id;
100
	}
101
102
	public function getUser(): string {
103
		return $this->id;
104
	}
105
106
	public function getType(): string {
107
		return $this->type;
108
	}
109
110
	public function getLanguage(): string {
111
		return $this->language;
112
	}
113
114
	public function getDisplayName(): string {
115
		return $this->displayName;
116
	}
117
118
	public function getDescription(): string {
119
		return $this->description;
120
	}
121
122
	public function getIcon(): string {
123
		return $this->icon;
124
	}
125
126
	public function getEmailAddress(): string {
127
		return $this->emailAddress ? $this->emailAddress : '';
128
	}
129
130
	public function getOrganisation(): string {
131
		return $this->organisation;
132
	}
133
134
	/**
135
	 * @return string[]
136
	 *
137
	 * @psalm-return array<array-key, string>
138
	 */
139
	public function getCategories(): array {
140
		return $this->categories;
141
	}
142
143
	public function getIsNoUser(): bool {
144
		return $this->isNoUser;
145
	}
146
147
	public function setType(string $type): string {
148
		$this->type = $type;
149
		return $this->type;
150
	}
151
152
	public function setDisplayName(string $displayName): string {
153
		$this->displayName = $displayName;
154
		return $this->displayName;
155
	}
156
157
	public function setDescription(string $description): string {
158
		$this->description = $description;
159
		return $this->description;
160
	}
161
162
	public function setEmailAddress(string $emailAddress) : string {
163
		$this->emailAddress = $emailAddress;
164
		return $this->emailAddress;
165
	}
166
167
	public function setLanguage(string $language): string {
168
		$this->language = $language;
169
		return $this->language;
170
	}
171
172
	public function setOrganisation($organisation): string {
173
		$this->organisation = $organisation;
174
		return $this->organisation;
175
	}
176
177
	/**
178
	 * serach all possible sharees - use ISearch to respect autocomplete restrictions
179
	 *
180
	 * Undocumented function long description
181
	 *
182
	 * @param type var Description
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Model\var was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
183
	 * @return return type
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Model\return was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
184
	 */
185
	public static function search(string $query = ''): array {
186
		$items = [];
187
		$types = [
188
			IShare::TYPE_USER,
189
			IShare::TYPE_GROUP
190
		];
191
		if (Circle::isEnabled() && class_exists('\OCA\Circles\ShareByCircleProvider')) {
192
			$types[] = IShare::TYPE_CIRCLE;
193
		}
194
195
		list($result, $more) = self::getContainer()->query(ISearch::class)->search($query, $types, false, 200, 0);
196
197
		foreach ($result['users'] as $item) {
198
			$items[] = new User($item['value']['shareWith']);
199
		}
200
201
		foreach ($result['exact']['users'] as $item) {
202
			$items[] = new User($item['value']['shareWith']);
203
		}
204
205
		foreach ($result['groups'] as $item) {
206
			$items[] = new Group($item['value']['shareWith']);
207
		}
208
209
		foreach ($result['exact']['groups'] as $item) {
210
			$items[] = new Group($item['value']['shareWith']);
211
		}
212
213
		$items = array_merge($items, Contact::search($query));
214
		$items = array_merge($items, ContactGroup::search($query));
215
216
		foreach ($result['circles'] as $item) {
217
			$items[] = new Circle($item['value']['shareWith']);
218
		}
219
220
		foreach ($result['exact']['circles'] as $item) {
221
			$items[] = new Circle($item['value']['shareWith']);
222
		}
223
224
		return $items;
225
	}
226
227
	/**
228
	 * @return array
229
	 */
230
	public function getMembers() {
231
		return [];
232
	}
233
234
	protected static function getContainer() {
235
		$app = \OC::$server->query(Application::class);
236
237
		return $app->getContainer();
238
	}
239
240
	/**
241
	 * @return Circle|Contact|ContactGroup|Email|GenericUser|Group|User
242
	 */
243
	public static function getUserGroupChild(string $type, string $id, string $displayName = '', string $emailAddress = '') {
244
		switch ($type) {
245
			case Group::TYPE:
246
				return new Group($id);
247
			case Circle::TYPE:
248
				return new Circle($id);
249
			case Contact::TYPE:
250
				return new Contact($id);
251
			case ContactGroup::TYPE:
252
				return new ContactGroup($id);
253
			case User::TYPE:
254
				return new User($id);
255
			case Email::TYPE:
256
				return new Email($id);
257
			case self::TYPE_PUBLIC:
258
				return new GenericUser($id, self::TYPE_PUBLIC);
259
			case self::TYPE_EXTERNAL:
260
				return new GenericUser($id, self::TYPE_EXTERNAL, $displayName, $emailAddress);
261
			default:
262
				throw new InvalidShareTypeException('Invalid share type (' . $type . ')');
263
			}
264
	}
265
266
	public function jsonSerialize(): array {
267
		return	[
268
			'id'        	=> $this->getId(),
269
			'user'          => $this->getId(),
270
			'userId'        => $this->getId(),
271
			'type'       	=> $this->getType(),
272
			'displayName'	=> $this->getDisplayName(),
273
			'organisation'	=> $this->getOrganisation(),
274
			'emailAddress'	=> $this->getEmailAddress(),
275
			'language'		=> $this->getLanguage(),
276
			'desc' 			=> $this->getDescription(),
277
			'subtitle'		=> $this->getDescription(),
278
			'icon'			=> $this->getIcon(),
279
			'categories'	=> $this->getCategories(),
280
			'isNoUser'		=> $this->getIsNoUser(),
281
		];
282
	}
283
}
284