Completed
Push — master ( 4e9d9d...cb83ec )
by Maxence
07:33
created

BaseMember::getLevelString()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 6
eloc 13
nc 6
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 OC\L10N\L10N;
30
use OCA\Circles\Service\MiscService;
31
32
class BaseMember implements \JsonSerializable {
33
34
	const LEVEL_NONE = 0;
35
	const LEVEL_MEMBER = 1;
36
	const LEVEL_MODERATOR = 4;
37
	const LEVEL_ADMIN = 8;
38
	const LEVEL_OWNER = 9;
39
40
	const STATUS_NONMEMBER = 'Unknown';
41
	const STATUS_INVITED = 'Invited';
42
	const STATUS_REQUEST = 'Requesting';
43
	const STATUS_MEMBER = 'Member';
44
	const STATUS_BLOCKED = 'Blocked';
45
	const STATUS_KICKED = 'Kicked';
46
47
	const TYPE_USER = 1;
48
	const TYPE_GROUP = 2;
49
	const TYPE_MAIL = 3;
50
51
	/** @var string */
52
	private $circleUniqueId;
53
54
	/** @var L10N */
55
	protected $l10n;
56
57
	/** @var string */
58
	private $userId = '';
59
60
	/** @var int */
61
	private $type = self::TYPE_USER;
62
63
	/** @var string */
64
	private $displayName;
65
66
	/** @var int */
67
	private $level;
68
69
	/** @var string */
70
	private $status;
71
72
	/** @var string */
73
	private $note;
74
75
	/** @var string */
76
	private $joined;
77
78
	/**
79
	 * BaseMember constructor.
80
	 *
81
	 * @param string $circleUniqueId
82
	 * @param string $userId
83
	 * @param int $type
84
	 */
85
	public function __construct($userId = '', $type = 0, $circleUniqueId = '') {
86
		$this->l10n = \OC::$server->getL10N('circles');
87
88
		$this->setType($type);
89
		$this->setUserId($userId);
90
		$this->setCircleId($circleUniqueId);
91
		$this->setLevel(Member::LEVEL_NONE);
92
		$this->setStatus(Member::STATUS_NONMEMBER);
93
	}
94
95
96
	/**
97
	 * @param string $circleUniqueId
98
	 *
99
	 * @return $this
100
	 */
101
	public function setCircleId($circleUniqueId) {
102
		$this->circleUniqueId = $circleUniqueId;
103
104
		return $this;
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getCircleId() {
111
		return $this->circleUniqueId;
112
	}
113
114
115
	/**
116
	 * @return int
117
	 */
118
	public function getType() {
119
		return $this->type;
120
	}
121
122
	public function setType($type) {
123
		$this->type = (int)$type;
124
	}
125
126
127
	public function getViewerType() {
128
		if ($this->getType() === 2) {
129
			return 'group';
130
		} else {
131
			return 'user';
132
		}
133
	}
134
135
	public function setUserId($userId) {
136
		$this->userId = $userId;
137
138
		if ($this->getType() === Member::TYPE_USER) {
139
			$this->setDisplayName(MiscService::staticGetDisplayName($userId, true));
140
		} else {
141
			$this->setDisplayName($userId);
142
		}
143
144
		return $this;
145
	}
146
147
	public function getUserId() {
148
		return $this->userId;
149
	}
150
151
152
	public function setDisplayName($display) {
153
		$this->displayName = $display;
154
155
		return $this;
156
	}
157
158
	public function getDisplayName() {
159
		return $this->displayName;
160
	}
161
162
163
	public function setLevel($level) {
164
		$this->level = (int)$level;
165
166
		return $this;
167
	}
168
169
	public function getLevel() {
170
		return $this->level;
171
	}
172
173
174
	public function setNote($note) {
175
		$this->note = $note;
176
177
		return $this;
178
	}
179
180
	public function getNote() {
181
		return $this->note;
182
	}
183
184
185
	public function setStatus($status) {
186
		if (is_null($status)) {
187
			$this->status = self::STATUS_NONMEMBER;
188
		} else {
189
			$this->status = $status;
190
		}
191
192
		return $this;
193
	}
194
195
	public function getStatus() {
196
		return $this->status;
197
	}
198
199
200
	public function setJoined($joined) {
201
		$this->joined = $joined;
202
203
		return $this;
204
	}
205
206
	public function getJoined() {
207
		return $this->joined;
208
	}
209
210
211
	public function isLevel($level) {
212
		return ($this->getLevel() >= $level);
213
	}
214
215
216
	public function isAlmostMember() {
217
		return ($this->getStatus() === Member::STATUS_INVITED
218
				|| $this->getStatus() === Member::STATUS_REQUEST);
219
	}
220
221
222
	protected function setAsAMember($level = 1) {
223
		$this->setStatus(Member::STATUS_MEMBER);
224
		$this->setLevel($level);
225
	}
226
227
228
	/**
229
	 * @param $arr
230
	 *
231
	 * 0.13.0 : remove both key_exists condition
232
	 *
233
	 * @return null|Member
234
	 */
235
	public static function fromArray($arr) {
236
		if ($arr === null) {
237
			return null;
238
		}
239
240
		$member = new Member();
241
		$member->setCircleId($arr['circle_id']);
242
		$member->setLevel($arr['level']);
243
244
		if (key_exists('user_type', $arr)) {
245
			$member->setType($arr['user_type']);
246
		}
247
		if (key_exists('type', $arr)) {
248
			$member->setType($arr['type']);
249
		}
250
251
		$member->setUserId($arr['user_id']);
252
		$member->setStatus($arr['status']);
253
		$member->setNote($arr['note']);
254
		$member->setJoined($arr['joined']);
255
256
		return $member;
257
	}
258
259
260
	/**
261
	 * @param $json
262
	 *
263
	 * @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...
264
	 */
265
	public static function fromJSON($json) {
266
		return self::fromArray(json_decode($json, true));
267
	}
268
269
270
	public function jsonSerialize() {
271
		return array(
272
			'circle_id'    => $this->getCircleId(),
273
			'user_id'      => $this->getUserId(),
274
			'user_type'    => $this->getType(),
275
			'display_name' => $this->getDisplayName(),
276
			'level'        => $this->getLevel(),
277
			'level_string' => $this->getLevelString(),
278
			'status'       => $this->getStatus(),
279
			'note'         => $this->getNote(),
280
			'joined'       => $this->getJoined()
281
		);
282
	}
283
284
	public function getLevelString() {
285
		switch ($this->getLevel()) {
286
			case self::LEVEL_NONE:
287
				return 'Not a member';
288
			case self::LEVEL_MEMBER:
289
				return 'Member';
290
			case self::LEVEL_MODERATOR:
291
				return 'Moderator';
292
			case self::LEVEL_ADMIN:
293
				return 'Admin';
294
			case self::LEVEL_OWNER:
295
				return 'Owner';
296
		}
297
298
		return 'none';
299
	}
300
}
301