Completed
Push — some-scrutinizing ( 26da3a...6d8498 )
by Maxence
02:28
created

Member::getLevelString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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 OCA\Circles\Exceptions\MemberAlreadyExistsException;
30
use OCA\Circles\Exceptions\MemberCantJoinPersonalCircle;
31
use OCA\Circles\Exceptions\MemberDoesNotExistException;
32
use OCA\Circles\Exceptions\MemberIsBlockedException;
33
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
34
use OCA\Circles\Exceptions\MemberIsOwnerException;
35
36
class Member extends BaseMember implements \JsonSerializable {
37
38
	private $levelString;
39
40
	public function setLevel($level) {
41
		parent::setLevel($level);
42
		$this->setLevelString(self::levelString($this->getLevel()));
43
44
		return $this;
45
	}
46
47
	public function setLevelString($str) {
48
		$this->levelString = $str;
49
50
		return $this;
51
	}
52
53
	public function getLevelString() {
54
		return $this->levelString;
55
	}
56
57
58
	/**
59
	 * @param int $circleType
60
	 *
61
	 * @throws MemberCantJoinPersonalCircle
62
	 */
63
	public function joinCircle($circleType) {
64
65
		switch ($circleType) {
66
			case Circle::CIRCLES_HIDDEN:
67
			case Circle::CIRCLES_PUBLIC:
68
				$this->joinOpenCircle();
69
				break;
70
71
			case Circle::CIRCLES_PRIVATE:
72
				$this->joinPrivateCircle();
73
				break;
74
75
			case Circle::CIRCLES_PERSONAL:
76
				throw new MemberCantJoinPersonalCircle();
77
		}
78
	}
79
80
	/**
81
	 * Update status of member like he joined a public circle.
82
	 */
83
	private function joinOpenCircle() {
84
85
		if ($this->getStatus() === Member::STATUS_NONMEMBER
86
			|| $this->getStatus() === Member::STATUS_KICKED
87
		) {
88
			$this->setStatus(Member::STATUS_MEMBER);
89
			$this->setLevel(Member::LEVEL_MEMBER);
90
		}
91
	}
92
93
	/**
94
	 * Update status of member like he joined a private circle
95
	 * (invite/request)
96
	 */
97
	private function joinPrivateCircle() {
98
99
		switch ($this->getStatus()) {
100
			case Member::STATUS_NONMEMBER:
101
			case Member::STATUS_KICKED:
102
				$this->setStatus(Member::STATUS_REQUEST);
103
				break;
104
105
			case Member::STATUS_INVITED:
106
				$this->setStatus(Member::STATUS_MEMBER);
107
				$this->setLevel(Member::LEVEL_MEMBER);
108
				break;
109
		}
110
	}
111
112
113
	public function isModerator() {
114
		return ($this->getLevel() >= self::LEVEL_MODERATOR);
115
	}
116
117
	/**
118
	 * @throws MemberIsNotModeratorException
119
	 */
120
	public function hasToBeModerator() {
121
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
122
			throw new MemberIsNotModeratorException();
123
		}
124
	}
125
126
127
	/**
128
	 * @throws MemberDoesNotExistException
129
	 */
130
	public function hasToBeMember() {
131
		if ($this->getLevel() < self::LEVEL_MEMBER) {
132
			throw new MemberDoesNotExistException();
133
		}
134
	}
135
136
137
	/**
138
	 * @throws MemberIsOwnerException
139
	 */
140
	public function cantBeOwner() {
141
		if ($this->getLevel() === self::LEVEL_OWNER) {
142
			throw new MemberIsOwnerException();
143
		}
144
	}
145
146
	/**
147
	 * @param $member
148
	 *
149
	 * @throws MemberAlreadyExistsException
150
	 * @throws MemberIsBlockedException
151
	 */
152
	public function hasToBeAbleToJoinTheCircle() {
153
154
		if ($this->getLevel() > 0) {
155
			throw new MemberAlreadyExistsException("You are already a member of this circle");
156
		}
157
158
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
159
			throw new MemberIsBlockedException("You are blocked from this circle");
160
		}
161
	}
162
163
	public function jsonSerialize() {
164
		return array(
165
			'circleid'     => $this->getCircleId(),
166
			'userid'       => $this->getUserId(),
167
			'level'        => $this->getLevel(),
168
			'level_string' => $this->getLevelString(),
169
			'status'       => $this->getStatus(),
170
			'joined'       => $this->getJoined()
171
		);
172
	}
173
174
175
	public static function fromArray($arr) {
176
177
		if (!is_array($arr)) {
178
			return null;
179
		}
180
181
		$member = new Member();
0 ignored issues
show
Bug introduced by
The call to Member::__construct() misses some required arguments starting with $circleId.
Loading history...
182
183
		$member->setCircleId($arr['circle_id']);
184
		$member->setUserId($arr['user_id']);
185
		$member->setLevel($arr['level']);
186
		$member->setStatus($arr['status']);
187
		if (key_exists('note', $arr)) {
188
			$member->setNote($arr['note']);
189
		}
190
		if (key_exists('joined', $arr)) {
191
			$member->setJoined($arr['joined']);
192
		}
193
194
		return $member;
195
	}
196
197
198
	public static function levelString($level) {
199
		switch ($level) {
200
			case self::LEVEL_NONE:
201
				return 'Not a member';
202
			case self::LEVEL_MEMBER:
203
				return 'Member';
204
			case self::LEVEL_MODERATOR:
205
				return 'Moderator';
206
			case self::LEVEL_ADMIN:
207
				return 'Admin';
208
			case self::LEVEL_OWNER:
209
				return 'Owner';
210
		}
211
212
		return 'none';
213
	}
214
215
216
	public function toString() {
217
		return "toString ?";
218
	}
219
}
220
221
222