UserGroupClass::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
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\IL10N;
29
use OCP\Collaboration\Collaborators\ISearch;
30
use OCP\Share\IShare;
31
use OCA\Polls\AppInfo\Application;
32
33
class UserGroupClass implements \JsonSerializable {
34
	public const TYPE = 'generic';
35
	public const TYPE_PUBLIC = 'public';
36
	public const TYPE_EXTERNAL = 'external';
37
	public const TYPE_CIRCLE = Circle::TYPE;
38
	public const TYPE_CONTACT = Contact::TYPE;
39
	public const TYPE_CONTACTGROUP = ContactGroup::TYPE;
40
	public const TYPE_EMAIL = Email::TYPE;
41
	public const TYPE_GROUP = Group::TYPE;
42
	public const TYPE_USER = User::TYPE;
43
44
	/** @var IL10N */
45
	private $l10n;
46
47
	/** @var string */
48
	protected $id;
49
50
	/** @var string */
51
	protected $type;
52
53
	/** @var string */
54
	protected $displayName = '';
55
56
	/** @var string */
57
	protected $description = '';
58
59
	/** @var string */
60
	protected $emailAddress = '';
61
62
	/** @var string */
63
	protected $language = '';
64
65
	/** @var string */
66
	protected $organisation = '';
67
68
	/** @var string */
69
	protected $icon = '';
70
71
	/** @var bool */
72
	protected $isNoUser = true;
73
74
	/** @var string[] */
75
	protected $categories = [];
76
77
	public function __construct(
78
		string $id,
79
		string $type,
80
		string $displayName = '',
81
		string $emailAddress = '',
82
		string $language = ''
83
	) {
84
		$this->id = $id;
85
		$this->type = $type;
86
		$this->displayName = $displayName;
87
		$this->emailAddress = $emailAddress;
88
		$this->language = $language;
89
		$this->icon = 'icon-share';
90
		$this->l10n = \OC::$server->getL10N('polls');
91
	}
92
93
	public function getId(): string {
94
		return $this->id;
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100
	public function getPublicId(): string {
101
		return $this->id;
102
	}
103
104
	public function getUser(): string {
105
		return $this->id;
106
	}
107
108
	public function getType(): string {
109
		return $this->type;
110
	}
111
112
	public function getLanguage(): string {
113
		return $this->language;
114
	}
115
116
	public function getDisplayName(): string {
117
		return $this->displayName;
118
	}
119
120
	public function getDescription(): string {
121
		return $this->description;
122
	}
123
124
	public function getIcon(): string {
125
		return $this->icon;
126
	}
127
128
	public function getEmailAddress(): string {
129
		return $this->emailAddress;
130
	}
131
132
	public function getOrganisation(): string {
133
		return $this->organisation;
134
	}
135
136
	/**
137
	 * @return string[]
138
	 *
139
	 * @psalm-return array<array-key, string>
140
	 */
141
	public function getCategories(): array {
142
		return $this->categories;
143
	}
144
145
	public function getIsNoUser(): bool {
146
		return $this->isNoUser;
147
	}
148
149
	public function setType(string $type): string {
150
		$this->type = $type;
151
		return $this->type;
152
	}
153
154
	public function setDisplayName(string $displayName): string {
155
		$this->displayName = $displayName;
156
		return $this->displayName;
157
	}
158
159
	public function setDescription(string $description): string {
160
		$this->description = $description;
161
		return $this->description;
162
	}
163
164
	public function setEmailAddress(string $emailAddress) : string {
165
		$this->emailAddress = $emailAddress;
166
		return $this->emailAddress;
167
	}
168
169
	public function setLanguage(string $language): string {
170
		$this->language = $language;
171
		return $this->language;
172
	}
173
174
	public function setOrganisation(string $organisation): string {
175
		$this->organisation = $organisation;
176
		return $this->organisation;
177
	}
178
179
	/**
180
	 * search all possible sharees - use ISearch to respect autocomplete restrictions
181
	 */
182
	public static function search(string $query = ''): array {
183
		$items = [];
184
		$types = [
185
			IShare::TYPE_USER,
186
			IShare::TYPE_GROUP
187
		];
188
		if (Circle::isEnabled() && class_exists('\OCA\Circles\ShareByCircleProvider')) {
189
			$types[] = IShare::TYPE_CIRCLE;
190
		}
191
192
		[$result, $more] = self::getContainer()->query(ISearch::class)->search($query, $types, false, 200, 0);
193
194
		foreach ($result['users'] as $item) {
195
			$items[] = new User($item['value']['shareWith']);
196
		}
197
198
		foreach ($result['exact']['users'] as $item) {
199
			$items[] = new User($item['value']['shareWith']);
200
		}
201
202
		foreach ($result['groups'] as $item) {
203
			$items[] = new Group($item['value']['shareWith']);
204
		}
205
206
		foreach ($result['exact']['groups'] as $item) {
207
			$items[] = new Group($item['value']['shareWith']);
208
		}
209
210
		$items = array_merge($items, Contact::search($query));
211
		$items = array_merge($items, ContactGroup::search($query));
212
213
		if (Circle::isEnabled()) {
214
			foreach ($result['circles'] as $item) {
215
				$items[] = new Circle($item['value']['shareWith']);
216
			}
217
			foreach ($result['exact']['circles'] as $item) {
218
				$items[] = new Circle($item['value']['shareWith']);
219
			}
220
		}
221
222
223
		return $items;
224
	}
225
226
	/**
227
	 * @return array
228
	 */
229
	public function getMembers(): array {
230
		return [];
231
	}
232
233
	protected static function getContainer() {
234
		$app = \OC::$server->query(Application::class);
235
236
		return $app->getContainer();
237
	}
238
239
	/**
240
	 * @return Circle|Contact|ContactGroup|Email|GenericUser|Group|User
241
	 */
242
	public static function getUserGroupChild(string $type, string $id, string $displayName = '', string $emailAddress = '') {
243
		switch ($type) {
244
			case Group::TYPE:
245
				return new Group($id);
246
			case Circle::TYPE:
247
				return new Circle($id);
248
			case Contact::TYPE:
249
				return new Contact($id);
250
			case ContactGroup::TYPE:
251
				return new ContactGroup($id);
252
			case User::TYPE:
253
				return new User($id);
254
			case Email::TYPE:
255
				return new Email($id);
256
			case self::TYPE_PUBLIC:
257
				return new GenericUser($id, self::TYPE_PUBLIC);
258
			case self::TYPE_EXTERNAL:
259
				return new GenericUser($id, self::TYPE_EXTERNAL, $displayName, $emailAddress);
260
			default:
261
				throw new InvalidShareTypeException('Invalid share type (' . $type . ')');
262
			}
263
	}
264
265
	public function jsonSerialize(): array {
266
		return	[
267
			'id' => $this->getId(),
268
			'user' => $this->getId(),
269
			'userId' => $this->getId(),
270
			'type' => $this->getType(),
271
			'displayName' => $this->getDisplayName(),
272
			'organisation' => $this->getOrganisation(),
273
			'emailAddress' => $this->getEmailAddress(),
274
			'language' => $this->getLanguage(),
275
			'desc' => $this->getDescription(),
276
			'subtitle' => $this->getDescription(),
277
			'icon' => $this->getIcon(),
278
			'categories' => $this->getCategories(),
279
			'isNoUser' => $this->getIsNoUser(),
280
		];
281
	}
282
}
283