Completed
Push — some-scrutinizing ( b2f84c...29b6ab )
by Maxence
23:41
created

Member::hasToBeAbleToJoinTheCircle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
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 isMember() {
114
		return ($this->getLevel() >= self::LEVEL_MEMBER);
115
	}
116
117
	public function isModerator() {
118
		return ($this->getLevel() >= self::LEVEL_MODERATOR);
119
	}
120
121
	/**
122
	 * @throws MemberIsNotModeratorException
123
	 */
124
	public function hasToBeModerator() {
125
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
126
			throw new MemberIsNotModeratorException();
127
		}
128
	}
129
130
131
	/**
132
	 * @throws MemberDoesNotExistException
133
	 */
134
	public function hasToBeMember() {
135
		if ($this->getLevel() < self::LEVEL_MEMBER) {
136
			throw new MemberDoesNotExistException();
137
		}
138
	}
139
140
141
	/**
142
	 * @throws MemberIsOwnerException
143
	 */
144
	public function cantBeOwner() {
145
		if ($this->getLevel() === self::LEVEL_OWNER) {
146
			throw new MemberIsOwnerException();
147
		}
148
	}
149
150
	/**
151
	 * @param $member
152
	 *
153
	 * @throws MemberAlreadyExistsException
154
	 * @throws MemberIsBlockedException
155
	 */
156
	public function hasToBeAbleToJoinTheCircle() {
157
158
		if ($this->getLevel() > 0) {
159
			throw new MemberAlreadyExistsException("You are already a member of this circle");
160
		}
161
162
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
163
			throw new MemberIsBlockedException("You are blocked from this circle");
164
		}
165
	}
166
167
	public function jsonSerialize() {
168
		return array(
169
			'circleid'     => $this->getCircleId(),
170
			'userid'       => $this->getUserId(),
171
			'level'        => $this->getLevel(),
172
			'level_string' => $this->getLevelString(),
173
			'status'       => $this->getStatus(),
174
			'joined'       => $this->getJoined()
175
		);
176
	}
177
178
179
	public static function levelString($level) {
180
		switch ($level) {
181
			case self::LEVEL_NONE:
182
				return 'Not a member';
183
			case self::LEVEL_MEMBER:
184
				return 'Member';
185
			case self::LEVEL_MODERATOR:
186
				return 'Moderator';
187
			case self::LEVEL_ADMIN:
188
				return 'Admin';
189
			case self::LEVEL_OWNER:
190
				return 'Owner';
191
		}
192
193
		return 'none';
194
	}
195
196
}
197
198
199