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