Passed
Pull Request — master (#1307)
by René
03:59
created

UserGroupClass::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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