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

User   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 58
eloc 115
c 2
b 0
f 2
dl 0
loc 203
rs 4.5599

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 2 1
A setDisplayName() 0 2 1
A __construct() 0 15 2
A getLanguage() 0 6 2
A setEmailAddress() 0 2 1
A getUserId() 0 2 1
C getDesc() 0 30 13
C getDisplayName() 0 21 12
B getIcon() 0 15 10
A getOrganisation() 0 5 4
A getEmailAddress() 0 10 6
A jsonSerialize() 0 11 1
A loadContact() 0 8 4

How to fix   Complexity   

Complex Class

Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.

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
29
class User implements \JsonSerializable {
30
	public const TYPE_USER = 'user';
31
	public const TYPE_GROUP = 'group';
32
	public const TYPE_CONTACTGROUP = 'contactGroup';
33
	public const TYPE_CONTACT = 'contact';
34
	public const TYPE_EMAIL = 'email';
35
	public const TYPE_CIRCLE = 'circle';
36
	public 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
		} elseif ($this->type === self::TYPE_GROUP) {
114
			try {
115
				// since NC19
116
				return \OC::$server->getGroupManager()->get($this->userId)->getDisplayName();
117
			} catch (\Exception $e) {
118
				// until NC18
119
				return $this->userId;
120
			}
121
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
122
			return $this->userId;
123
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
124
			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...
125
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
126
			return isset($this->contact['FN']) ? $this->contact['FN'] : '';
127
		} elseif ($this->displayName) {
128
			return $this->displayName;
129
		} else {
130
			return $this->userId;
131
		}
132
	}
133
134
	public function getOrganisation() {
135
		if ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
136
			return isset($this->contact['ORG']) ? $this->contact['ORG'] : '';
137
		} else {
138
			return '';
139
		}
140
	}
141
142
	public function getEmailAddress() {
143
		if ($this->type === self::TYPE_USER) {
144
			// Variant: \OC::$server->getConfig()->getUserValue($this->userId, 'settings', 'email'),
145
			return \OC::$server->getUserManager()->get($this->userId)->getEMailAddress();
146
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
147
			return isset($this->contact['EMAIL'][0]) ? $this->contact['EMAIL'][0] : '';
148
		} elseif ($this->type === self::TYPE_EMAIL) {
149
			return $this->userId;
150
		} else {
151
			return $this->emailAddress;
152
		}
153
	}
154
155
	public function getDesc() {
156
		if ($this->type === self::TYPE_USER) {
157
			return $this->l10n->t('User');
158
		} elseif ($this->type === self::TYPE_GROUP) {
159
			return $this->l10n->t('Group');
160
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
161
			$this->desc = $this->l10n->t('Contact');
162
			if (isset($this->contact['ORG'])) {
163
				// Add organisation to description
164
				$this->desc = $this->contact['ORG'];
165
			}
166
167
			if (isset($this->contact['CATEGORIES'])) {
168
				// Add contact groups to description
169
				// Add aspace before each comma
170
				if ($this->desc === $this->l10n->t('Contact')) {
171
					$this->desc = $this->contact['CATEGORIES'];
172
				} else {
173
					$this->desc = $this->desc . ', ' . $this->contact['CATEGORIES'];
174
				}
175
			}
176
			return $this->desc;
177
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
178
			return $this->l10n->t('Contact group');
179
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
180
			return \OCA\Circles\Api\v1\Circles::detailsCircle($this->userId)->gettypeLongString();
181
		} elseif ($this->type === self::TYPE_EMAIL) {
182
			return $this->l10n->t('External email');
183
		} else {
184
			return '';
185
		}
186
	}
187
188
	public function getIcon() {
189
		if ($this->type === self::TYPE_USER) {
190
			return 'icon-user';
191
		} elseif ($this->type === self::TYPE_GROUP) {
192
			return 'icon-group';
193
		} elseif ($this->type === self::TYPE_CONTACT && $this->contactsEnabled) {
194
			return 'icon-mail';
195
		} elseif ($this->type === self::TYPE_EMAIL) {
196
			return 'icon-mail';
197
		} elseif ($this->type === self::TYPE_CONTACTGROUP && $this->contactsEnabled) {
198
			return 'icon-group';
199
		} elseif ($this->type === self::TYPE_CIRCLE && $this->circlesEnabled) {
200
			return 'icon-circle';
201
		} else {
202
			return '';
203
		}
204
	}
205
206
	private function loadContact() {
207
		if ($this->type === self::TYPE_CONTACT && \OC::$server->getContactsManager()->isEnabled()) {
208
			// TODO: remove FN in a later version than 1.5
209
			$contacts = \OC::$server->getContactsManager()->search($this->userId, ['UID', 'FN']);
210
			if (!$contacts) {
211
				$this->contact = [];
212
			} else {
213
				$this->contact = $contacts[0];
214
			}
215
		}
216
	}
217
218
	/**
219
	 * @return array
220
	 */
221
	public function jsonSerialize(): array {
222
		return	[
223
			'userId'        => $this->userId,
224
			'type'       	=> $this->type,
225
			'user'          => $this->userId,
226
			'displayName'	=> $this->getDisplayName(),
227
			'Organisation'	=> $this->getOrganisation(),
228
			'emailAddress'	=> $this->getEmailAddress(),
229
			'desc' 			=> $this->getDesc(),
230
			'icon'			=> $this->getIcon(),
231
			'contact'		=> $this->contact,
232
		];
233
	}
234
}
235