Passed
Pull Request — master (#1128)
by René
06:43
created

Group   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 39
c 1
b 0
f 0
dl 0
loc 169
rs 10

15 Methods

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