Completed
Push — some-scrutinizing ( 18fbfb...d3de44 )
by Maxence
03:16
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\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
	public function inviteToCircle($circleType) {
59
		if ($circleType === Circle::CIRCLES_PRIVATE) {
60
			return $this->inviteIntoPrivateCircle();
61
		}
62
63
		return $this->addMemberToCircle();
64
	}
65
66
67
	/**
68
	 * @param int $circleType
69
	 *
70
	 * @throws MemberCantJoinCircle
71
	 */
72
	public function joinCircle($circleType) {
73
74
		switch ($circleType) {
75
			case Circle::CIRCLES_HIDDEN:
76
			case Circle::CIRCLES_PUBLIC:
77
				return $this->addMemberToCircle();
78
79
			case Circle::CIRCLES_PRIVATE:
80
				return $this->joinPrivateCircle();
81
		}
82
83
		throw new MemberCantJoinCircle();
84
	}
85
86
87
	/**
88
	 * Update status of member like he joined a public circle.
89
	 */
90
	private function addMemberToCircle() {
91
92
		if ($this->getStatus() === Member::STATUS_NONMEMBER
93
			|| $this->getStatus() === Member::STATUS_KICKED
94
		) {
95
			$this->setAsAMember(Member::LEVEL_MEMBER);
96
		}
97
	}
98
99
100
	/**
101
	 * Update status of member like he joined a private circle
102
	 * (invite/request)
103
	 */
104 View Code Duplication
	private function joinPrivateCircle() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
106
		switch ($this->getStatus()) {
107
			case Member::STATUS_NONMEMBER:
108
			case Member::STATUS_KICKED:
109
				$this->setStatus(Member::STATUS_REQUEST);
110
				break;
111
112
			case Member::STATUS_INVITED:
113
				$this->setAsAMember(Member::LEVEL_MEMBER);
114
				break;
115
		}
116
	}
117
118
119 View Code Duplication
	private function inviteIntoPrivateCircle() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
		switch ($this->getStatus()) {
121
			case Member::STATUS_NONMEMBER:
122
			case Member::STATUS_KICKED:
123
				$this->setStatus(Member::STATUS_INVITED);
124
				break;
125
126
			case Member::STATUS_REQUEST:
127
				$this->setAsAMember(Member::LEVEL_MEMBER);
128
				break;
129
		}
130
	}
131
132
133
134
	/**
135
	 * @throws MemberIsNotModeratorException
136
	 */
137
	public function hasToBeModerator() {
138
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
139
			throw new MemberIsNotModeratorException();
140
		}
141
	}
142
143
144
	/**
145
	 * @throws MemberDoesNotExistException
146
	 */
147
	public function hasToBeMember() {
148
		if ($this->getLevel() < self::LEVEL_MEMBER) {
149
			throw new MemberDoesNotExistException();
150
		}
151
	}
152
153
154
	/**
155
	 * @throws MemberIsOwnerException
156
	 */
157
	public function cantBeOwner() {
158
		if ($this->getLevel() === self::LEVEL_OWNER) {
159
			throw new MemberIsOwnerException();
160
		}
161
	}
162
163
	/**
164
	 * @param $member
165
	 *
166
	 * @throws MemberAlreadyExistsException
167
	 * @throws MemberIsBlockedException
168
	 */
169
	public function hasToBeAbleToJoinTheCircle() {
170
171
		if ($this->getLevel() > 0) {
172
			throw new MemberAlreadyExistsException("You are already a member of this circle");
173
		}
174
175
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
176
			throw new MemberIsBlockedException("You are blocked from this circle");
177
		}
178
	}
179
180
	public function jsonSerialize() {
181
		return array(
182
			'circle_id'    => $this->getCircleId(),
183
			'user_id'      => $this->getUserId(),
184
			'level'        => $this->getLevel(),
185
			'level_string' => $this->getLevelString(),
186
			'status'       => $this->getStatus(),
187
			'joined'       => $this->getJoined()
188
		);
189
	}
190
191
192
	public static function levelString($level) {
193
		switch ($level) {
194
			case self::LEVEL_NONE:
195
				return 'Not a member';
196
			case self::LEVEL_MEMBER:
197
				return 'Member';
198
			case self::LEVEL_MODERATOR:
199
				return 'Moderator';
200
			case self::LEVEL_ADMIN:
201
				return 'Admin';
202
			case self::LEVEL_OWNER:
203
				return 'Owner';
204
		}
205
206
		return 'none';
207
	}
208
209
}
210
211
212