Completed
Push — master ( 93d6a4...66e2aa )
by Maxence
04:08
created

BaseMember::setNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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