Completed
Push — master ( fff755...8f8f7b )
by Maxence
03:48
created

CirclesService::convertTypeStringToBitValue()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 12
nc 32
nop 1
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\Db\MembersMapper;
32
use OCA\Circles\Exceptions\CircleTypeDisabledException;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
35
use \OCA\Circles\Model\Circle;
36
use \OCA\Circles\Model\Member;
37
use OCP\IL10N;
38
39
class CirclesService {
40
41
	/** @var string */
42
	private $userId;
43
44
	/** @var IL10N */
45
	private $l10n;
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var CirclesMapper */
51
	private $dbCircles;
52
53
	/** @var MembersMapper */
54
	private $dbMembers;
55
56
	/** @var MiscService */
57
	private $miscService;
58
59
60
	/**
61
	 * CirclesService constructor.
62
	 *
63
	 * @param $userId
64
	 * @param IL10N $l10n
65
	 * @param ConfigService $configService
66
	 * @param DatabaseService $databaseService
67
	 * @param MiscService $miscService
68
	 */
69 View Code Duplication
	public function __construct(
0 ignored issues
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...
70
		$userId,
71
		IL10N $l10n,
72
		ConfigService $configService,
73
		DatabaseService $databaseService,
74
		MiscService $miscService
75
	) {
76
		$this->userId = $userId;
77
		$this->l10n = $l10n;
78
		$this->configService = $configService;
79
		$this->miscService = $miscService;
80
81
		$this->dbCircles = $databaseService->getCirclesMapper();
82
		$this->dbMembers = $databaseService->getMembersMapper();
83
	}
84
85
86
	/**
87
	 * Create circle using this->userId as owner
88
	 *
89
	 * @param int $type
90
	 * @param string $name
91
	 *
92
	 * @return Circle
93
	 * @throws CircleTypeDisabledException
94
	 * @throws \Exception
95
	 */
96
	public function createCircle($type, $name) {
97
		self::convertTypeStringToBitValue($type);
98
99
		if ($type === "") {
100
			throw new CircleTypeDisabledException(
101
				$this->l10n->t('You need a specify a type of circle')
102
			);
103
		}
104
105
		if (!$this->configService->isCircleAllowed($type)) {
106
			throw new CircleTypeDisabledException(
107
				$this->l10n->t('You cannot create this type of circle')
108
			);
109
		}
110
111
		$owner = new Member($this->l10n, $this->userId);
112
		$owner->setStatus(Member::STATUS_MEMBER);
113
		$circle = new Circle($this->l10n, $type, $name);
114
		$circle->setMembers([$owner]);
115
116
		try {
117
			$this->dbCircles->create($circle, $owner);
118
			$this->dbMembers->add($owner);
119
		} catch (\Exception $e) {
120
			$this->dbCircles->destroy($circle->getId());
121
			throw $e;
122
		}
123
124
		return $circle;
125
	}
126
127
128
	/**
129
	 * list Circles depends on type (or all) and name (parts) and minimum level.
130
	 *
131
	 * @param $type
132
	 * @param string $name
133
	 * @param int $level
134
	 *
135
	 * @return Circle[]
136
	 * @throws CircleTypeDisabledException
137
	 */
138
	public function listCircles($type, $name = '', $level = 0) {
139
		self::convertTypeStringToBitValue($type);
140
141
		if (!$this->configService->isCircleAllowed((int)$type)) {
142
			throw new CircleTypeDisabledException(
143
				$this->l10n->t('You cannot display this type of circle')
144
			);
145
		}
146
147
		$data = [];
148
		$result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level);
149
		foreach ($result as $item) {
150
			$data[] = $item;
151
		}
152
153
		return $data;
154
	}
155
156
157
	/**
158
	 * returns details on circle and its members if this->userId is a member itself.
159
	 *
160
	 * @param $circleId
161
	 *
162
	 * @return Circle
163
	 * @throws \Exception
164
	 * @internal param $circleId
165
	 * @internal param string $iError
166
	 */
167
	public function detailsCircle($circleId) {
168
169
		try {
170
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
171
			if ($circle->getUser()
172
					   ->isLevel(Member::LEVEL_MEMBER)
173
			) {
174
				$members = $this->dbMembers->getMembersFromCircle(
175
					$circleId, $circle->getUser()
176
				);
177
				$circle->setMembers($members);
178
			}
179
		} catch (\Exception $e) {
180
			throw $e;
181
		}
182
183
		return $circle;
184
	}
