Completed
Push — master ( 221ac4...66e177 )
by Maxence
9s
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\MemberIsNotOwnerException;
35
use OCA\Circles\Exceptions\MemberIsOwnerException;
36
37
class Member extends BaseMember implements \JsonSerializable {
38
39
40
	public function inviteToCircle($circleType) {
41
		if ($circleType === Circle::CIRCLES_PRIVATE) {
42
			return $this->inviteIntoPrivateCircle();
43
		}
44
45
		return $this->addMemberToCircle();
46
	}
47
48
49
	/**
50
	 * @param int $circleType
51
	 *
52
	 * @throws MemberCantJoinCircle
53
	 */
54
	public function joinCircle($circleType) {
55
56
		switch ($circleType) {
57
			case Circle::CIRCLES_HIDDEN:
58
			case Circle::CIRCLES_PUBLIC:
59
				return $this->addMemberToCircle();
60
61
			case Circle::CIRCLES_PRIVATE:
62
				return $this->joinPrivateCircle();
63
		}
64
65
		throw new MemberCantJoinCircle();
66
	}
67
68
69
	/**
70
	 * Update status of member like he joined a public circle.
71
	 */
72
	private function addMemberToCircle() {
73
74
		if ($this->getStatus() === Member::STATUS_NONMEMBER
75
			|| $this->getStatus() === Member::STATUS_KICKED
76
		) {
77
			$this->setAsAMember(Member::LEVEL_MEMBER);
78
		}
79
	}
80
81
82
	/**
83
	 * Update status of member like he joined a private circle
84
	 * (invite/request)
85
	 */
86 View Code Duplication
	private function joinPrivateCircle() {
87
88
		switch ($this->getStatus()) {
89
			case Member::STATUS_NONMEMBER:
90
			case Member::STATUS_KICKED:
91
				$this->setStatus(Member::STATUS_REQUEST);
92
				break;
93
94
			case Member::STATUS_INVITED:
95
				$this->setAsAMember(Member::LEVEL_MEMBER);
96
				break;
97
		}
98
	}
99
100
101 View Code Duplication
	private function inviteIntoPrivateCircle() {
102
		switch ($this->getStatus()) {
103
			case Member::STATUS_NONMEMBER:
104
			case Member::STATUS_KICKED:
105
				$this->setStatus(Member::STATUS_INVITED);
106
				break;
107
108
			case Member::STATUS_REQUEST:
109
				$this->setAsAMember(Member::LEVEL_MEMBER);
110
				break;
111
		}
112
	}
113
114
115
	/**
116
	 * @throws MemberIsNotModeratorException
117
	 */
118
	public function hasToBeModerator() {
119
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
120
			throw new MemberIsNotModeratorException();
121
		}
122
	}
123
124
125
	/**
126
	 * @throws MemberIsNotModeratorException
127
	 */
128
	public function hasToBeOwner() {
129
		if ($this->getLevel() < self::LEVEL_OWNER) {
130
			throw new MemberIsNotOwnerException();
131
		}
132
	}
133
134
135
	/**
136
	 * @throws MemberDoesNotExistException
137
	 */
138
	public function hasToBeMember() {
139
		if ($this->getLevel() < self::LEVEL_MEMBER) {
140
			throw new MemberDoesNotExistException();
141
		}
142
	}
143
144
145
	/**
146
	 * @throws MemberIsOwnerException
147
	 */
148
	public function cantBeOwner() {
149
		if ($this->getLevel() === self::LEVEL_OWNER) {
150
			throw new MemberIsOwnerException();
151
		}
152
	}
153
154
	/**
155
	 * @param $member
156
	 *
157
	 * @throws MemberAlreadyExistsException
158
	 * @throws MemberIsBlockedException
159
	 */
160
	public function hasToBeAbleToJoinTheCircle() {
161
162
		if ($this->getLevel() > 0) {
163
			throw new MemberAlreadyExistsException("You are already a member of this circle");
164
		}
165
166
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
167
			throw new MemberIsBlockedException("You are blocked from this circle");
168
		}
169
	}
170
171
	public function jsonSerialize() {
172
		return array(
173
			'circle_id'    => $this->getCircleId(),
174
			'user_id'      => $this->getUserId(),
175
			'level'        => $this->getLevel(),
176
			'level_string' => $this->getLevelString(),
177
			'status'       => $this->getStatus(),
178
			'note'         => $this->getNote(),
179
			'joined'       => $this->getJoined()
180
		);
181
	}
182
183
184
}
185
186
187