Completed
Pull Request — master (#94)
by Maxence
02:28
created

BaseMember::isAlmostMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 3
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 int */
47
	private $circleId;
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
	public function __construct($l10n, $userId = '', $circleId = -1) {
74
		$this->l10n = $l10n;
75
76
		if ($userId !== '') {
77
			$this->setUserId($userId);
78
		}
79
		if ($circleId > -1) {
80
			$this->setCircleId($circleId);
81
		}
82
		$this->setLevel(Member::LEVEL_NONE);
83
		$this->setStatus(Member::STATUS_NONMEMBER);
84
	}
85
86
87
	public function setCircleId($circleId) {
88
		$this->circleId = (int)$circleId;
89
90
		return $this;
91
	}
92
93
	public function getCircleId() {
94
		return $this->circleId;
95
	}
96
97
	/**
98
	 * @param string $groupId
99
	 */
100
	public function setGroupId($groupId) {
101
		$this->groupId = $groupId;
102
	}
103
104
	/**
105
	 * @return string
106
	 */
107
	public function getGroupId() {
108
		return $this->groupId;
109
	}
110
111
112
	public function setUserId($userId) {
113
		$this->userId = $userId;
114
115
		$this->setDisplayName(
116
			\OC::$server->getUserManager()
117
						->get($userId)
118
						->getDisplayName()
119
		);
120
121
		return $this;
122
	}
123
124
	public function getUserId() {
125
		return $this->userId;
126
	}
127
128
129
	public function setDisplayName($display) {
130
		$this->displayName = $display;
131
132
		return $this;
133
	}
134
135
	public function getDisplayNAme() {
136
		return $this->displayName;
137
	}
138
139
140
	public function setLevel($level) {
141
		$this->level = (int)$level;
142
143
		return $this;
144
	}
145
146
	public function getLevel() {
147
		return $this->level;
148
	}
149
150
151
	public function setNote($note) {
152
		$this->note = $note;
153
154
		return $this;
155
	}
156
157
	public function getNote() {
158
		return $this->note;
159
	}
160
161
162
	public function setStatus($status) {
163
		if (is_null($status)) {
164
			$this->status = self::STATUS_NONMEMBER;
165
		} else {
166
			$this->status = $status;
167
		}
168
169
		return $this;
170
	}
171
172
	public function getStatus() {
173
		return $this->status;
174
	}
175
176
177
	public function setJoined($joined) {
178
		$this->joined = $joined;
179
180
		return $this;
181
	}
182
183
	public function getJoined() {
184
		return $this->joined;
185
	}
186
187
188
	public function isLevel($level) {
189
		return ($this->getLevel() >= $level);
190
	}
191
192
193
	public function isAlmostMember() {
194
		return ($this->getStatus() === Member::STATUS_INVITED
195
				|| $this->getStatus() === Member::STATUS_REQUEST);
196
	}
197
198
199
	protected function setAsAMember($level = 1) {
200
		$this->setStatus(Member::STATUS_MEMBER);
201
		$this->setLevel($level);
202
	}
203
204
205
	public static function fromArray($l10n, $arr) {
206
		if ($arr === null) {
207
			return null;
208
		}
209
210
		$member = new Member($l10n);
211
212
		$member->setCircleId($arr['circle_id']);
213
		$member->setLevel($arr['level']);
214
215
		if (key_exists('user_id', $arr)) {
216
			$member->setUserId($arr['user_id']);
217
		}
218
219
		if (key_exists('group_id', $arr)) {
220
			$member->setGroupId($arr['group_id']);
221
		}
222
223
		if (key_exists('status', $arr)) {
224
			$member->setStatus($arr['status']);
225
		}
226
227
		if (key_exists('note', $arr)) {
228
			$member->setNote($arr['note']);
229
		}
230
231
		if (key_exists('joined', $arr)) {
232
			$member->setJoined($arr['joined']);
233
		}
234
235
		return $member;
236
	}
237
238
239
	public static function fromJSON($l10n, $json) {
240
		return self::fromArray($l10n, json_decode($json, true));
241
	}
242
243
	public function jsonSerialize() {
244
		return array(
245
			'circle_id'    => $this->getCircleId(),
246
			'user_id'      => $this->getUserId(),
247
			'group_id'     => $this->getGroupId(),
248
			'display_name' => $this->getDisplayNAme(),
249
			'level'        => $this->getLevel(),
250
			'level_string' => $this->getLevelString(),
251
			'status'       => $this->getStatus(),
252
			'note'         => $this->getNote(),
253
			'joined'       => $this->getJoined()
254
		);
255
	}
256
257
	public function getLevelString() {
258
		switch ($this->getLevel()) {
259
			case self::LEVEL_NONE:
260
				return 'Not a member';
261
			case self::LEVEL_MEMBER:
262
				return 'Member';
263
			case self::LEVEL_MODERATOR:
264
				return 'Moderator';
265
			case self::LEVEL_ADMIN:
266
				return 'Admin';
267
			case self::LEVEL_OWNER:
268
				return 'Owner';
269
		}
270
271
		return 'none';
272
	}
273
}
274