Completed
Push — master ( 809444...363a27 )
by Maxence
05:40 queued 02:26
created

Member::joinCircle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
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\MemberIsNotAdminException;
34
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
35
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
36
use OCA\Circles\Exceptions\MemberIsOwnerException;
37
use OCA\Circles\Exceptions\ModeratorIsNotHighEnoughException;
38
39
class Member extends BaseMember implements \JsonSerializable {
40
41
42
	public function inviteToCircle($circleType) {
43
		if ($circleType === Circle::CIRCLES_PRIVATE) {
44
			return $this->inviteIntoPrivateCircle();
45
		}
46
47
		return $this->addMemberToCircle();
48
	}
49
50
51
	/**
52
	 * @param int $circleType
53
	 *
54
	 * @throws MemberCantJoinCircle
55
	 */
56
	public function joinCircle($circleType) {
57
58
		switch ($circleType) {
59
			case Circle::CIRCLES_HIDDEN:
60
			case Circle::CIRCLES_PUBLIC:
61
				return $this->addMemberToCircle();
62
63
			case Circle::CIRCLES_PRIVATE:
64
				return $this->joinPrivateCircle();
65
		}
66
67
		throw new MemberCantJoinCircle($this->l10n->t('You cannot join this circle'));
68
	}
69
70
71
	/**
72
	 * Update status of member like he joined a public circle.
73
	 */
74
	private function addMemberToCircle() {
75
76
		if ($this->getStatus() === Member::STATUS_NONMEMBER
77
			|| $this->getStatus() === Member::STATUS_KICKED
78
		) {
79
			$this->setAsAMember(Member::LEVEL_MEMBER);
80
		}
81
	}
82
83
84
	/**
85
	 * Update status of member like he joined a private circle
86
	 * (invite/request)
87
	 */
88 View Code Duplication
	private function joinPrivateCircle() {
89
90
		switch ($this->getStatus()) {
91
			case Member::STATUS_NONMEMBER:
92
			case Member::STATUS_KICKED:
93
				$this->setStatus(Member::STATUS_REQUEST);
94
				break;
95
96
			case Member::STATUS_INVITED:
97
				$this->setAsAMember(Member::LEVEL_MEMBER);
98
				break;
99
		}
100
	}
101
102
103 View Code Duplication
	private function inviteIntoPrivateCircle() {
104
		switch ($this->getStatus()) {
105
			case Member::STATUS_NONMEMBER:
106
			case Member::STATUS_KICKED:
107
				$this->setStatus(Member::STATUS_INVITED);
108
				break;
109
110
			case Member::STATUS_REQUEST:
111
				$this->setAsAMember(Member::LEVEL_MEMBER);
112
				break;
113
		}
114
	}
115
116
117
	/**
118
	 * @throws MemberIsNotModeratorException
119
	 */
120
	public function hasToBeModerator() {
121
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
122
			throw new MemberIsNotModeratorException(
123
				$this->l10n->t('This member is not a moderator')
124
			);
125
		}
126
	}
127
128
129
	/**
130
	 * @param $level
131
	 *
132
	 * @throws ModeratorIsNotHighEnoughException
133
	 */
134
	public function hasToBeHigherLevel($level) {
135
136
		if ($this->getLevel() <= $level) {
137
			throw new ModeratorIsNotHighEnoughException(
138
				$this->l10n->t('Not enough privileges')
139
			);
140
		}
141
	}
142
143
144
	/**
145
	 * @throws MemberIsNotModeratorException
146
	 */
147
	public function hasToBeOwner() {
148
		if ($this->getLevel() < self::LEVEL_OWNER) {
149
			throw new MemberIsNotOwnerException(
150
				$this->l10n->t('This member is not the owner of the circle')
151
			);
152
		}
153
	}
154
155
156
	/**
157
	 * @throws MemberIsNotModeratorException
158
	 */
159
	public function hasToBeAdmin() {
160
		if ($this->getLevel() < self::LEVEL_OWNER) {
161
			throw new MemberIsNotAdminException(
162
				$this->l10n->t('This member is not admin of the circle')
163
			);
164
		}
165
	}
166
167
	/**
168
	 * @throws MemberDoesNotExistException
169
	 */
170
	public function hasToBeMember() {
171
		if ($this->getLevel() < self::LEVEL_MEMBER) {
172
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
173
		}
174
	}
175
176
177
	/**
178
	 * @throws MemberIsOwnerException
179
	 */
180
	public function cantBeOwner() {
181
		if ($this->getLevel() === self::LEVEL_OWNER) {
182
			throw new MemberIsOwnerException(
183
				$this->l10n->t('This member is the owner of the circle')
184
			);
185
		}
186
	}
187
188
	/**
189
	 * @throws MemberAlreadyExistsException
190
	 * @throws MemberIsBlockedException
191
	 */
192
	public function hasToBeAbleToJoinTheCircle() {
193
194
		if ($this->getLevel() > 0) {
195
			throw new MemberAlreadyExistsException(
196
				$this->l10n->t("You are already a member of this circle")
197
			);
198
		}
199
200
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
201
			throw new MemberIsBlockedException(
202
				$this->l10n->t("You have been blocked from this circle")
203
			);
204
		}
205
	}
206
207
	public function jsonSerialize() {
208
		return array(
209
			'circle_id'    => $this->getCircleId(),
210
			'user_id'      => $this->getUserId(),
211
			'level'        => $this->getLevel(),
212
			'level_string' => $this->getLevelString(),
213
			'status'       => $this->getStatus(),
214
			'note'         => $this->getNote(),
215
			'joined'       => $this->getJoined()
216
		);
217
	}
218
219
220
}
221
222
223