Completed
Pull Request — master (#347)
by Maxence
01:49
created

BaseMember::setDisplayName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Model;
28
29
use OCA\Circles\AppInfo\Application;
30
use OCA\Circles\Service\MiscService;
31
use OCP\IL10N;
32
33
class BaseMember implements \JsonSerializable {
34
35
	const LEVEL_NONE = 0;
36
	const LEVEL_MEMBER = 1;
37
	const LEVEL_MODERATOR = 4;
38
	const LEVEL_ADMIN = 8;
39
	const LEVEL_OWNER = 9;
40
41
	const STATUS_NONMEMBER = 'Unknown';
42
	const STATUS_INVITED = 'Invited';
43
	const STATUS_REQUEST = 'Requesting';
44
	const STATUS_MEMBER = 'Member';
45
	const STATUS_BLOCKED = 'Blocked';
46
	const STATUS_KICKED = 'Kicked';
47
48
	const TYPE_USER = 1;
49
	const TYPE_GROUP = 2;
50
	const TYPE_MAIL = 3;
51
	const TYPE_CONTACT = 4;
52
53
	/** @var string */
54
	private $circleUniqueId;
55
56
	/** @var string */
57
	private $circleContactGroupName;
58
59
	/** @var IL10N */
60
	protected $l10n;
61
62
	/** @var string */
63
	private $userId = '';
64
65
	/** @var int */
66
	private $type = self::TYPE_USER;
67
68
	/** @var string */
69
	private $displayName;
70
71
	/** @var int */
72
	private $level;
73
74
	/** @var string */
75
	private $status;
76
77
	/** @var string */
78
	private $contactId = '';
79
80
	/** @var string */
81
	private $note;
82
83
	/** @var string */
84
	private $joined;
85
86
	/** @var bool */
87
	protected $broadcasting = true;
88
89
	/**
90
	 * BaseMember constructor.
91
	 *
92
	 * @param string $circleUniqueId
93
	 * @param string $userId
94
	 * @param int $type
95
	 */
96
	public function __construct($userId = '', $type = 0, $circleUniqueId = '') {
97
		$this->l10n = \OC::$server->getL10N(Application::APP_NAME);
98
99
		$this->setType($type);
100
		$this->setUserId($userId);
101
		$this->setCircleId($circleUniqueId);
102
		$this->setLevel(Member::LEVEL_NONE);
103
		$this->setStatus(Member::STATUS_NONMEMBER);
104
	}
105
106
107
	/**
108
	 * @param string $circleUniqueId
109
	 *
110
	 * @return $this
111
	 */
112
	public function setCircleId($circleUniqueId) {
113
		$this->circleUniqueId = $circleUniqueId;
114
115
		return $this;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function getCircleId() {
122
		return $this->circleUniqueId;
123
	}
124
125
126
	/**
127
	 * @param string $circleContactGroupName
128
	 *
129
	 * @return $this
130
	 */
131
	public function setCircleContactGroupName($circleContactGroupName): self {
132
		$this->circleContactGroupName = $circleContactGroupName;
133
134
		return $this;
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	public function getCircleContactGroupName(): string {
141
		return $this->circleUniqueId;
142
	}
143
144
145
	/**
146
	 * @return int
147
	 */
148
	public function getType() {
149
		return $this->type;
150
	}
151
152
	public function setType($type) {
153
		$this->type = (int)$type;
154
	}
155
156
157
	public function getViewerType() {
158
		if ($this->getType() === 2) {
159
			return 'group';
160
		} else {
161
			return 'user';
162
		}
163
	}
164
165
166
	public function setUserId($userId) {
167
		$this->userId = $userId;
168
		$this->setDisplayName(MiscService::getDisplay($userId, $this->getType()));
169
170
		return $this;
171
	}
172
173
	public function getUserId() {
174
		return $this->userId;
175
	}
176
177
178
	public function setDisplayName($display) {
179
		$this->displayName = $display;
180
181
		return $this;
182
	}
183
184
	public function getDisplayName() {
185
		return $this->displayName;
186
	}
187
188
189
	public function setLevel($level) {
190
		$this->level = (int)$level;
191
192
		return $this;
193
	}
194
195
	public function getLevel() {
196
		return $this->level;
197
	}
198
199
200
	public function setNote($note) {
201
		$this->note = $note;
202
203
		return $this;
204
	}
205
206
	public function getNote() {
207
		return $this->note;
208
	}
209
210
211
	public function setContactId($contactId) {
212
		$this->contactId = $contactId;
213
214
		return $this;
215
	}
216
217
	public function getContactId() {
218
		return $this->contactId;
219
	}
220
221
222
	public function setStatus($status) {
223
		if (is_null($status)) {
224
			$this->status = self::STATUS_NONMEMBER;
225
		} else {
226
			$this->status = $status;
227
		}
228
229
		return $this;
230
	}
231
232
	public function getStatus() {
233
		return $this->status;
234
	}
235
236
237
	public function setJoined($joined) {
238
		$this->joined = $joined;
239
240
		return $this;
241
	}
242
243
	public function getJoined() {
244
		return $this->joined;
245
	}
246
247
248
	public function isLevel($level) {
249
		return ($this->getLevel() >= $level);
250
	}
251
252
253
	public function isAlmostMember() {
254
		return ($this->getStatus() === Member::STATUS_INVITED
255
				|| $this->getStatus() === Member::STATUS_REQUEST);
256
	}
257
258
259
	protected function setAsAMember($level = 1) {
260
		$this->setStatus(Member::STATUS_MEMBER);
261
		$this->setLevel($level);
262
	}
263
264
265
	/**
266
	 * @param $arr
267
	 *
268
	 * @return null|Member
269
	 */
270
	public static function fromArray($arr) {
271
		if ($arr === null) {
272
			return null;
273
		}
274
275
		$member = new Member();
276
		$member->setCircleId($arr['circle_id']);
277
		$member->setLevel($arr['level']);
278
279
		$member->setType(MiscService::get($arr, 'user_type'));
280
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
281
282
		$member->setUserId($arr['user_id']);
283
		$member->setStatus($arr['status']);
284
		$member->setNote($arr['note']);
285
		$member->setJoined($arr['joined']);
286
287
		return $member;
288
	}
289
290
291
	/**
292
	 * @param $json
293
	 *
294
	 * @return Member
0 ignored issues
show
Documentation introduced by
Should the return type not be Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
295
	 */
296
	public static function fromJSON($json) {
297
		return self::fromArray(json_decode($json, true));
298
	}
299
300
301
	public function jsonSerialize() {
302
		return [
303
			'circle_id'    => $this->getCircleId(),
304
			'user_id'      => $this->getUserId(),
305
			'user_type'    => $this->getType(),
306
			'display_name' => $this->getDisplayName(),
307
			'contact_id'   => $this->getContactId(),
308
			'level'        => $this->getLevel(),
309
			'level_string' => $this->getLevelString(),
310
			'status'       => $this->getStatus(),
311
			'note'         => $this->getNote(),
312
			'joined'       => $this->getJoined()
313
		];
314
	}
315
316
	public function getLevelString() {
317
		switch ($this->getLevel()) {
318
			case self::LEVEL_NONE:
319
				return 'Not a member';
320
			case self::LEVEL_MEMBER:
321
				return 'Member';
322
			case self::LEVEL_MODERATOR:
323
				return 'Moderator';
324
			case self::LEVEL_ADMIN:
325
				return 'Admin';
326
			case self::LEVEL_OWNER:
327
				return 'Owner';
328
		}
329
330
		return 'none';
331
	}
332
333
334
	public function getTypeString() {
335
		switch ($this->getType()) {
336
			case self::TYPE_USER:
337
				return 'Local Member';
338
			case self::TYPE_GROUP:
339
				return 'Group';
340
			case self::TYPE_MAIL:
341
				return 'Mail address';
342
			case self::TYPE_CONTACT:
343
				return 'Contact';
344
		}
345
346
		return 'none';
347
	}
348
}
349