Completed
Push — master ( 97a159...fc6227 )
by Maxence
29s
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\MemberTypeCantEditLevelException;
39
use OCA\Circles\Exceptions\ModeratorIsNotHighEnoughException;
40
41
class Member extends BaseMember {
42
43
44
	public function inviteToCircle($circleType) {
45
46
		if ($circleType === 0) {
47
			throw new CircleTypeNotValidException('Circle Type is not valid');
48
		}
49
50
		if ($circleType === Circle::CIRCLES_CLOSED) {
51
			return $this->inviteIntoClosedCircle();
52
		}
53
54
		return $this->addMemberToCircle();
55
	}
56
57
58
	/**
59
	 * @param int $circleType
60
	 *
61
	 * @throws MemberCantJoinCircleException
62
	 */
63
	public function joinCircle($circleType) {
64
65
		switch ($circleType) {
66
			case Circle::CIRCLES_SECRET:
67
			case Circle::CIRCLES_PUBLIC:
68
				return $this->addMemberToCircle();
69
70
			case Circle::CIRCLES_CLOSED:
71
				return $this->joinClosedCircle();
72
		}
73
74
		throw new MemberCantJoinCircleException($this->l10n->t('You cannot join this circle'));
75
	}
76
77
78
	/**
79
	 * Update status of member like he joined a public circle.
80
	 */
81
	public function addMemberToCircle() {
82
83
		if ($this->getStatus() === Member::STATUS_NONMEMBER
84
			|| $this->getStatus() === Member::STATUS_KICKED
85
		) {
86
			$this->setAsAMember(Member::LEVEL_MEMBER);
87
		}
88
	}
89
90
91
	/**
92
	 * Update status of member like he joined a closed circle
93
	 * (invite/request)
94
	 */
95 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...
96
97
		switch ($this->getStatus()) {
98
			case Member::STATUS_NONMEMBER:
99
			case Member::STATUS_KICKED:
100
				$this->setStatus(Member::STATUS_REQUEST);
101
				break;
102
103
			case Member::STATUS_INVITED:
104
				$this->setAsAMember(Member::LEVEL_MEMBER);
105
				break;
106
		}
107
	}
108
109
110 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...
111
		switch ($this->getStatus()) {
112
			case Member::STATUS_NONMEMBER:
113
			case Member::STATUS_KICKED:
114
				$this->setStatus(Member::STATUS_INVITED);
115
				break;
116
117
			case Member::STATUS_REQUEST:
118
				$this->setAsAMember(Member::LEVEL_MEMBER);
119
				break;
120
		}
121
	}
122
123
124
	/**
125
	 * @throws MemberIsNotModeratorException
126
	 */
127
	public function hasToBeModerator() {
128
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
129
			throw new MemberIsNotModeratorException(
130
				$this->l10n->t('This member is not a moderator')
131
			);
132
		}
133
	}
134
135
136
	/**
137
	 * @param $level
138
	 *
139
	 * @throws ModeratorIsNotHighEnoughException
140
	 */
141
	public function hasToBeHigherLevel($level) {
142
143
		if ($this->getLevel() <= $level) {
144
			throw new ModeratorIsNotHighEnoughException(
145
				$this->l10n->t('Not enough privileges')
146
			);
147
		}
148
	}
149
150
151
	/**
152
	 * @throws MemberIsNotModeratorException
153
	 */
154
	public function hasToBeOwner() {
155
		if ($this->getLevel() < self::LEVEL_OWNER) {
156
			throw new MemberIsNotOwnerException(
157
				$this->l10n->t('This member is not the owner of the circle')
158
			);
159
		}
160
	}
161
162
163
	/**
164
	 * @throws MemberIsNotModeratorException
165
	 */
166
	public function hasToBeAdmin() {
167
		if ($this->getLevel() < self::LEVEL_ADMIN) {
168
			throw new MemberIsNotAdminException(
169
				$this->l10n->t('This member is not admin of the circle')
170
			);
171
		}
172
	}
173
174
	/**
175
	 * @throws MemberDoesNotExistException
176
	 */
177
	public function hasToBeMember() {
178
		if ($this->getLevel() < self::LEVEL_MEMBER) {
179
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
180
		}
181
182
		return true;
183
	}
184
185
186
	/**
187
	 * @throws MemberDoesNotExistException
188
	 */
189
	public function hasToBeMemberOrAlmost() {
190
		if ($this->isAlmostMember() || $this->hasToBeMember()) {
191
			return true;
192
		}
193
194
		throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
195
	}
196
197
198
	/**
199
	 * @throws MemberIsOwnerException
200
	 */
201
	public function cantBeOwner() {
202
		if ($this->getLevel() === self::LEVEL_OWNER) {
203
			throw new MemberIsOwnerException(
204
				$this->l10n->t('This member is the owner of the circle')
205
			);
206
		}
207
	}
208
209
210
	/**
211
	 * return if member already exists
212
	 *
213
	 * @return bool
214
	 */
215
	public function alreadyExistOrJoining() {
216
		return ($this->getLevel() > Member::LEVEL_NONE
217
				|| ($this->getStatus() !== Member::STATUS_NONMEMBER
218
					&& $this->getStatus() !== Member::STATUS_REQUEST)
219
		);
220
	}
221
222
223
	/**
224
	 * @param bool $able
225
	 */
226
	public function broadcasting($able) {
227
		$this->broadcasting = $able;
228
	}
229
230
231
	/**
232
	 * @return bool
233
	 */
234
	public function isBroadcasting() {
235
		return $this->broadcasting;
236
	}
237
238
239
	/**
240
	 * @throws MemberTypeCantEditLevelException
241
	 */
242
	public function levelHasToBeEditable() {
243
		if ($this->getType() !== self::TYPE_USER) {
244
			throw new MemberTypeCantEditLevelException(
245
				$this->l10n->t('Level cannot be changed for that type of member')
246
			);
247
		}
248
	}
249
250
251
	/**
252
	 * @throws MemberAlreadyExistsException
253
	 * @throws MemberIsBlockedException
254
	 */
255 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...
256
257
		if ($this->getLevel() > 0) {
258
			throw new MemberAlreadyExistsException(
259
				$this->l10n->t("You are already a member of this circle")
260
			);
261
		}
262
263
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
264
			throw new MemberIsBlockedException(
265
				$this->l10n->t("You have been blocked from this circle")
266
			);
267
		}
268
	}
269
270
271
	/**
272
	 * @throws MemberAlreadyExistsException
273
	 */
274 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...
275
276
		if ($this->getLevel() > 0) {
277
			throw new MemberAlreadyExistsException(
278
				$this->l10n->t("User is already a member of this circle")
279
			);
280
		}
281
282
		if ($this->getStatus() === Member::STATUS_INVITED) {
283
			throw new MemberAlreadyExistsException(
284
				$this->l10n->t("User is already invited into this circle")
285
			);
286
		}
287
	}
288
289
}
290
291
292