Completed
Push — master ( 113154...113154 )
by Maxence
14s
created

CirclesService   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 253
Duplicated Lines 14.62 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 11
Bugs 1 Features 0
Metric Value
wmc 24
lcom 1
cbo 7
dl 37
loc 253
rs 10
c 11
b 1
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
B createCircle() 0 24 3
A listCircles() 0 17 3
A detailsCircle() 0 19 3
A joinCircle() 22 22 3
A leaveCircle() 0 21 3
A removeCircle() 0 13 2
B convertTypeStringToBitValue() 0 17 6

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\Service;
28
29
30
use OCA\Circles\Db\CirclesMapper;
31
use OCA\Circles\Exceptions\CircleTypeDisabledException;
32
use OCA\Circles\Exceptions\MemberDoesNotExistException;
33
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
34
use \OCA\Circles\Model\Circle;
35
use \OCA\Circles\Model\Member;
36
use OCP\IL10N;
37
38
class CirclesService {
39
40
	/** @var string */
41
	private $userId;
42
43
	/** @var IL10N */
44
	private $l10n;
45
46
	/** @var ConfigService */
47
	private $configService;
48
49
	/** @var CirclesMapper */
50
	private $dbCircles;
51
52
	/** @var MembersMapper */
53
	private $dbMembers;
54
55
	/** @var MiscService */
56
	private $miscService;
57
58
59
	/**
60
	 * CirclesService constructor.
61
	 *
62
	 * @param $userId
63
	 * @param IL10N $l10n
64
	 * @param ConfigService $configService
65
	 * @param DatabaseService $databaseService
66
	 * @param MiscService $miscService
67
	 */
68 View Code Duplication
	public function __construct(
69
		$userId,
70
		IL10N $l10n,
71
		ConfigService $configService,
72
		DatabaseService $databaseService,
73
		MiscService $miscService
74
	) {
75
		$this->userId = $userId;
76
		$this->l10n = $l10n;
77
		$this->configService = $configService;
78
		$this->miscService = $miscService;
79
80
		$this->dbCircles = $databaseService->getCirclesMapper();
81
		$this->dbMembers = $databaseService->getMembersMapper();
82
	}
83
84
85
	/**
86
	 * Create circle using this->userId as owner
87
	 *
88
	 * @param int $type
89
	 * @param string $name
90
	 *
91
	 * @return Circle
92
	 * @throws CircleTypeDisabledException
93
	 * @throws \Exception
94
	 */
95
	public function createCircle($type, $name) {
96
		self::convertTypeStringToBitValue($type);
97
98
		if (!$this->configService->isCircleAllowed($type)) {
99
			throw new CircleTypeDisabledException(
100
				$this->l10n->t('The creation of this type of circle is not allowed')
101
			);
102
		}
103
104
		$owner = new Member($this->l10n, $this->userId);
105
		$owner->setStatus(Member::STATUS_MEMBER);
106
		$circle = new Circle($type, $name);
107
		$circle->setMembers([$owner]);
108
109
		try {
110
			$this->dbCircles->create($circle, $owner);
111
			$this->dbMembers->add($owner);
112
		} catch (\Exception $e) {
113
			$this->dbCircles->destroy($circle->getId());
114
			throw $e;
115
		}
116
117
		return $circle;
118
	}
119
120
121
	/**
122
	 * list Circles depends on type (or all) and name (parts) and minimum level.
123
	 *
124
	 * @param $type
125
	 * @param string $name
126
	 * @param int $level
127
	 *
128
	 * @return array
129
	 * @throws CircleTypeDisabledException
130
	 */
131
	public function listCircles($type, $name = '', $level = 0) {
132
		self::convertTypeStringToBitValue($type);
133
134
		if (!$this->configService->isCircleAllowed((int)$type)) {
135
			throw new CircleTypeDisabledException(
136
				$this->l10n->t('The creation of this type of circle is not allowed')
137
			);
138
		}
139
140
		$data = [];
141
		$result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level);
142
		foreach ($result as $item) {
143
			$data[] = $item;
144
		}
145
146
		return $data;
147
	}
