Completed
Pull Request — master (#551)
by Maxence
02:38
created

DeprecatedMember::joinClosedCircle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
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 DeprecatedMember 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 === DeprecatedCircle::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 DeprecatedCircle::CIRCLES_SECRET:
67
			case DeprecatedCircle::CIRCLES_PUBLIC:
68
				return $this->addMemberToCircle();
69
70
			case DeprecatedCircle::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() === DeprecatedMember::STATUS_NONMEMBER
84
			|| $this->getStatus() === DeprecatedMember::STATUS_KICKED
85
		) {
86
			$this->setAsAMember(DeprecatedMember::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 DeprecatedMember::STATUS_NONMEMBER:
99
			case DeprecatedMember::STATUS_KICKED:
100
				$this->setStatus(DeprecatedMember::STATUS_REQUEST);
101
				break;
102
103
			case DeprecatedMember::STATUS_INVITED:
104
				$this->setAsAMember(DeprecatedMember::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 DeprecatedMember::STATUS_NONMEMBER:
113
			case DeprecatedMember::STATUS_KICKED:
114
				$this->setStatus(DeprecatedMember::STATUS_INVITED);
115
				break;
116
117
			case DeprecatedMember::STATUS_REQUEST:
118
				$this->setAsAMember(DeprecatedMember::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
		if ($this->getLevel() <= $level) {
143
			throw new ModeratorIsNotHighEnoughException(
144
				$this->l10n->t('Insufficient privileges')
145
			);
146
		}
147
	}
148
149
150
	/**
151
	 * @throws MemberDoesNotExistException
152
	 */
153
	public function hasToBeMember() {
154
		if ($this->getLevel() < self::LEVEL_MEMBER) {
155
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\MemberDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
156
		}
157
158
		return true;
159
	}
160
161
162
	/**
163
	 * @throws MemberDoesNotExistException
164
	 */
165
	public function hasToBeMemberOrAlmost() {
166
		if ($this->isAlmostMember() || $this->hasToBeMember()) {
167
			return true;
168
		}
169
170
		throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
0 ignored issues
show
Deprecated Code introduced by
The class OCA\Circles\Exceptions\MemberDoesNotExistException has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
171
	}
172
173
174
	/**
175
	 * @throws MemberIsOwnerException
176
	 */
177
	public function cantBeOwner() {
178
		if ($this->getLevel() === self::LEVEL_OWNER) {
179
			throw new MemberIsOwnerException(
180
				$this->l10n->t('This member is the owner of the circle')
181
			);
182
		}
183
	}
184
185
186
	/**
187
	 * return if member already exists
188
	 *
189
	 * @return bool
190
	 */
191
	public function alreadyExistOrJoining() {
192
		return ($this->getLevel() > DeprecatedMember::LEVEL_NONE
193
				|| ($this->getStatus() !== DeprecatedMember::STATUS_NONMEMBER
194
					&& $this->getStatus() !== DeprecatedMember::STATUS_REQUEST)
195
		);
196
	}
197
198
199
	/**
200
	 * @param bool $able
201
	 */
202
	public function broadcasting($able) {
203
		$this->broadcasting = $able;
204
	}
205
206
207
	/**
208
	 * @return bool
209
	 */
210
	public function isBroadcasting() {
211
		return $this->broadcasting;
212
	}
213
214
215
	/**
216
	 * @throws MemberTypeCantEditLevelException
217
	 */
218
	public function levelHasToBeEditable() {
219
		if ($this->getType() !== self::TYPE_USER) {
220
			throw new MemberTypeCantEditLevelException(
221
				$this->l10n->t('Level cannot be changed for this type of member')
222
			);
223
		}
224
	}
225
226
227
	/**
228
	 * @throws MemberAlreadyExistsException
229
	 * @throws MemberIsBlockedException
230
	 */
231 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...
232
233
		if ($this->getLevel() > 0) {
234
			throw new MemberAlreadyExistsException(
235
				$this->l10n->t("You are already a member of this circle")
236
			);
237
		}
238
239
		if ($this->getStatus() === DeprecatedMember::STATUS_BLOCKED) {
240
			throw new MemberIsBlockedException(
241
				$this->l10n->t("You have been blocked from this circle")
242
			);
243
		}
244
	}
245
246
247
	/**
248
	 * @throws MemberAlreadyExistsException
249
	 */
250 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...
251
252
		if ($this->getLevel() > 0) {
253
			throw new MemberAlreadyExistsException(
254
				$this->l10n->t("The user is already a member of this circle")
255
			);
256
		}
257
258
		if ($this->getStatus() === DeprecatedMember::STATUS_INVITED) {
259
			throw new MemberAlreadyExistsException(
260
				$this->l10n->t("The user has already been invited into this circle")
261
			);
262
		}
263
	}
264
265
}
266
267
268