Completed
Push — some-scrutinizing ( 6d8498...b2f84c )
by Maxence
15:17
created

Member::fromArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 9.0534
c 2
b 0
f 0
cc 4
eloc 13
nc 5
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\MemberCantJoinPersonalCircle;
31
use OCA\Circles\Exceptions\MemberDoesNotExistException;
32
use OCA\Circles\Exceptions\MemberIsBlockedException;
33
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
34
use OCA\Circles\Exceptions\MemberIsOwnerException;
35
36
class Member extends BaseMember implements \JsonSerializable {
37
38
	private $levelString;
39
40
	public function setLevel($level) {
41
		parent::setLevel($level);
42
		$this->setLevelString(self::levelString($this->getLevel()));
43
44
		return $this;
45
	}
46
47
	public function setLevelString($str) {
48
		$this->levelString = $str;
49
50
		return $this;
51
	}
52
53
	public function getLevelString() {
54
		return $this->levelString;
55
	}
56
57
58
	/**
59
	 * @param int $circleType
60
	 *
61
	 * @throws MemberCantJoinPersonalCircle
62
	 */
63
	public function joinCircle($circleType) {
64
65
		switch ($circleType) {
66
			case Circle::CIRCLES_HIDDEN:
67
			case Circle::CIRCLES_PUBLIC:
68
				$this->joinOpenCircle();
69
				break;
70
71
			case Circle::CIRCLES_PRIVATE:
72
				$this->joinPrivateCircle();
73
				break;
74
75
			case Circle::CIRCLES_PERSONAL:
76
				throw new MemberCantJoinPersonalCircle();
77
		}
78
	}
79
80
	/**
81
	 * Update status of member like he joined a public circle.
82
	 */
83
	private function joinOpenCircle() {
84
85
		if ($this->getStatus() === Member::STATUS_NONMEMBER
86
			|| $this->getStatus() === Member::STATUS_KICKED
87
		) {
88
			$this->setStatus(Member::STATUS_MEMBER);
89
			$this->setLevel(Member::LEVEL_MEMBER);
90
		}
91
	}
92
93
	/**
94
	 * Update status of member like he joined a private circle
95
	 * (invite/request)
96
	 */
97
	private function joinPrivateCircle() {
98
99
		switch ($this->getStatus()) {
100
			case Member::STATUS_NONMEMBER:
101
			case Member::STATUS_KICKED:
102
				$this->setStatus(Member::STATUS_REQUEST);
103
				break;
104
105
			case Member::STATUS_INVITED:
106
				$this->setStatus(Member::STATUS_MEMBER);
107
				$this->setLevel(Member::LEVEL_MEMBER);
108
				break;
109
		}
110
	}
111
112
113
	public function isModerator() {
114
		return ($this->getLevel() >= self::LEVEL_MODERATOR);
115
	}
116
117
	/**
118
	 * @throws MemberIsNotModeratorException
119
	 */
120
	public function hasToBeModerator() {
121
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
122
			throw new MemberIsNotModeratorException();
123
		}
124
	}
125
126
127
	/**
128
	 * @throws MemberDoesNotExistException
129
	 */
130
	public function hasToBeMember() {
131
		if ($this->getLevel() < self::LEVEL_MEMBER) {
132
			throw new MemberDoesNotExistException();
133
		}
134
	}
135
136
137
	/**
138
	 * @throws MemberIsOwnerException
139
	 */
140
	public function cantBeOwner() {
141
		if ($this->getLevel() === self::LEVEL_OWNER) {
142
			throw new MemberIsOwnerException();
143
		}
144
	}
145
146
	/**
147
	 * @param $member
148
	 *
149
	 * @throws MemberAlreadyExistsException
150
	 * @throws MemberIsBlockedException
151
	 */
152
	public function hasToBeAbleToJoinTheCircle() {
153
154
		if ($this->getLevel() > 0) {
155
			throw new MemberAlreadyExistsException("You are already a member of this circle");
156
		}
157
158
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
159
			throw new MemberIsBlockedException("You are blocked from this circle");
160
		}
161
	}
162
163
	public function jsonSerialize() {
164
		return array(
165
			'circleid'     => $this->getCircleId(),
166
			'userid'       => $this->getUserId(),
167
			'level'        => $this->getLevel(),
168
			'level_string' => $this->getLevelString(),
169
			'status'       => $this->getStatus(),
170
			'joined'       => $this->getJoined()
171
		);
172
	}
173
174
175
	public static function levelString($level) {
176
		switch ($level) {
177
			case self::LEVEL_NONE:
178
				return 'Not a member';
179
			case self::LEVEL_MEMBER:
180
				return 'Member';
181
			case self::LEVEL_MODERATOR:
182
				return 'Moderator';
183
			case self::LEVEL_ADMIN:
184
				return 'Admin';
185
			case self::LEVEL_OWNER:
186
				return 'Owner';
187
		}
188
189
		return 'none';
190
	}
191
192
}
193
194
195