148
149
150
	/**
151
	 * returns details on circle and its members if this->userId is a member itself.
152
	 *
153
	 * @param $circleId
154
	 *
155
	 * @return Circle
156
	 * @throws \Exception
157
	 * @internal param $circleId
158
	 * @internal param string $iError
159
	 */
160
	public function detailsCircle($circleId) {
161
162
		try {
163
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
164
			if ($circle->getUser()
165
					   ->isLevel(Member::LEVEL_MEMBER)
166
			) {
167
				$members = $this->dbMembers->getMembersFromCircle(
168
					$circleId, $circle->getUser()
169
				);
170
				$circle->setMembers($members);
171
			}
172
		} catch (\Exception $e) {
173
			throw $e;
174
		}
175
176
		return $circle;
177
178
	}
179
180
	/**
181
	 * Join a circle.
182
	 *
183
	 * @param $circleId
184
	 *
185
	 * @return null|Member
186
	 * @throws \Exception
187
	 */
188 View Code Duplication
	public function joinCircle($circleId) {
189
190
		try {
191
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
192
193
			try {
194
				$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId);
195
			} catch (MemberDoesNotExistException $m) {
196
				$member = new Member($this->l10n, $this->userId, $circle->getId());
197
				$this->dbMembers->add($member);
198
			}
199
200
			$member->hasToBeAbleToJoinTheCircle();
201
			$member->joinCircle($circle->getType());
202
			$this->dbMembers->editMember($member);
203
204
		} catch (\Exception $e) {
205
			throw $e;
206
		}
207
208
		return $member;
209
	}
210
211
212
	/**
213
	 * Leave a circle.
214
	 *
215
	 * @param $circleId
216
	 *
217
	 * @return null|Member
218
	 * @throws \Exception
219
	 */
220
	public function leaveCircle($circleId) {
221
222
		try {
223
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
224
			$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false);
225
226
			if (!$member->isAlmostMember()) {
227
				$member->hasToBeMember();
228
			}
229
230
			$member->cantBeOwner();
231
			$member->setStatus(Member::STATUS_NONMEMBER);
232
			$member->setLevel(Member::LEVEL_NONE);
233
			$this->dbMembers->editMember($member);
234
235
		} catch (\Exception $e) {
236
			throw $e;
237
		}
238
239
		return $member;
240
	}
241
242
243
	/**
244
	 * destroy a circle.
245
	 *
246
	 * @param int $circleId
247
	 *
248
	 * @throws MemberIsNotOwnerException
249
	 */
250
	public function removeCircle($circleId) {
251
252
		try {
253
			$member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false);
254
			$member->hasToBeOwner();
255
256
			$this->dbMembers->removeAllFromCircle($circleId);
257
			$this->dbCircles->destroy($circleId);
258
259
		} catch (MemberIsNotOwnerException $e) {
260
			throw $e;
261
		}
262
	}
263
264
265
	/**
266
	 * Convert a Type in String to its Bit Value
267
	 *
268
	 * @param $type
269
	 *
270
	 * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
271
	 */
272
	public static function convertTypeStringToBitValue(& $type) {
273
		if (strtolower($type) === 'personal') {
274
			$type = Circle::CIRCLES_PERSONAL;
275
		}
276
		if (strtolower($type) === 'hidden') {
277
			$type = Circle::CIRCLES_HIDDEN;
278
		}
279
		if (strtolower($type) === 'private') {
280
			$type = Circle::CIRCLES_PRIVATE;
281
		}
282
		if (strtolower($type) === 'public') {
283
			$type = Circle::CIRCLES_PUBLIC;
284
		}
285
		if (strtolower($type) === 'all') {
286
			$type = Circle::CIRCLES_ALL;
287
		}
288
	}
289
290
}