Completed
Branch master (e3f4c4)
by Maxence
02:21
created

Member::cantBeOwner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
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\MemberIsNotModeratorException;
31
use OCA\Circles\Exceptions\MemberIsOwnerException;
32
33
class Member implements \JsonSerializable {
34
35
	const LEVEL_NONE = 0;
36
	const LEVEL_MEMBER = 1;
37
	const LEVEL_MODERATOR = 6;
38
	const LEVEL_ADMIN = 8;
39
	const LEVEL_OWNER = 9;
40
41
	const STATUS_NONMEMBER = 'Unknown';
42
	const STATUS_INVITED = 'Invited';
43
	const STATUS_REQUEST = 'Requesting';
44
	const STATUS_MEMBER = 'Member';
45
	const STATUS_BLOCKED = 'Blocked';
46
	const STATUS_KICKED = 'Kicked';
47
48
	private $circleid;
49
	private $userid;
50
	private $level;
51
	private $levelString;
52
	private $status;
53
	private $note;
54
	private $joined;
55
56
	public function __construct() {
57
	}
58
59
60
	public function setCircleId($circleid) {
61
		$this->circleid = (int)$circleid;
62
63
		return $this;
64
	}
65
66
	public function getCircleId() {
67
		return $this->circleid;
68
	}
69
70
71
	public function setUserId($userid) {
72
		$this->userid = $userid;
73
74
		return $this;
75
	}
76
77
	public function getUserId() {
78
		return $this->userid;
79
	}
80
81
82
	public function setLevel($level) {
83
		$this->level = (int)$level;
84
		$this->setLevelString(self::LevelSring($this->level));
85
86
		return $this;
87
	}
88
89
	public function getLevel() {
90
		return $this->level;
91
	}
92
93
94
	public function setLevelString($str) {
95
		$this->levelString = $str;
96
97
		return $this;
98
	}
99
100
	public function getLevelString() {
101
		return $this->levelString;
102
	}
103
104
105
	public function setNote($note) {
106
		$this->note = $note;
107
108
		return $this;
109
	}
110
111
	public function getNote() {
112
		return $this->note;
113
	}
114
115
116
	public function setStatus($status) {
117
		if (is_null($status)) {
118
			$this->status = self::STATUS_NONMEMBER;
119
		} else {
120
			$this->status = $status;
121
		}
122
123
		return $this;
124
	}
125
126
	public function getStatus() {
127
		return $this->status;
128
	}
129
130
131
	public function setJoined($joined) {
132
		$this->joined = $joined;
133
134
		return $this;
135
	}
136
137
	public function getJoined() {
138
		return $this->joined;
139
	}
140
141
	/**
142
	 * @throws MemberIsNotModeratorException
143
	 */
144
	public function hasToBeModerator() {
145
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
146
			throw new MemberIsNotModeratorException();
147
		}
148
	}
149
150
151
	/**
152
	 * @throws MemberIsOwnerException
153
	 */
154
	public function cantBeOwner() {
155
		if ($this->getLevel() === self::LEVEL_OWNER) {
156
			throw new MemberIsOwnerException();
157
		}
158
	}
159
160
	/**
161
	 * @param $member
162
	 *
163
	 * @throws MemberAlreadyExistsException
164
	 * @throws MemberIsBlockedException
165
	 */
166
	public function hasToBeAbleToJoinTheCircle() {
167
168
		if ($this->getLevel() > 0) {
169
			throw new MemberAlreadyExistsException("You are already a member of this circle");
170
		}
171
172
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
173
			throw new MemberIsBlockedException("You are blocked from this circle");
174
		}
175
	}
176
177
	public function jsonSerialize() {
178
		return array(
179
			'circleid'     => $this->getCircleId(),
180
			'userid'       => $this->getUserId(),
181
			'level'        => $this->getLevel(),
182
			'level_string' => $this->getLevelString(),
183
			'status'       => $this->getStatus(),
184
			'joined'       => $this->getJoined()
185
		);
186
	}
187
188
189
	public static function fromArray($arr) {
190
191
		if (!is_array($arr)) {
192
			return null;
193
		}
194
195
		$member = new Member();
196
197
		$member->setCircleId($arr['circle_id']);
198
		$member->setUserId($arr['user_id']);
199
		$member->setLevel($arr['level']);
200
		$member->setStatus($arr['status']);
201
		if (key_exists('note', $arr)) {
202
			$member->setNote($arr['note']);
203
		}
204
		if (key_exists('joined', $arr)) {
205
			$member->setJoined($arr['joined']);
206
		}
207
208
		return $member;
209
	}
210
211
212
	public static function LevelSring($level) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
213
		switch ($level) {
214
			case self::LEVEL_NONE:
215
				return 'Not a member';
216
			case self::LEVEL_MEMBER:
217
				return 'Member';
218
			case self::LEVEL_MODERATOR:
219
				return 'Moderator';
220
			case self::LEVEL_ADMIN:
221
				return 'Admin';
222
			case self::LEVEL_OWNER:
223
				return 'Owner';
224
		}
225
226
		return 'none';
227
	}
228
229
230
	public function toString() {
231
		return "toString ?";
232
	}
233
}
234
235
236