Completed
Push — master ( d27d80...f88e97 )
by Maxence
03:07
created

BaseMember::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
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
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
	 * @param $arr
230
	 *
231
	 * @deprecated use only by Provider
232
	 *
233
	 * @return null|Member
234
	 */
235
	public static function fromArray($arr) {
236
		if ($arr === null) {
237
			return null;
238
		}
239
240
		$member = new Member();
241
242
		$member->setCircleId($arr['circle_id']);
243
		$member->setLevel($arr['level']);
244
		if (key_exists('user_id', $arr)) {
245
			$member->setUserId($arr['user_id']);
246
		}
247
248
//		if (key_exists('group_id', $arr)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
//			$member->setGroupId($arr['group_id']);
250
//		}
251
252
		if (key_exists('status', $arr)) {
253
			$member->setStatus($arr['status']);
254
		}
255
256
		if (key_exists('note', $arr)) {
257
			$member->setNote($arr['note']);
258
		}
259
260
		if (key_exists('joined', $arr)) {
261
			$member->setJoined($arr['joined']);
262
		}
263
264
		return $member;
265
	}
266
267
268
	/**
269
	 * @param $json
270
	 *
271
	 * @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...
272
	 * @deprecated
273
	 */
274
	public static function fromJSON($json) {
275
		return self::fromArray(json_decode($json, true));
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\BaseMember::fromArray() has been deprecated with message: use only by Provider

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
276
	}
277
278
	public function jsonSerialize() {
279
		return array(
280
			'circle_id'    => $this->getCircleId(),
281
			'user_id'      => $this->getUserId(),
282
			//			'group_id'     => $this->getGroupId(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
283
			'type'         => $this->getType(),
284
			'viewer_type'  => $this->getViewerType(),
285
			'display_name' => $this->getDisplayName(),
286
			'level'        => $this->getLevel(),
287
			'level_string' => $this->getLevelString(),
288
			'status'       => $this->getStatus(),
289
			'note'         => $this->getNote(),
290
			'joined'       => $this->getJoined()
291
		);
292
	}
293
294
	public function getLevelString() {
295
		switch ($this->getLevel()) {
296
			case self::LEVEL_NONE:
297
				return 'Not a member';
298
			case self::LEVEL_MEMBER:
299
				return 'Member';
300
			case self::LEVEL_MODERATOR:
301
				return 'Moderator';
302
			case self::LEVEL_ADMIN:
303
				return 'Admin';
304
			case self::LEVEL_OWNER:
305
				return 'Owner';
306
		}
307
308
		return 'none';
309
	}
310
}
311