Passed
Pull Request — master (#1128)
by René
04:45
created

Group::getMembers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 1
b 0
f 0
cc 2
nc 2
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
25
namespace OCA\Polls\Model;
26
27
use OCP\IL10N;
28
use OCA\Polls\Interfaces\IUserObj;
29
30
class Group implements \JsonSerializable, IUserObj {
31
	public const TYPE = 'group';
32
33
	/** @var IL10N */
34
	private $l10n;
0 ignored issues
show
introduced by
The private property $l10n is not used, and could be removed.
Loading history...
35
36
	/** @var string */
37
	private $id;
38
39
	/** @var IGroup */
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Model\IGroup 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...
40
	private $group;
41
42
	/**
43
	 * Group constructor.
44
	 * @param $id
45
	 * @param $displayName
46
	 */
47
	public function __construct(
48
		$id
49
	) {
50
		$this->id = $id;
51
		$this->load();
52
	}
53
54
	/**
55
	 * getId
56
	 * @NoAdminRequired
57
	 * @return String
58
	 */
59
	public function getId() {
60
		return $this->id;
61
	}
62
63
	/**
64
	 * getUser
65
	 * @NoAdminRequired
66
	 * @return String
67
	 */
68
	public function getUser() {
69
		return $this->id;
70
	}
71
72
	/**
73
	 * getType
74
	 * @NoAdminRequired
75
	 * @return String
76
	 */
77
	public function getType() {
78
		return self::TYPE;
79
	}
80
81
	/**
82
	 * getlanguage
83
	 * @NoAdminRequired
84
	 * @return String
85
	 */
86
	public function getLanguage() {
87
		return '';
88
	}
89
90
	/**
91
	 * getDisplayName
92
	 * @NoAdminRequired
93
	 * @return String
94
	 */
95
	public function getDisplayName() {
96
		try {
97
			// since NC19
98
			return $this->group->getDisplayName();
99
		} catch (\Exception $e) {
100
			// until NC18
101
			return $this->id;
102
		}
103
	}
104
105
	/**
106
	 * getOrganisation
107
	 * @NoAdminRequired
108
	 * @return String
109
	 */
110
	public function getOrganisation() {
111
		return '';
112
	}
113
114
	/**
115
	 * getEmailAddress
116
	 * @NoAdminRequired
117
	 * @return String
118
	 */
119
	public function getEmailAddress() {
120
		return '';
121
	}
122
123
	/**
124
	 * getDesc
125
	 * @NoAdminRequired
126
	 * @return String
127
	 */
128
	public function getDesc() {
129
		return \OC::$server->getL10N('polls')->t('Group');
130
	}
131
132
	/**
133
	 * getIcon
134
	 * @NoAdminRequired
135
	 * @return String
136
	 */
137
	public function getIcon() {
138
		return 'icon-group';
139
	}
140
141
	private function load() {
142
		$this->group = \OC::$server->getGroupManager()->get($this->id);
143
	}
144
145
	/**
146
	 * listRaw
147
	 * @NoAdminRequired
148
	 * @param string $query
149
	 * @return Array
150
	 */
151
	public static function listRaw($query = '') {
152
		return \OC::$server->getGroupManager()->search($query);
153
	}
154
155
	/**
156
	 * search
157
	 * @NoAdminRequired
158
	 * @param string $query
159
	 * @param array $skip - group names to skip in return array
160
	 * @return Group[]
161
	 */
162
	public static function search($query = '', $skip = []) {
163
		$groups = [];
164
		foreach (self::listRaw($query) as $group) {
165
			if (!in_array($group->getGID(), $skip)) {
166
				$groups[] = new Self($group->getGID());
167
			}
168
		}
169
		return $groups;
170
	}
171
172
	/**
173
	 * Get a list of circle members
174
	 * @NoAdminRequired
175
	 * @param string $query
176
	 * @return User[]
177
	 */
178
	public function getMembers() {
179
		$members = [];
180
		foreach (array_keys(\OC::$server->getGroupManager()->displayNamesInGroup($this->id)) as $member) {
181
			$members[] = new user($member);
182
		}
183
		return $members;
184
	}
185
186
187
188
	/**
189
	 * @return array
190
	 */
191
	public function jsonSerialize(): array {
192
		return	[
193
			'id'        	=> $this->id,
194
			'user'          => $this->id,
195
			'type'       	=> $this->getType(),
196
			'displayName'	=> $this->getDisplayName(),
197
			'organisation'	=> $this->getOrganisation(),
198
			'emailAddress'	=> $this->getEmailAddress(),
199
			'desc' 			=> $this->getDesc(),
200
			'icon'			=> $this->getIcon(),
201
			'isNoUser'		=> true,
202
		];
203
	}
204
}
205