Completed
Push — master ( 70edb2...6362ed )
by Maxence
02:18
created

BaseMember::getViewerId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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