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\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
|
|
|
public function inviteToCircle($circleType) { |
59
|
|
|
if ($circleType === Circle::CIRCLES_PRIVATE) { |
60
|
|
|
return $this->inviteIntoPrivateCircle(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->addMemberToCircle(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $circleType |
69
|
|
|
* |
70
|
|
|
* @throws MemberCantJoinCircle |
71
|
|
|
*/ |
72
|
|
|
public function joinCircle($circleType) { |
73
|
|
|
|
74
|
|
|
switch ($circleType) { |
75
|
|
|
case Circle::CIRCLES_HIDDEN: |
76
|
|
|
case Circle::CIRCLES_PUBLIC: |
77
|
|
|
return $this->addMemberToCircle(); |
78
|
|
|
|
79
|
|
|
case Circle::CIRCLES_PRIVATE: |
80
|
|
|
return $this->joinPrivateCircle(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new MemberCantJoinCircle(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Update status of member like he joined a public circle. |
89
|
|
|
*/ |
90
|
|
|
private function addMemberToCircle() { |
91
|
|
|
|
92
|
|
|
if ($this->getStatus() === Member::STATUS_NONMEMBER |
93
|
|
|
|| $this->getStatus() === Member::STATUS_KICKED |
94
|
|
|
) { |
95
|
|
|
$this->setAsAMember(Member::LEVEL_MEMBER); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
private function setAsAMember($level = 1) { |
101
|
|
|
$this->setStatus(Member::STATUS_MEMBER); |
102
|
|
|
$this->setLevel($level); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Update status of member like he joined a private circle |
108
|
|
|
* (invite/request) |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
private function joinPrivateCircle() { |
|
|
|
|
111
|
|
|
|
112
|
|
|
switch ($this->getStatus()) { |
113
|
|
|
case Member::STATUS_NONMEMBER: |
114
|
|
|
case Member::STATUS_KICKED: |
115
|
|
|
$this->setStatus(Member::STATUS_REQUEST); |
116
|
|
|
break; |
117
|
|
|
|
118
|
|
|
case Member::STATUS_INVITED: |
119
|
|
|
$this->setAsAMember(Member::LEVEL_MEMBER); |
120
|
|
|
break; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
View Code Duplication |
private function inviteIntoPrivateCircle() { |
|
|
|
|
126
|
|
|
switch ($this->getStatus()) { |
127
|
|
|
case Member::STATUS_NONMEMBER: |
128
|
|
|
case Member::STATUS_KICKED: |
129
|
|
|
$this->setStatus(Member::STATUS_INVITED); |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
case Member::STATUS_REQUEST: |
133
|
|
|
$this->setAsAMember(Member::LEVEL_MEMBER); |
134
|
|
|
break; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function isMember() { |
139
|
|
|
return ($this->getLevel() >= self::LEVEL_MEMBER); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isModerator() { |
143
|
|
|
return ($this->getLevel() >= self::LEVEL_MODERATOR); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @throws MemberIsNotModeratorException |
148
|
|
|
*/ |
149
|
|
|
public function hasToBeModerator() { |
150
|
|
|
if ($this->getLevel() < self::LEVEL_MODERATOR) { |
151
|
|
|
throw new MemberIsNotModeratorException(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @throws MemberDoesNotExistException |
158
|
|
|
*/ |
159
|
|
|
public function hasToBeMember() { |
160
|
|
|
if ($this->getLevel() < self::LEVEL_MEMBER) { |
161
|
|
|
throw new MemberDoesNotExistException(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @throws MemberIsOwnerException |
168
|
|
|
*/ |
169
|
|
|
public function cantBeOwner() { |
170
|
|
|
if ($this->getLevel() === self::LEVEL_OWNER) { |
171
|
|
|
throw new MemberIsOwnerException(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $member |
177
|
|
|
* |
178
|
|
|
* @throws MemberAlreadyExistsException |
179
|
|
|
* @throws MemberIsBlockedException |
180
|
|
|
*/ |
181
|
|
|
public function hasToBeAbleToJoinTheCircle() { |
182
|
|
|
|
183
|
|
|
if ($this->getLevel() > 0) { |
184
|
|
|
throw new MemberAlreadyExistsException("You are already a member of this circle"); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($this->getStatus() === Member::STATUS_BLOCKED) { |
188
|
|
|
throw new MemberIsBlockedException("You are blocked from this circle"); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function jsonSerialize() { |
193
|
|
|
return array( |
194
|
|
|
'circle_id' => $this->getCircleId(), |
195
|
|
|
'user_id' => $this->getUserId(), |
196
|
|
|
'level' => $this->getLevel(), |
197
|
|
|
'level_string' => $this->getLevelString(), |
198
|
|
|
'status' => $this->getStatus(), |
199
|
|
|
'joined' => $this->getJoined() |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
|
204
|
|
|
public static function levelString($level) { |
205
|
|
|
switch ($level) { |
206
|
|
|
case self::LEVEL_NONE: |
207
|
|
|
return 'Not a member'; |
208
|
|
|
case self::LEVEL_MEMBER: |
209
|
|
|
return 'Member'; |
210
|
|
|
case self::LEVEL_MODERATOR: |
211
|
|
|
return 'Moderator'; |
212
|
|
|
case self::LEVEL_ADMIN: |
213
|
|
|
return 'Admin'; |
214
|
|
|
case self::LEVEL_OWNER: |
215
|
|
|
return 'Owner'; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return 'none'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
|
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.