Completed
Push — some-scrutinizing ( d3de44...c6cac2 )
by Maxence
02:32
created

Member   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 139
Duplicated Lines 17.99 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 27
c 6
b 0
f 0
lcom 1
cbo 7
dl 25
loc 139
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A inviteToCircle() 0 7 2
A joinCircle() 0 13 4
A addMemberToCircle() 0 8 3
A joinPrivateCircle() 13 13 4
A inviteIntoPrivateCircle() 12 12 4
A hasToBeModerator() 0 5 2
A hasToBeMember() 0 5 2
A cantBeOwner() 0 5 2
A hasToBeAbleToJoinTheCircle() 0 10 3
A jsonSerialize() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
39
	public function inviteToCircle($circleType) {
40
		if ($circleType === Circle::CIRCLES_PRIVATE) {
41
			return $this->inviteIntoPrivateCircle();
42
		}
43
44
		return $this->addMemberToCircle();
45
	}
46
47
48
	/**
49
	 * @param int $circleType
50
	 *
51
	 * @throws MemberCantJoinCircle
52
	 */
53
	public function joinCircle($circleType) {
54
55
		switch ($circleType) {
56
			case Circle::CIRCLES_HIDDEN:
57
			case Circle::CIRCLES_PUBLIC:
58
				return $this->addMemberToCircle();
59
60
			case Circle::CIRCLES_PRIVATE:
61
				return $this->joinPrivateCircle();
62
		}
63
64
		throw new MemberCantJoinCircle();
65
	}
66
67
68
	/**
69
	 * Update status of member like he joined a public circle.
70
	 */
71
	private function addMemberToCircle() {
72
73
		if ($this->getStatus() === Member::STATUS_NONMEMBER
74
			|| $this->getStatus() === Member::STATUS_KICKED
75
		) {
76
			$this->setAsAMember(Member::LEVEL_MEMBER);
77
		}
78
	}
79
80
81
	/**
82
	 * Update status of member like he joined a private circle
83
	 * (invite/request)
84
	 */
85 View Code Duplication
	private function joinPrivateCircle() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
86
87
		switch ($this->getStatus()) {
88
			case Member::STATUS_NONMEMBER:
89
			case Member::STATUS_KICKED:
90
				$this->setStatus(Member::STATUS_REQUEST);
91
				break;
92
93
			case Member::STATUS_INVITED:
94
				$this->setAsAMember(Member::LEVEL_MEMBER);
95
				break;
96
		}
97
	}
98
99
100 View Code Duplication
	private function inviteIntoPrivateCircle() {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
		switch ($this->getStatus()) {
102
			case Member::STATUS_NONMEMBER:
103
			case Member::STATUS_KICKED:
104
				$this->setStatus(Member::STATUS_INVITED);
105
				break;
106
107
			case Member::STATUS_REQUEST:
108
				$this->setAsAMember(Member::LEVEL_MEMBER);
109
				break;
110
		}
111
	}
112
113
114
115
	/**
116
	 * @throws MemberIsNotModeratorException
117
	 */
118
	public function hasToBeModerator() {
119
		if ($this->getLevel() < self::LEVEL_MODERATOR) {
120
			throw new MemberIsNotModeratorException();
121
		}
122
	}
123
124
125
	/**
126
	 * @throws MemberDoesNotExistException
127
	 */
128
	public function hasToBeMember() {
129
		if ($this->getLevel() < self::LEVEL_MEMBER) {
130
			throw new MemberDoesNotExistException();
131
		}
132
	}
133
134
135
	/**
136
	 * @throws MemberIsOwnerException
137
	 */
138
	public function cantBeOwner() {
139
		if ($this->getLevel() === self::LEVEL_OWNER) {
140
			throw new MemberIsOwnerException();
141
		}
142
	}
143
144
	/**
145
	 * @param $member
146
	 *
147
	 * @throws MemberAlreadyExistsException
148
	 * @throws MemberIsBlockedException
149
	 */
150
	public function hasToBeAbleToJoinTheCircle() {
151
152
		if ($this->getLevel() > 0) {
153
			throw new MemberAlreadyExistsException("You are already a member of this circle");
154
		}
155
156
		if ($this->getStatus() === Member::STATUS_BLOCKED) {
157
			throw new MemberIsBlockedException("You are blocked from this circle");
158
		}
159
	}
160
161
	public function jsonSerialize() {
162
		return array(
163
			'circle_id'    => $this->getCircleId(),
164
			'user_id'      => $this->getUserId(),
165
			'level'        => $this->getLevel(),
166
			'level_string' => $this->getLevelString(),
167
			'status'       => $this->getStatus(),
168
			'joined'       => $this->getJoined()
169
		);
170
	}
171
172
173
174
}
175
176
177