Completed
Push — master ( 134274...b890e4 )
by Maxence
03:03
created

BaseMember::getUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
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 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 int */
47
	private $circleId;
48
49
50
	/** @var L10N */
51
	protected $l10n;
52
53
	/** @var string */
54
	private $userId;
55
56
	/** @var int */
57
	private $level;
58
59
	/** @var string */
60
	private $status;
61
62
	/** @var string */
63
	private $note;
64
65
	/** @var string */
66
	private $joined;
67
68
	public function __construct($l10n, $userId = '', $circleId = -1) {
69
		$this->l10n = $l10n;
70
71
		if ($userId !== '') {
72
			$this->setUserId($userId);
73
		}
74
		if ($circleId > -1) {
75
			$this->setCircleId($circleId);
76
		}
77
		$this->setLevel(Member::LEVEL_NONE);
78
		$this->setStatus(Member::STATUS_NONMEMBER);
79
	}
80
81
82
	public function setCircleId($circleId) {
83
		$this->circleId = (int)$circleId;
84
85
		return $this;
86
	}
87
88
	public function getCircleId() {
89
		return $this->circleId;
90
	}
91
92
93
	public function setUserId($userId) {
94
		$this->userId = $userId;
95
96
		return $this;
97
	}
98
99
	public function getUserId() {
100
		return $this->userId;
101
	}
102
103
104
	public function setLevel($level) {
105
		$this->level = (int)$level;
106
107
		return $this;
108
	}
109
110
	public function getLevel() {
111
		return $this->level;
112
	}
113
114
115
	public function setNote($note) {
116
		$this->note = $note;
117
118
		return $this;
119
	}
120
121
	public function getNote() {
122
		return $this->note;
123
	}
124
125
126
	public function setStatus($status) {
127
		if (is_null($status)) {
128
			$this->status = self::STATUS_NONMEMBER;
129
		} else {
130
			$this->status = $status;
131
		}
132
133
		return $this;
134
	}
135
136
	public function getStatus() {
137
		return $this->status;
138
	}
139
140
141
	public function setJoined($joined) {
142
		$this->joined = $joined;
143
144
		return $this;
145
	}
146
147
	public function getJoined() {
148
		return $this->joined;
149
	}
150
151
152
	public function isLevel($level) {
153
		return ($this->getLevel() >= $level);
154
	}
155
156
157
	public function isAlmostMember() {
158
		return ($this->getStatus() === Member::STATUS_INVITED
159
				|| $this->getStatus() === Member::STATUS_REQUEST);
160
	}
161
162
163
	protected function setAsAMember($level = 1) {
164
		$this->setStatus(Member::STATUS_MEMBER);
165
		$this->setLevel($level);
166
	}
167
168
169
	public static function fromArray2($l10n, $arr) {
170
		if ($arr === null)
171
			return null;
172
173
		$member = new Member($l10n);
174
175
		$member->setCircleId($arr['circle_id']);
176
		$member->setUserId($arr['user_id']);
177
		$member->setLevel($arr['level']);
178
		$member->setStatus($arr['status']);
179
		if (key_exists('note', $arr)) {
180
			$member->setNote($arr['note']);
181
		}
182
183
		if (key_exists('joined', $arr)) {
184
			$member->setJoined($arr['joined']);
185
		}
186
187
		return $member;
188
	}
189
190
191
	public static function fromJSON($l10n, $json) {
192
		return self::fromArray2($l10n, json_decode($json, true));
193
	}
194
195
	public function jsonSerialize() {
196
		return array(
197
			'circle_id'    => $this->getCircleId(),
198
			'user_id'      => $this->getUserId(),
199
			'level'        => $this->getLevel(),
200
			'level_string' => $this->getLevelString(),
201
			'status'       => $this->getStatus(),
202
			'note'         => $this->getNote(),
203
			'joined'       => $this->getJoined()
204
		);
205
	}
206
207
	public function getLevelString() {
208
		switch ($this->getLevel()) {
209
			case self::LEVEL_NONE:
210
				return 'Not a member';
211
			case self::LEVEL_MEMBER:
212
				return 'Member';
213
			case self::LEVEL_MODERATOR:
214
				return 'Moderator';
215
			case self::LEVEL_ADMIN:
216
				return 'Admin';
217
			case self::LEVEL_OWNER:
218
				return 'Owner';
219
		}
220
221
		return 'none';
222
	}
223
}
224