Completed
Push — master ( 060fe4...9de15f )
by Maxence
02:41
created

Member::cantBeOwner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
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\CircleTypeNotValidException;
30
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
31
use OCA\Circles\Exceptions\MemberCantJoinCircleException;
32
use OCA\Circles\Exceptions\MemberDoesNotExistException;
33
use OCA\Circles\Exceptions\MemberIsBlockedException;
34
use OCA\Circles\Exceptions\MemberIsNotAdminException;
35
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
36
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
37
use OCA\Circles\Exceptions\MemberIsOwnerException;
38
use OCA\Circles\Exceptions\ModeratorIsNotHighEnoughException;
39
40
class Member extends BaseMember {
41
42
43
	public function inviteToCircle($circleType) {
44
45
		if ($circleType === 0) {
46
			throw new CircleTypeNotValidException('Circle Type is not valid');
47
		}
48
49
		if ($circleType === Circle::CIRCLES_CLOSED) {
50
			return $this->inviteIntoClosedCircle();
51
		}
52
53
		return $this->addMemberToCircle();
54
	}
55
56
57
	/**
58
	 * @param int $circleType
59
	 *
60
	 * @throws MemberCantJoinCircleException
61
	 */
62
	public function joinCircle($circleType) {
63
64
		switch ($circleType) {
65
			case Circle::CIRCLES_SECRET:
66
			case Circle::CIRCLES_PUBLIC:
67
				return $this->addMemberToCircle();
68
69
			case Circle::CIRCLES_CLOSED:
70
				return $this->joinClosedCircle();
71
		}
72
73
		throw new MemberCantJoinCircleException($this->l10n->t('You cannot join this circle'));
74
	}
75
76
77
	/**
78
	 * Update status of member like he joined a public circle.
79
	 */
80
	private function addMemberToCircle() {
81
82
		if ($this->getStatus() === Member::STATUS_NONMEMBER
83
			|| $this->getStatus() === Member::STATUS_KICKED
84
		) {
85
			$this->setAsAMember(Member::LEVEL_MEMBER);
86
		}
87
	}
88
89
90
	/**
91
	 * Update status of member like he joined a closed circle
92
	 * (invite/request)
93
	 */
94 View Code Duplication
	private function joinClosedCircle() {
0 ignored issues
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...
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->setAsAMember(Member::LEVEL_MEMBER);
104
				break;
105
		}
106
	}
107
108
109 View Code Duplication
	private function inviteIntoClosedCircle() {
0 ignored issues
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...
110
		switch ($this->getStatus()) {
111
			case Member::STATUS_NONMEMBER:
112
			case Member::STATUS_KICKED:
113
				$this->setStatus(Member::STATUS_INVITED);
114
				break;
115
116
			case Member::STATUS_REQUEST:
117
				$this->setAsAMember(Member::LEVEL_MEMBER);
118
				break;
119
		}
120
	}
121
122
123
	/**
124
	 * @throws MemberIsNotModeratorException
125
	 */
126
	public function hasToBeModerator() {
127
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
128
			throw new MemberIsNotModeratorException(
129
				$this->l10n->t('This member is not a moderator')
130
			);
131
		}
132
	}
133
134
135
	/**
136
	 * @param $level
137
	 *
138
	 * @throws ModeratorIsNotHighEnoughException
139
	 */
140
	public function hasToBeHigherLevel($level) {
141
142
		if ($this->getLevel() <= $level) {
143
			throw new ModeratorIsNotHighEnoughException(
144
				$this->l10n->t('Not enough privileges')
145
			);
146
		}
147
	}
148
149
150
	/**
151
	 * @throws MemberIsNotModeratorException
152
	 */
153
	public function hasToBeOwner() {
154
		if ($this->getLevel() < self::LEVEL_OWNER) {
155
			throw new MemberIsNotOwnerException(
156
				$this->l10n->t('This member is not the owner of the circle')
157
			);
158
		}
159
	}
160
161
162
	/**
163
	 * @throws MemberIsNotModeratorException
164
	 */
165
	public function hasToBeAdmin() {
166
		if ($this->getLevel() < self::LEVEL_ADMIN) {
167
			throw new MemberIsNotAdminException(
168
				$this->l10n->t('This member is not admin of the circle')
169
			);
170
		}
171
	}
172
173
	/**
174
	 * @throws MemberDoesNotExistException
175
	 */
176
	public function hasToBeMember() {
177
		if ($this->getLevel() < self::LEVEL_MEMBER) {
178
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
179
		}
180
181
		return true;
182
	}
183
184
185
	/**
186
	 * @throws MemberDoesNotExistException
187
	 */
188
	public function hasToBeMemberOrAlmost() {
189
		if ($this->isAlmostMember() || $this->hasToBeMember()) {
190
			return true;
191
		}
192
193
		throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
194
	}
195
196
197
	/**
198
	 * @throws MemberIsOwnerException
199
	 */
200
	public function cantBeOwner() {
201
		if ($this->getLevel() === self::LEVEL_OWNER) {
202
			throw new MemberIsOwnerException(
203
				$this->l10n->t('This member is the owner of the circle')
204
			);
205
		}
206
	}
207
208
209
	/**
210
	 * return if member already exists
211
	 *
212
	 * @return bool
213
	 */
214
	public function alreadyExistOrJoining() {
215
		return ($this->getLevel() > Member::LEVEL_NONE
216
				|| ($this->getStatus() !== Member::STATUS_NONMEMBER
217
					&& $this->getStatus() !== Member::STATUS_REQUEST)
218
		);
219
	}
220
221
222
	/**
223
	 * @throws MemberAlreadyExistsException
224
	 * @throws MemberIsBlockedException
225
	 */
226 View Code Duplication
	public function hasToBeAbleToJoinTheCircle() {
0 ignored issues
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...
227
228
		if ($this->getLevel() > 0) {
229
			throw new MemberAlreadyExistsException(
230
				$this->l10n->t("You are already a member of this circle")
231
			);
232
		}
233
234
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
235
			throw new MemberIsBlockedException(
236
				$this->l10n->t("You have been blocked from this circle")
237
			);
238
		}
239
	}
240
241
242
	/**
243
	 * @throws MemberAlreadyExistsException
244
	 */
245 View Code Duplication
	public function hasToBeInviteAble() {
0 ignored issues
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...
246
247
		if ($this->getLevel() > 0) {
248
			throw new MemberAlreadyExistsException(
249
				$this->l10n->t("User is already a member of this circle")
250
			);
251
		}
252
253
		if ($this->getStatus() === Member::STATUS_INVITED) {
254
			throw new MemberAlreadyExistsException(
255
				$this->l10n->t("User is already invited into this circle")
256
			);
257
		}
258
	}
259
260
}
261
262
263