Completed
Pull Request — master (#362)
by Maxence
02:25
created

BaseMember::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 IL10N */
57
	protected $l10n;
58
59
	/** @var string */
60
	private $userId = '';
61
62
	/** @var int */
63
	private $type = self::TYPE_USER;
64
65
	/** @var string */
66
	private $displayName;
67
68
	/** @var int */
69
	private $level;
70
71
	/** @var string */
72
	private $status;
73
74
	/** @var string */
75
	private $note;
76
77
	/** @var string */
78
	private $instance = '';
79
80
	/** @var string */
81
	private $joined = '';
82
83
	/** @var bool */
84
	protected $broadcasting = true;
85
86
	/**
87
	 * BaseMember constructor.
88
	 *
89
	 * @param string $circleUniqueId
90
	 * @param string $userId
91
	 * @param int $type
92
	 */
93
	public function __construct($userId = '', $type = 0, $circleUniqueId = '') {
94
		$this->l10n = \OC::$server->getL10N(Application::APP_NAME);
95
96
		$this->setType($type);
97
		$this->setUserId($userId);
98
		$this->setCircleId($circleUniqueId);
99
		$this->setLevel(Member::LEVEL_NONE);
100
		$this->setStatus(Member::STATUS_NONMEMBER);
101
	}
102
103
104
	/**
105
	 * @param string $circleUniqueId
106
	 *
107
	 * @return $this
108
	 */
109
	public function setCircleId($circleUniqueId) {
110
		$this->circleUniqueId = $circleUniqueId;
111
112
		return $this;
113
	}
114
115
	/**
116
	 * @return string
117
	 */
118
	public function getCircleId() {
119
		return $this->circleUniqueId;
120
	}
121
122
123
	/**
124
	 * @return int
125
	 */
126
	public function getType() {
127
		return $this->type;
128
	}
129
130
	public function setType($type) {
131
		$this->type = (int)$type;
132
	}
133
134
135
	public function getViewerType() {
136
		if ($this->getType() === 2) {
137
			return 'group';
138
		} else {
139
			return 'user';
140
		}
141
	}
142
143
144
	public function setUserId($userId) {
145
		$this->userId = $userId;
146
		$this->setDisplayName(MiscService::getDisplay($userId, $this->getType()));
147
148
		return $this;
149
	}
150
151
	public function getUserId() {
152
		return $this->userId;
153
	}
154
155
156
	public function setDisplayName($display) {
157
		$this->displayName = $display;
158
159
		return $this;
160
	}
161
162
	public function getDisplayName() {
163
		return $this->displayName;
164
	}
165
166
167
	public function setLevel($level) {
168
		$this->level = (int)$level;
169
170
		return $this;
171
	}
172
173
	public function getLevel() {
174
		return $this->level;
175
	}
176
177
178
	public function setNote($note) {
179
		$this->note = $note;
180
181
		return $this;
182
	}
183
184
	public function getNote() {
185
		return $this->note;
186
	}
187
188
189
	public function setInstance($instance) {
190
		$this->instance = $instance;
191
192
		return $this;
193
	}
194
195
	public function getInstance() {
196
		return $this->instance;
197
	}
198
199
200
	public function setStatus($status) {
201
		if (is_null($status)) {
202
			$this->status = self::STATUS_NONMEMBER;
203
		} else {
204
			$this->status = $status;
205
		}
206
207
		return $this;
208
	}
209
210
	public function getStatus() {
211
		return $this->status;
212
	}
213
214
215
	public function setJoined($joined) {
216
		$this->joined = $joined;
217
218
		return $this;
219
	}
220
221
	public function getJoined() {
222
		return $this->joined;
223
	}
224
225
226
	public function isLevel($level) {
227
		return ($this->getLevel() >= $level);
228
	}
229
230
231
	public function isAlmostMember() {
232
		return ($this->getStatus() === Member::STATUS_INVITED
233
				|| $this->getStatus() === Member::STATUS_REQUEST);
234
	}
235
236
237
	protected function setAsAMember($level = 1) {
238
		$this->setStatus(Member::STATUS_MEMBER);
239
		$this->setLevel($level);
240
	}
241
242
243
	/**
244
	 * @param $arr
245
	 *
246
	 * @return null|Member
247
	 */
248
	public static function fromArray($arr) {
249
		if ($arr === null) {
250
			return null;
251
		}
252
253
		$member = new Member();
254
		$member->setCircleId($arr['circle_id']);
255
		$member->setLevel($arr['level']);
256
257
		$member->setType(MiscService::get($arr, 'user_type'));
258
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
259
260
		$member->setInstance($arr['instance']);
261
		$member->setUserId($arr['user_id']);
262
		$member->setStatus($arr['status']);
263
		$member->setInstance($arr['instance']);
264
		$member->setNote($arr['note']);
265
		$member->setJoined($arr['joined']);
266
267
		return $member;
268
	}
269
270
271
	/**
272
	 * @param $json
273
	 *
274
	 * @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...
275
	 */
276
	public static function fromJSON($json) {
277
		return self::fromArray(json_decode($json, true));
278
	}
279
280
281
	public function jsonSerialize() {
282
		return [
283
			'circle_id'    => $this->getCircleId(),
284
			'user_id'      => $this->getUserId(),
285
			'user_type'    => $this->getType(),
286
			'display_name' => $this->getDisplayName(),
287
			'level'        => $this->getLevel(),
288
			'level_string' => $this->getLevelString(),
289
			'status'       => $this->getStatus(),
290
			'instance'     => $this->getInstance(),
291
			'note'         => $this->getNote(),
292
			'joined'       => $this->getJoined()
293
		];
294
	}
295
296
	public function getLevelString() {
297
		switch ($this->getLevel()) {
298
			case self::LEVEL_NONE:
299
				return 'Not a member';
300
			case self::LEVEL_MEMBER:
301
				return 'Member';
302
			case self::LEVEL_MODERATOR:
303
				return 'Moderator';
304
			case self::LEVEL_ADMIN:
305
				return 'Admin';
306
			case self::LEVEL_OWNER:
307
				return 'Owner';
308
		}
309
310
		return 'none';
311
	}
312
313
314
	public function getTypeString() {
315
		switch ($this->getType()) {
316
			case self::TYPE_USER:
317
				return 'Local Member';
318
			case self::TYPE_GROUP:
319
				return 'Group';
320
			case self::TYPE_MAIL:
321
				return 'Mail address';
322
			case self::TYPE_CONTACT:
323
				return 'Contact';
324
		}
325
326
		return 'none';
327
	}
328
}
329