Completed
Pull Request — master (#6)
by Maxence
03:50 queued 47s
created

MembersService::InviteMemberToPrivateCircle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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
		// we check that this->userId is moderator
103
		try {
104
			$this->databaseService->getMembersMapper()
105
								  ->getMemberFromCircle($circleId, $this->userId)
106
								  ->isModerator();
107
		} catch (MemberDoesNotExistException $e) {
108
			throw $e;
109
		} catch (MemberIsNotModeratorException $e) {
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
117
		} catch (MemberDoesNotExistException $e) {
118
			$member = new Member();
119
			$member->setCircleId($circleId);
120
			$member->setUserId($name);
121
			$member->setLevel(Member::LEVEL_NONE);
122
			$member->setStatus(Member::STATUS_NONMEMBER);
123
124
			$this->databaseService->getMembersMapper()
125
								  ->add(
126
									  $member
127
								  );
128
		}
129
130
		try {
131
			$circle = $this->databaseService->getCirclesMapper()
132
											->getDetailsFromCircle($this->userId, $circleId);
133
		} catch (CircleDoesNotExistException $e) {
134
			throw $e;
135
		}
136
137
		if ($member->getLevel() > Member::LEVEL_NONE
138
			|| ($member->getStatus() !== Member::STATUS_NONMEMBER
139
				&& $member->getStatus() !== Member::STATUS_REQUEST)
140
		) {
141
			throw new MemberAlreadyExistsException();
142
		}
143
144
		$member->setCircleId($circleId);
145
		$member->setUserId($name);
146
147
		if ($circle->getType() === Circle::CIRCLES_PRIVATE) {
148
			self::InviteMemberToPrivateCircle($member);
149
		} else {
150
			self::AddMemberToCircle($member);
151
		}
152
153
		$this->databaseService->getMembersMapper()
154
							  ->editMember($member);
155
156
		return $this->databaseService->getMembersMapper()
157
									 ->getMembersFromCircle(
158
										 $circleId, ($circle->getUser()
159
															->getLevel()
160
													 >= Member::LEVEL_MODERATOR)
161
									 );
162
	}
163
164
165
	/**
166
	 * Invite a Member to a private Circle, or accept his request.
167
	 *
168
	 * @param $member
169
	 */
170
	public static function InviteMemberToPrivateCircle(&$member) {
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...
171
		if ($member->getStatus() === Member::STATUS_REQUEST) {
172
			self::AddMemberToCircle($member);
173
		} else {
174
			$member->setLevel(Member::LEVEL_NONE);
175
			$member->setStatus(Member::STATUS_INVITED);
176
		}
177
	}
178
179
	/**
180
	 * add a member to a circle.
181
	 *
182
	 * @param $member
183
	 */
184
	public static function AddMemberToCircle(&$member) {
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...
185
		$member->setLevel(Member::LEVEL_MEMBER);
186
		$member->setStatus(Member::STATUS_MEMBER);
187
	}
188
189
190
	/**
191
	 * @param $circleId
192
	 * @param $name
193
	 *
194
	 * @return array
195
	 * @throws MemberDoesNotExistException
196
	 * @throws MemberIsNotModeratorException
197
	 * @throws MemberIsOwnerException
198
	 */
199
	public function removeMember($circleId, $name) {
200
201
		try {
202
			$ismod = $this->databaseService->getMembersMapper()
203
										   ->getMemberFromCircle($circleId, $this->userId);
204
		} catch (MemberDoesNotExistException $e) {
205
			throw $e;
206
		}
207
208
209
		if ($ismod->getLevel() < Member::LEVEL_MODERATOR) {
210
			throw new MemberIsNotModeratorException("You are not moderator of this circle");
211
		}
212
213
		try {
214
			$member = $this->databaseService->getMembersMapper()
215
											->getMemberFromCircle($circleId, $name);
216
		} catch (MemberDoesNotExistException $e) {
217
			throw $e;
218
		}
219
220
221
		if ($member->getLevel() === Member::LEVEL_OWNER) {
222
			throw new MemberIsOwnerException();
223
		}
224
225
		$this->databaseService->getMembersMapper()
226
							  ->remove($member);
227
228
		$circle = $this->databaseService->getCirclesMapper()
229
										->getDetailsFromCircle(
230
											$this->userId, $circleId
231
										);
232
233
		return $this->databaseService->getMembersMapper()
234
									 ->getMembersFromCircle(
235
										 $circleId, ($circle->getUser()
236
															->getLevel()
237
													 >= Member::LEVEL_MODERATOR)
238
									 );
239
	}
240
241
}