Completed
Push — master ( 397c2e...5232e6 )
by
unknown
02:26
created

MembersService::addMember()   C

Complexity

Conditions 14
Paths 19

Size

Total Lines 79
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 79
rs 5.1058
c 1
b 0
f 0
cc 14
eloc 55
nc 19
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Circles - bring cloud-users closer
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\Service;
28
29
30
use OC\User\NoUserException;
31
use OCA\Circles\Exceptions\CircleDoesNotExistException;
32
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
35
use OCA\Circles\Exceptions\MemberIsOwnerException;
36
use \OCA\Circles\Model\Circle;
37
use \OCA\Circles\Model\Member;
38
use OCP\IL10N;
39
use OCP\IUserManager;
40
41
class MembersService {
42
43
	private $userId;
44
	private $l10n;
45
	private $configService;
46
	private $databaseService;
47
	private $miscService;
48
49 View Code Duplication
	public function __construct(
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...
50
		$userId,
51
		IL10N $l10n,
52
		IUserManager $userManager,
53
		ConfigService $configService,
54
		DatabaseService $databaseService,
55
		MiscService $miscService
56
	) {
57
		$this->userId = $userId;
58
		$this->l10n = $l10n;
59
		$this->userManager = $userManager;
0 ignored issues
show
Bug introduced by
The property userManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
		$this->configService = $configService;
61
		$this->databaseService = $databaseService;
62
		$this->miscService = $miscService;
63
	}
64
65
66
//	public function searchMembers($name) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
//		$iError = new iError();
68
//
69
//		$result = $this->userManager->get($name);
70
//		$this->miscService->log("___" . var_export($result, true));
71
////		if ($user != null) {
72
////
73
////			$realname = $user->getDisplayName();
74
//
75
//		$result = [
76
//			'name'   => $name,
77
//			'result' => $result,
78
//			'status' => 1,
79
//			'error'  => $iError->toArray()
80
//		];
81
//
82
//		return $result;
83
//	}
84
85
	/**
86
	 * @param $circleId
87
	 * @param $name
88
	 *
89
	 * @return array
90
	 * @throws CircleDoesNotExistException
91
	 * @throws MemberAlreadyExistsException
92
	 * @throws MemberDoesNotExistException
93
	 * @throws MemberIsNotModeratorException
94
	 * @throws NoUserException
95
	 */
96
	public function addMember($circleId, $name) {
97
98
		if (!$this->userManager->userExists($name)) {
99
			throw new NoUserException("The selected user does not exist");
100
		}
101
102
		try {
103
			$ismod = $this->databaseService->getMembersMapper()
104
										   ->getMemberFromCircle($circleId, $this->userId);
105
		} catch (MemberDoesNotExistException $e) {
106
			throw $e;
107
		}
108
109
		if ($ismod->getLevel() < Member::LEVEL_MODERATOR) {
110
			throw new MemberIsNotModeratorException("You are not moderator of this circle");
111
		}
112
113
		try {
114
			$member = $this->databaseService->getMembersMapper()
115
											->getMemberFromCircle($circleId, $name);
116
		} catch (MemberDoesNotExistException $e) {
117
			$member = new Member();
118
			$member->setCircleId($circleId);
119
			$member->setUserId($name);
120
			$member->setLevel(Member::LEVEL_NONE);
121
			$member->setStatus(Member::STATUS_NONMEMBER);
122
123
			$this->databaseService->getMembersMapper()
124
								  ->add(
125
									  $member
126
								  );
127
		}
128
129
		try {
130
			$circle = $this->databaseService->getCirclesMapper()
131
											->getDetailsFromCircle($this->userId, $circleId);
132
		} catch (CircleDoesNotExistException $e) {
133
			throw $e;
134
		}
135
136
		if ($member->getLevel() > Member::LEVEL_NONE
137
			|| ($member->getStatus() !== Member::STATUS_NONMEMBER
138
				&& $member->getStatus() !== Member::STATUS_REQUEST)
139
		) {
140
			throw new MemberAlreadyExistsException();
141
		}
142
143
		$member->setCircleId($circleId);
144
		$member->setUserId($name);
145
146
		switch ($circle->getType()) {
147
			case Circle::CIRCLES_PERSONAL:
148
			case Circle::CIRCLES_HIDDEN:
149
			case Circle::CIRCLES_PUBLIC:
150
				$member->setLevel(Member::LEVEL_MEMBER);
151
				$member->setStatus(Member::STATUS_MEMBER);
152
				break;
153
154
			case Circle::CIRCLES_PRIVATE:
155
				if ($member->getStatus() === Member::STATUS_REQUEST) {
156
					$member->setLevel(Member::LEVEL_MEMBER);
157
					$member->setStatus(Member::STATUS_MEMBER);
158
				} else {
159
					$member->setLevel(Member::LEVEL_NONE);
160
					$member->setStatus(Member::STATUS_INVITED);
161
				}
162
				break;
163
		}
164
165
		$this->databaseService->getMembersMapper()
166
							  ->editMember($member);
167
168
		return $this->databaseService->getMembersMapper()
169
									 ->getMembersFromCircle(
170
										 $circleId, ($circle->getUser()
171
															->getLevel()
172
													 >= Member::LEVEL_MODERATOR)
173
									 );
174
	}
175
176
	/**
177
	 * @param $circleId
178
	 * @param $name
179
	 *
180
	 * @return array
181
	 * @throws MemberDoesNotExistException
182
	 * @throws MemberIsNotModeratorException
183
	 * @throws MemberIsOwnerException
184
	 */
185
	public function removeMember($circleId, $name) {
186
187
		try {
188
			$ismod = $this->databaseService->getMembersMapper()
189
										   ->getMemberFromCircle($circleId, $this->userId);
190
		} catch (MemberDoesNotExistException $e) {
191
			throw $e;
192
		}
193
194
195
		if ($ismod->getLevel() < Member::LEVEL_MODERATOR) {
196
			throw new MemberIsNotModeratorException("You are not moderator of this circle");
197
		}
198
199
		try {
200
			$member = $this->databaseService->getMembersMapper()
201
											->getMemberFromCircle($circleId, $name);
202
		} catch (MemberDoesNotExistException $e) {
203
			throw $e;
204
		}
205
206
207
		if ($member->getLevel() === Member::LEVEL_OWNER) {
208
			throw new MemberIsOwnerException();
209
		}
210
211
		$this->databaseService->getMembersMapper()
212
							  ->remove($member);
213
214
		$circle = $this->databaseService->getCirclesMapper()
215
										->getDetailsFromCircle(
216
											$this->userId, $circleId
217
										);
218
219
		return $this->databaseService->getMembersMapper()
220
									 ->getMembersFromCircle(
221
										 $circleId, ($circle->getUser()
222
															->getLevel()
223
													 >= Member::LEVEL_MODERATOR)
224
									 );
225
	}
226
227
}