Completed
Push — master ( 9d9a73...7c98ce )
by Maxence
03:24
created

Member::hasToBeAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
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('Invalid circle type');
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('Insufficient privileges')
146
			);
147
		}
148
	}
149
150
151
	/**
152
	 * @throws MemberDoesNotExistException
153
	 */
154
	public function hasToBeMember() {
155
		if ($this->getLevel() < self::LEVEL_MEMBER) {
156
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
157
		}
158
159
		return true;
160
	}
161
162
163
	/**
164
	 * @throws MemberDoesNotExistException
165
	 */
166
	public function hasToBeMemberOrAlmost() {
167
		if ($this->isAlmostMember() || $this->hasToBeMember()) {
168
			return true;
169
		}
170
171
		throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
172
	}
173
174
175
	/**
176
	 * @throws MemberIsOwnerException
177
	 */
178
	public function cantBeOwner() {
179
		if ($this->getLevel() === self::LEVEL_OWNER) {
180
			throw new MemberIsOwnerException(
181
				$this->l10n->t('This member is the owner of the circle')
182
			);
183
		}
184
	}
185
186
187
	/**
188
	 * return if member already exists
189
	 *
190
	 * @return bool
191
	 */
192
	public function alreadyExistOrJoining() {
193
		return ($this->getLevel() > Member::LEVEL_NONE
194
				|| ($this->getStatus() !== Member::STATUS_NONMEMBER
195
					&& $this->getStatus() !== Member::STATUS_REQUEST)
196
		);
197
	}
198
199
200
	/**
201
	 * @param bool $able
202
	 */
203
	public function broadcasting($able) {
204
		$this->broadcasting = $able;
205
	}
206
207
208
	/**
209
	 * @return bool
210
	 */
211
	public function isBroadcasting() {
212
		return $this->broadcasting;
213
	}
214
215
216
	/**
217
	 * @throws MemberTypeCantEditLevelException
218
	 */
219
	public function levelHasToBeEditable() {
220
		if ($this->getType() !== self::TYPE_USER) {
221
			throw new MemberTypeCantEditLevelException(
222
				$this->l10n->t('Level cannot be changed for this type of member')
223
			);
224
		}
225
	}
226
227
228
	/**
229
	 * @throws MemberAlreadyExistsException
230
	 * @throws MemberIsBlockedException
231
	 */
232 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...
233
234
		if ($this->getLevel() > 0) {
235
			throw new MemberAlreadyExistsException(
236
				$this->l10n->t("You are already a member of this circle")
237
			);
238
		}
239
240
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
241
			throw new MemberIsBlockedException(
242
				$this->l10n->t("You have been blocked from this circle")
243
			);
244
		}
245
	}
246
247
248
	/**
249
	 * @throws MemberAlreadyExistsException
250
	 */
251 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...
252
253
		if ($this->getLevel() > 0) {
254
			throw new MemberAlreadyExistsException(
255
				$this->l10n->t("The user is already a member of this circle")
256
			);
257
		}
258
259
		if ($this->getStatus() === Member::STATUS_INVITED) {
260
			throw new MemberAlreadyExistsException(
261
				$this->l10n->t("The user has already been invited into this circle")
262
			);
263
		}
264
	}
265
266
}
267
268
269