185
186
187
	/**
188
	 * Join a circle.
189
	 *
190
	 * @param $circleId
191
	 *
192
	 * @return null|Member
193
	 * @throws \Exception
194
	 */
195 View Code Duplication
	public function joinCircle($circleId) {
196
197
		try {
198
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
199
200
			try {
201
				$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId);
202
			} catch (MemberDoesNotExistException $m) {
203
				$member = new Member($this->l10n, $this->userId, $circle->getId());
204
				$this->dbMembers->add($member);
205
			}
206
207
			$member->hasToBeAbleToJoinTheCircle();
208
			$member->joinCircle($circle->getType());
209
			$this->dbMembers->editMember($member);
210
211
		} catch (\Exception $e) {
212
			throw $e;
213
		}
214
215
		return $member;
216
	}
217
218
219
	/**
220
	 * Leave a circle.
221
	 *
222
	 * @param $circleId
223
	 *
224
	 * @return null|Member
225
	 * @throws \Exception
226
	 */
227
	public function leaveCircle($circleId) {
228
229
		try {
230
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
231
			$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false);
232
233
			if (!$member->isAlmostMember()) {
234
				$member->hasToBeMember();
235
			}
236
237
			$member->cantBeOwner();
238
			$member->setStatus(Member::STATUS_NONMEMBER);
239
			$member->setLevel(Member::LEVEL_NONE);
240
			$this->dbMembers->editMember($member);
241
242
		} catch (\Exception $e) {
243
			throw $e;
244
		}
245
246
		return $member;
247
	}
248
249
250
	/**
251
	 * destroy a circle.
252
	 *
253
	 * @param int $circleId
254
	 *
255
	 * @throws MemberIsNotOwnerException
256
	 */
257
	public function removeCircle($circleId) {
258
259
		try {
260
			$member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false);
261
			$member->hasToBeOwner();
262
263
			$this->dbMembers->removeAllFromCircle($circleId);
264
			$this->dbCircles->destroy($circleId);
265
266
		} catch (MemberIsNotOwnerException $e) {
267
			throw $e;
268
		}
269
	}
270
271
272
	/**
273
	 * @param $circleName
274
	 *
275
	 * @return Circle|null
276
	 */
277
	public function infoCircleByName($circleName) {
278
		return $this->dbCircles->getDetailsFromCircleByName($circleName);
279
	}
280
281
	/**
282
	 * Convert a Type in String to its Bit Value
283
	 *
284
	 * @param $type
285
	 *
286
	 * @return int
287
	 */
288
	public static function convertTypeStringToBitValue(& $type) {
289
		if (strtolower($type) === 'personal') {
290
			$type = Circle::CIRCLES_PERSONAL;
291
		}
292
		if (strtolower($type) === 'hidden') {
293
			$type = Circle::CIRCLES_HIDDEN;
294
		}
295
		if (strtolower($type) === 'private') {
296
			$type = Circle::CIRCLES_PRIVATE;
297
		}
298
		if (strtolower($type) === 'public') {
299
			$type = Circle::CIRCLES_PUBLIC;
300
		}
301
		if (strtolower($type) === 'all') {
302
			$type = Circle::CIRCLES_ALL;
303
		}
304
305
		return 0;
306
	}
307
308
309
	/**
310
	 * getCircleIcon()
311
	 *
312
	 * Return the right imagePath for a type of circle.
313
	 *
314
	 * @param string $type
315
	 *
316
	 * @return string
317
	 */
318
	public static function getCircleIcon($type) {
319
		$urlGen = \OC::$server->getURLGenerator();
320
		switch ($type) {
321
			case Circle::CIRCLES_PERSONAL:
322
				return $urlGen->imagePath('circles', 'personal.svg');
323
			case Circle::CIRCLES_PRIVATE:
324
				return $urlGen->imagePath('circles', 'private.svg');
325
			case Circle::CIRCLES_HIDDEN:
326
				return $urlGen->imagePath('circles', 'hidden.svg');
327
			case Circle::CIRCLES_PUBLIC:
328
				return $urlGen->imagePath('circles', 'public.svg');
329
		}
330
331
		return $urlGen->imagePath('circles', 'black_circle.svg');
332
	}
333
334
}