Completed
Push — some-scrutinizing ( 29b6ab...e5bac5 )
by Maxence
18:16
created

Member::joinOpenCircle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 2
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\MemberCantJoinCircle;
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 MemberCantJoinCircle
62
	 */
63
	public function joinCircle($circleType) {
64
65
		switch ($circleType) {
66
			case Circle::CIRCLES_HIDDEN:
67
			case Circle::CIRCLES_PUBLIC:
68
				return $this->joinOpenCircle();
69
70
			case Circle::CIRCLES_PRIVATE:
71
				return $this->joinPrivateCircle();
72
		}
73
74
		throw new MemberCantJoinCircle();
75
	}
76
77
	/**
78
	 * Update status of member like he joined a public circle.
79
	 */
80
	private function joinOpenCircle() {
81
82
		if ($this->getStatus() === Member::STATUS_NONMEMBER
83
			|| $this->getStatus() === Member::STATUS_KICKED
84
		) {
85
			$this->setStatus(Member::STATUS_MEMBER);
86
			$this->setLevel(Member::LEVEL_MEMBER);
87
		}
88
	}
89
90
	/**
91
	 * Update status of member like he joined a private circle
92
	 * (invite/request)
93
	 */
94
	private function joinPrivateCircle() {
95
96
		switch ($this->getStatus()) {
97
			case Member::STATUS_NONMEMBER:
98
			case Member::STATUS_KICKED:
99
				$this->setStatus(Member::STATUS_REQUEST);
100
				break;
101
102
			case Member::STATUS_INVITED:
103
				$this->setStatus(Member::STATUS_MEMBER);
104
				$this->setLevel(Member::LEVEL_MEMBER);
105
				break;
106
		}
107
	}
108
109
110
	public function isMember() {
111
		return ($this->getLevel() >= self::LEVEL_MEMBER);
112
	}
113
114
	public function isModerator() {
115
		return ($this->getLevel() >= self::LEVEL_MODERATOR);
116
	}
117
118
	/**
119
	 * @throws MemberIsNotModeratorException
120
	 */
121
	public function hasToBeModerator() {
122
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
123
			throw new MemberIsNotModeratorException();
124
		}
125
	}
126
127
128
	/**
129
	 * @throws MemberDoesNotExistException
130
	 */
131
	public function hasToBeMember() {
132
		if ($this->getLevel() < self::LEVEL_MEMBER) {
133
			throw new MemberDoesNotExistException();
134
		}
135
	}
136
137
138
	/**
139
	 * @throws MemberIsOwnerException
140
	 */
141
	public function cantBeOwner() {
142
		if ($this->getLevel() === self::LEVEL_OWNER) {
143
			throw new MemberIsOwnerException();
144
		}
145
	}
146
147
	/**
148
	 * @param $member
149
	 *
150
	 * @throws MemberAlreadyExistsException
151
	 * @throws MemberIsBlockedException
152
	 */
153
	public function hasToBeAbleToJoinTheCircle() {
154
155
		if ($this->getLevel() > 0) {
156
			throw new MemberAlreadyExistsException("You are already a member of this circle");
157
		}
158
159
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
160
			throw new MemberIsBlockedException("You are blocked from this circle");
161
		}
162
	}
163
164
	public function jsonSerialize() {
165
		return array(
166
			'circleid'     => $this->getCircleId(),
167
			'userid'       => $this->getUserId(),
168
			'level'        => $this->getLevel(),
169
			'level_string' => $this->getLevelString(),
170
			'status'       => $this->getStatus(),
171
			'joined'       => $this->getJoined()
172
		);
173
	}
174
175
176
	public static function levelString($level) {
177
		switch ($level) {
178
			case self::LEVEL_NONE:
179
				return 'Not a member';
180
			case self::LEVEL_MEMBER:
181
				return 'Member';
182
			case self::LEVEL_MODERATOR:
183
				return 'Moderator';
184
			case self::LEVEL_ADMIN:
185
				return 'Admin';
186
			case self::LEVEL_OWNER:
187
				return 'Owner';
188
		}
189
190
		return 'none';
191
	}
192
193
}
194
195
196