Completed
Push — federated-circles ( 915d64...66c054 )
by Maxence
03:31
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\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
38
class Member extends BaseMember implements \JsonSerializable {
39
40
41
	public function inviteToCircle($circleType) {
42
		if ($circleType === Circle::CIRCLES_PRIVATE) {
43
			return $this->inviteIntoPrivateCircle();
44
		}
45
46
		return $this->addMemberToCircle();
47
	}
48
49
50
	/**
51
	 * @param int $circleType
52
	 *
53
	 * @throws MemberCantJoinCircle
54
	 */
55
	public function joinCircle($circleType) {
56
57
		switch ($circleType) {
58
			case Circle::CIRCLES_HIDDEN:
59
			case Circle::CIRCLES_PUBLIC:
60
				return $this->addMemberToCircle();
61
62
			case Circle::CIRCLES_PRIVATE:
63
				return $this->joinPrivateCircle();
64
		}
65
66
		throw new MemberCantJoinCircle($this->l10n->t('You cannot join this circle'));
67
	}
68
69
70
	/**
71
	 * Update status of member like he joined a public circle.
72
	 */
73
	private function addMemberToCircle() {
74
75
		if ($this->getStatus() === Member::STATUS_NONMEMBER
76
			|| $this->getStatus() === Member::STATUS_KICKED
77
		) {
78
			$this->setAsAMember(Member::LEVEL_MEMBER);
79
		}
80
	}
81
82
83
	/**
84
	 * Update status of member like he joined a private circle
85
	 * (invite/request)
86
	 */
87 View Code Duplication
	private function joinPrivateCircle() {
88
89
		switch ($this->getStatus()) {
90
			case Member::STATUS_NONMEMBER:
91
			case Member::STATUS_KICKED:
92
				$this->setStatus(Member::STATUS_REQUEST);
93
				break;
94
95
			case Member::STATUS_INVITED:
96
				$this->setAsAMember(Member::LEVEL_MEMBER);
97
				break;
98
		}
99
	}
100
101
102 View Code Duplication
	private function inviteIntoPrivateCircle() {
103
		switch ($this->getStatus()) {
104
			case Member::STATUS_NONMEMBER:
105
			case Member::STATUS_KICKED:
106
				$this->setStatus(Member::STATUS_INVITED);
107
				break;
108
109
			case Member::STATUS_REQUEST:
110
				$this->setAsAMember(Member::LEVEL_MEMBER);
111
				break;
112
		}
113
	}
114
115
116
	/**
117
	 * @throws MemberIsNotModeratorException
118
	 */
119
	public function hasToBeModerator() {
120
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
121
			throw new MemberIsNotModeratorException(
122
				$this->l10n->t('This member is not a moderator')
123
			);
124
		}
125
	}
126
127
128
	/**
129
	 * @throws MemberIsNotModeratorException
130
	 */
131
	public function hasToBeOwner() {
132
		if ($this->getLevel() < self::LEVEL_OWNER) {
133
			throw new MemberIsNotOwnerException(
134
				$this->l10n->t('This member is not the owner of the circle')
135
			);
136
		}
137
	}
138
139
140
	/**
141
	 * @throws MemberIsNotModeratorException
142
	 */
143
	public function hasToBeAdmin() {
144
		if ($this->getLevel() < self::LEVEL_OWNER) {
145
			throw new MemberIsNotAdminException(
146
				$this->l10n->t('This member is not admin of the circle')
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
160
161
	/**
162
	 * @throws MemberIsOwnerException
163
	 */
164
	public function cantBeOwner() {
165
		if ($this->getLevel() === self::LEVEL_OWNER) {
166
			throw new MemberIsOwnerException(
167
				$this->l10n->t('This member is the owner of the circle')
168
			);
169
		}
170
	}
171
172
	/**
173
	 * @throws MemberAlreadyExistsException
174
	 * @throws MemberIsBlockedException
175
	 */
176
	public function hasToBeAbleToJoinTheCircle() {
177
178
		if ($this->getLevel() > 0) {
179
			throw new MemberAlreadyExistsException(
180
				$this->l10n->t("You are already a member of this circle")
181
			);
182
		}
183
184
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
185
			throw new MemberIsBlockedException(
186
				$this->l10n->t("You have been blocked from this circle")
187
			);
188
		}
189
	}
190
191
	public function jsonSerialize() {
192
		return array(
193
			'circle_id'    => $this->getCircleId(),
194
			'user_id'      => $this->getUserId(),
195
			'level'        => $this->getLevel(),
196
			'level_string' => $this->getLevelString(),
197
			'status'       => $this->getStatus(),
198
			'note'         => $this->getNote(),
199
			'joined'       => $this->getJoined()
200
		);
201
	}
202
203
204
}
205
206
207