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

User::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 10
dl 0
loc 11
rs 9.9332
c 2
b 0
f 2
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
25
namespace OCA\Polls\Model;
26
use OCP\IL10N;
27
28
class User implements \JsonSerializable {
29
30
	const TYPE_USER = 'user';
31
	const TYPE_GROUP = 'group';
32
	const TYPE_CONTACTGROUP = 'contactGroup';
33
	const TYPE_CONTACT = 'contact';
34
	const TYPE_EMAIL = 'email';
35
	const TYPE_CIRCLE = 'circle';
36
	const TYPE_EXTERNAL = 'external';
37
38
	/** @var IL10N */
39
	private $l10n;
40
41
	/** @var string */
42
	private $userId;
43
44
	/** @var string */
45
	private $type;
46
47
	/** @var string */
48
	private $displayName = '';
49
50
	/** @var string */
51
	private $desc = '';
52
53
	/** @var string */
54
	private $emailAddress = '';
55
56
	private $contact;
57
	private $circlesEnabled = false;
58
	private $contactsEnabled = false;
59
60
	/**
61
	 * User constructor.
62
	 * @param $type
63
	 * @param $userId
64
	 * @param $emailAddress
65
	 * @param $displayName
66
	 */
67
	public function __construct(
68
		$type,
69
		$userId,
70
		$emailAddress = '',
71
		$displayName = ''
72
	) {
73
		$this->l10n = \OC::$server->getL10N('polls');
74
		$this->type = $type;
75
		$this->userId = $userId;
76
		$this->emailAddress = $emailAddress;
77
		$this->displayName = $displayName;
78
		$this->loadContact();
79
		$this->circlesEnabled = \OC::$server->getAppManager()->isEnabledForUser('circles') &&
80
			(version_compare(\OC::$server->getAppManager()->getAppVersion('circles'), '0.17.1') >= 0);
81
		$this->contactsEnabled = \OC::$server->getContactsManager()->isEnabled();
82
	}
83
84
85
	public function setDisplayName($displayName) {
86
		$this->displayName = $displayName;
87
	}
88
89
	public function setEmailAddress($emailAddress) {
90
		$this->emailAddress = $emailAddress;
91
	}
92
93
	public function getUserId() {
94
		return $this->userId;
95
	}
96
97
	public function getType() {
98
		return $this->type;
99
	}
100
101
	public function getLanguage() {
102
		if ($this->type === self::TYPE_USER) {
103
			// Variant: $this->config->getUserValue($this->userId, 'core', 'lang')
104
			return \OC::$server->getConfig()->getUserValue($this->userId, 'core', 'lang');
105
		} else {
106
			return '';
107
		}
108
	}
109
110
	public function getDisplayName() {
111
		if ($this->type === self::TYPE_USER) {
112
			return \OC::$server->getUserManager()->get($this->userId)->getDisplayName();
113
114
		} elseif ($this->type === self::TYPE_GROUP) {
115
			try {
116
				// since NC19
117
				return \OC::$server->getGroupManager()->get($this->userId)->getDisplayName();
118
			} catch (\Exception $e) {
119
				// until NC18
120
				return $this->userId;
121
			}
122
123
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
124
			return $this->userId;
125
126
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
127
			return \OCA\Circles\Api\v1\Circles::detailsCircle($this->userId)->getName();
0 ignored issues
show
Bug introduced by
The type OCA\Circles\Api\v1\Circles 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...
128
129
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
130
			return isset($this->contact['FN']) ? $this->contact['FN'] : '';
131
132
		} elseif ($this->displayName) {
133
			return $this->displayName;
134
135
		} else {
136
			return $this->userId;
137
		}
138
	}
139
140
	public function getOrganisation() {
141
		if ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
142
			return isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
143
		} else {
144
			return '';
145
		}
146
	}
147
148
	public function getEmailAddress() {
149
		if ($this->type === self::TYPE_USER) {
150
			// Variant: \OC::$server->getConfig()->getUserValue($this->userId, 'settings', 'email'),
151
			return \OC::$server->getUserManager()->get($this->userId)->getEMailAddress();
152
153
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
154
			return isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : '';
155
156
		} elseif ($this->type === self::TYPE_EMAIL) {
157
			return $this->userId;
158
159
		} else {
160
			return $this->emailAddress;
161
		}
162
	}
163
164
	public function getDesc() {
165
		if ($this->type === self::TYPE_USER) {
166
			return $this->l10n->t('User');
167
168
		} elseif ($this->type === self::TYPE_GROUP) {
169
			return $this->l10n->t('Group');
170
171
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
172
			$this->desc = $this->l10n->t('Contact');
173
			if (isset($this->contact['ORG'])) {
174
				// Add organisation to description
175
				$this->desc = $this->contact['ORG'];
176
			}
177
178
			if (isset($this->contact['CATEGORIES'])) {
179
				// Add contact groups to description
180
				// Add aspace before each comma
181
				if ($this->desc === $this->l10n->t('Contact')) {
182
					$this->desc = $this->contact['CATEGORIES'];
183
				} else {
184
					$this->desc = $this->desc . ', ' . $this->contact['CATEGORIES'];
185
				}
186
			}
187
			return $this->desc;
188
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
189
			return $this->l10n->t('Contact group');
190
191
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
192
			return \OCA\Circles\Api\v1\Circles::detailsCircle($this->userId)->gettypeLongString();
193
194
		} elseif ($this->type === self::TYPE_EMAIL) {
195
			return $this->l10n->t('External email');
196
197
		} else {
198
			return '';
199
		}
200
	}
201
202
	public function getIcon() {
203
		if ($this->type === self::TYPE_USER) {
204
			return 'icon-user';
205
206
		} elseif ($this->type === self::TYPE_GROUP) {
207
			return 'icon-group';
208
209
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
210
			return 'icon-mail';
211
212
		} elseif ($this->type === self::TYPE_EMAIL) {
213
			return 'icon-mail';
214
215
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
216
			return 'icon-group';
217
218
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
219
			return 'icon-circle';
220
221
		} else {
222
			return '';
223
		}
224
	}
225
226
	private function loadContact() {
227
		if ($this->type === self::TYPE_CONTACT && \OC::$server->getContactsManager()->isEnabled()) {
228
			// TODO: remove FN in a later version than 1.5
229
			$contacts = \OC::$server->getContactsManager()->search($this->userId, ['UID', 'FN']);
230
			if (!$contacts) {
231
				$this->contact = [];
232
			} else {
233
				$this->contact = $contacts[0];
234
			}
235
		}
236
	}
237
238
	/**
239
	 * @return array
240
	 */
241
	public function jsonSerialize(): array {
242
		return	[
243
			'userId'        => $this->userId,
244
			'type'       	=> $this->type,
245
			'user'          => $this->userId,
246
			'displayName'	=> $this->getDisplayName(),
247
			'Organisation'	=> $this->getOrganisation(),
248
			'emailAddress'	=> $this->getEmailAddress(),
249
			'desc' 			=> $this->getDesc(),
250
			'icon'			=> $this->getIcon(),
251
			'contact'		=> $this->contact,
252
		];
253
	}
254
}
255