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

User   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 18
eloc 43
dl 0
loc 178
rs 10
c 2
b 0
f 2

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 2 1
A getUserIsDisabled() 0 2 1
A getDesc() 0 2 1
A listRaw() 0 2 1
A getDisplayName() 0 2 1
A getIcon() 0 2 1
A getUser() 0 2 1
A __construct() 0 5 1
A getId() 0 2 1
A getLanguage() 0 2 1
A getOrganisation() 0 2 1
A jsonSerialize() 0 11 1
A getEmailAddress() 0 2 1
A load() 0 2 1
A getUserId() 0 2 1
A search() 0 8 3
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 User implements \JsonSerializable, IUserObj {
31
	public const TYPE = 'user';
32
	public const TYPE_USER = 'user';
33
	public const TYPE_GROUP = 'group';
34
	public const TYPE_CONTACTGROUP = 'contactGroup';
35
	public const TYPE_CONTACT = 'contact';
36
	public const TYPE_EMAIL = 'email';
37
	public const TYPE_CIRCLE = 'circle';
38
	public const TYPE_EXTERNAL = 'external';
39
	public const TYPE_INVALID = 'invalid';
40
41
	/** @var IL10N */
42
	private $l10n;
0 ignored issues
show
introduced by
The private property $l10n is not used, and could be removed.
Loading history...
43
44
	/** @var string */
45
	private $id;
46
47
	/** @var IUser */
0 ignored issues
show
Bug introduced by
The type OCA\Polls\Model\IUser 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...
48
	private $user;
49
50
	/**
51
	 * User constructor.
52
	 * @param $type
53
	 * @param $id
54
	 * @param $emailAddress
55
	 * @param $displayName
56
	 */
57
	public function __construct(
58
		$id
59
	) {
60
		$this->id = $id;
61
		$this->load();
62
	}
63
64
	/**
65
	 * Get userId
66
	 * @NoAdminRequired
67
	 * @return String
68
	 */
69
	public function getUserId() {
70
		return $this->id;
71
	}
72
73
	/**
74
	 * Get userId
75
	 * @NoAdminRequired
76
	 * @return String
77
	 */
78
	public function getId() {
79
		return $this->id;
80
	}
81
82
	/**
83
	 * getUser
84
	 * @NoAdminRequired
85
	 * @return String
86
	 */
87
	public function getUser() {
88
		return $this->id;
89
	}
90
91
	/**
92
	 * Get user type
93
	 * @NoAdminRequired
94
	 * @return String
95
	 */
96
	public function getType() {
97
		return self::TYPE;
98
	}
99
100
	/**
101
	 * Get language of user, if type = TYPE_USER
102
	 * @NoAdminRequired
103
	 * @return String
104
	 */
105
	public function getLanguage() {
106
		return \OC::$server->getConfig()->getUserValue($this->id, 'core', 'lang');
107
	}
108
109
	/**
110
	 * Get displayName
111
	 * @NoAdminRequired
112
	 * @return String
113
	 */
114
	public function getDisplayName() {
115
		return \OC::$server->getUserManager()->get($this->id)->getDisplayName();
116
	}
117
118
	/**
119
	 * Get organisation, if type = TYPE_CONTACT
120
	 * @NoAdminRequired
121
	 * @return String
122
	 */
123
	public function getOrganisation() {
124
		return '';
125
	}
126
127
	/**
128
	 * Get email address
129
	 * @NoAdminRequired
130
	 * @return String
131
	 */
132
	public function getEmailAddress() {
133
		return $this->user->getEMailAddress();
134
	}
135
136
	/**
137
	 * Get additional description, if available
138
	 * @NoAdminRequired
139
	 * @return String
140
	 */
141
	public function getDesc() {
142
		return \OC::$server->getL10N('polls')->t('User');
143
	}
144
145
	/**
146
	 * Get icon class
147
	 * @NoAdminRequired
148
	 * @return String
149
	 */
150
	public function getIcon() {
151
		return 'icon-user';
152
	}
153
154
	/**
155
	 * Get icon class
156
	 * @NoAdminRequired
157
	 * @return String
158
	 */
159
	public function getUserIsDisabled() {
160
		return !\OC::$server->getUserManager()->get($user)->isEnabled();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to be never defined.
Loading history...
161
	}
162
163
	/**
164
	 * listRaw
165
	 * @NoAdminRequired
166
	 * @param string $query
167
	 * @return Array
168
	 */
169
	public static function listRaw($query = '') {
170
		return \OC::$server->getUserManager()->search($query);
171
	}
172
173
	/**
174
	 * search
175
	 * @NoAdminRequired
176
	 * @param string $query
177
	 * @param array $skip - group names to skip in return array
178
	 * @return Group[]
179
	 */
180
	public static function search($query = '', $skip = []) {
181
		$users = [];
182
		foreach (self::listRaw($query) as $user) {
183
			if (!in_array($user->getUID(), $skip)) {
184
				$users[] = new Self($user->getUID());
185
			}
186
		}
187
		return $users;
188
	}
189
190
	private function load() {
191
		$this->user = \OC::$server->getUserManager()->get($this->id);
192
	}
193
194
	/**
195
	 * @return array
196
	 */
197
	public function jsonSerialize(): array {
198
		return	[
199
			'user'          => $this->id,
200
			'id'        	=> $this->id,
201
			'userId'        => $this->id,
202
			'type'       	=> $this->getType(),
203
			'displayName'	=> $this->getDisplayName(),
204
			'organisation'	=> $this->getOrganisation(),
205
			'emailAddress'	=> $this->getEmailAddress(),
206
			'desc' 			=> $this->getDesc(),
207
			'icon'			=> $this->getIcon(),
208
		];
209
	}
210
}
211