Completed
Push — master ( 060fe4...9de15f )
by Maxence
02:41
created

BaseMember::setStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
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
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
	/**
230
	 * @param $arr
231
	 *
232
	 * @return null|Member
233
	 */
234
	public static function fromArray($arr) {
235
		$member = new Member();
236
		$member->setCircleId($arr['circle_id']);
237
		$member->setLevel($arr['level']);
238
		$member->setType($arr['type']);
239
		$member->setUserId($arr['user_id']);
240
		$member->setStatus($arr['status']);
241
		$member->setNote($arr['note']);
242
		$member->setJoined($arr['joined']);
243
244
		return $member;
245
	}
246
247
248
	/**
249
	 * @param $json
250
	 *
251
	 * @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...
252
	 */
253
	public static function fromJSON($json) {
254
		return self::fromArray(json_decode($json, true));
255
	}
256
257
258
	public function jsonSerialize() {
259
		return array(
260
			'circle_id'    => $this->getCircleId(),
261
			'user_id'      => $this->getUserId(),
262
			'type'         => $this->getType(),
263
			'display_name' => $this->getDisplayName(),
264
			'level'        => $this->getLevel(),
265
			'level_string' => $this->getLevelString(),
266
			'status'       => $this->getStatus(),
267
			'note'         => $this->getNote(),
268
			'joined'       => $this->getJoined()
269
		);
270
	}
271
272
	public function getLevelString() {
273
		switch ($this->getLevel()) {
274
			case self::LEVEL_NONE:
275
				return 'Not a member';
276
			case self::LEVEL_MEMBER:
277
				return 'Member';
278
			case self::LEVEL_MODERATOR:
279
				return 'Moderator';
280
			case self::LEVEL_ADMIN:
281
				return 'Admin';
282
			case self::LEVEL_OWNER:
283
				return 'Owner';
284
		}
285
286
		return 'none';
287
	}
288
}
289