Completed
Push — master ( 060fe4...9de15f )
by Maxence
02:41
created

CirclesService::settingsCircle()   A

Complexity

Conditions 3
Paths 9

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 9.4285
cc 3
eloc 12
nc 9
nop 2
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\CirclesRequest;
31
use OCA\Circles\Db\MembersRequest;
32
use OCA\Circles\Exceptions\CircleAlreadyExistsException;
33
use OCA\Circles\Exceptions\CircleTypeDisabledException;
34
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException;
35
use OCA\Circles\Exceptions\MemberDoesNotExistException;
36
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
37
use OCA\Circles\Model\Circle;
38
use OCA\Circles\Model\Member;
39
use OCP\IL10N;
40
41
class CirclesService {
42
43
	/** @var string */
44
	private $userId;
45
46
	/** @var IL10N */
47
	private $l10n;
48
49
	/** @var ConfigService */
50
	private $configService;
51
52
	/** @var CirclesRequest */
53
	private $circlesRequest;
54
55
	/** @var MembersRequest */
56
	private $membersRequest;
57
58
	/** @var EventsService */
59
	private $eventsService;
60
61
	/** @var MiscService */
62
	private $miscService;
63
64
65
	/**
66
	 * CirclesService constructor.
67
	 *
68
	 * @param $userId
69
	 * @param IL10N $l10n
70
	 * @param ConfigService $configService
71
	 * @param CirclesRequest $circlesRequest
72
	 * @param MembersRequest $membersRequest
73
	 * @param EventsService $eventsService
74
	 * @param MiscService $miscService
75
	 */
76 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...
77
		$userId,
78
		IL10N $l10n,
79
		ConfigService $configService,
80
		CirclesRequest $circlesRequest,
81
		MembersRequest $membersRequest,
82
		EventsService $eventsService,
83
		MiscService $miscService
84
	) {
85
		$this->userId = $userId;
86
		$this->l10n = $l10n;
87
		$this->configService = $configService;
88
		$this->circlesRequest = $circlesRequest;
89
		$this->membersRequest = $membersRequest;
90
		$this->eventsService = $eventsService;
91
		$this->miscService = $miscService;
92
	}
93
94
95
	/**
96
	 * Create circle using this->userId as owner
97
	 *
98
	 * @param int|string $type
99
	 * @param string $name
100
	 *
101
	 * @return Circle
102
	 * @throws CircleTypeDisabledException
103
	 * @throws \Exception
104
	 */
105
	public function createCircle($type, $name) {
106
		self::convertTypeStringToBitValue($type);
107
		$type = (int)$type;
108
109
		if ($type === '') {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $type (integer) and '' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
110
			throw new CircleTypeDisabledException(
111
				$this->l10n->t('You need a specify a type of circle')
112
			);
113
		}
114
115
		if (!$this->configService->isCircleAllowed($type)) {
116
			throw new CircleTypeDisabledException(
117
				$this->l10n->t('You cannot create this type of circle')
118
			);
119
		}
120
121
		$circle = new Circle($this->l10n, $type, $name);
122
123
		try {
124
			$this->circlesRequest->createCircle($circle, $this->userId);
125
			$this->membersRequest->createMember($circle->getOwner());
126
		} catch (CircleAlreadyExistsException $e) {
127
			throw $e;
128
		}
129
130
		$this->eventsService->onCircleCreation($circle);
131
132
		return $circle;
133
	}
134
135
136
	/**
137
	 * list Circles depends on type (or all) and name (parts) and minimum level.
138
	 *
139
	 * @param mixed $type
140
	 * @param string $name
141
	 * @param int $level
142
	 *
143
	 * @return Circle[]
144
	 * @throws CircleTypeDisabledException
145
	 */
146
	public function listCircles($type, $name = '', $level = 0) {
147
		self::convertTypeStringToBitValue($type);
148
149
		if (!$this->configService->isCircleAllowed((int)$type)) {
150
			throw new CircleTypeDisabledException(
151
				$this->l10n->t('You cannot display this type of circle')
152
			);
153
		}
154
155
		$data = [];
156
		$result = $this->circlesRequest->getCircles($this->userId, $type, $name, $level);
157
		foreach ($result as $item) {
158
			$data[] = $item;
159
		}
160
161
		return $data;
162
	}
163
164
165
	/**
166
	 * returns details on circle and its members if this->userId is a member itself.
167
	 *
168
	 * @param string $circleUniqueId
169
	 *
170
	 * @return Circle
171
	 * @throws \Exception
172
	]	 */
173
	public function detailsCircle($circleUniqueId) {
174
175
		try {
176
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
177
			if ($circle->getHigherViewer()
178
					   ->isLevel(Member::LEVEL_MEMBER)
179
			) {
180
				$this->detailsCircleMembers($circle);
181
				$this->detailsCircleLinkedGroups($circle);
182
				$this->detailsCircleFederatedCircles($circle);
183
			}
184
		} catch (\Exception $e) {
185
			throw $e;
186
		}
187
188
		return $circle;
189
	}
190
191
192
	/**
193
	 * get the Members list and add the result to the Circle.
194
	 *
195
	 * @param Circle $circle
196
	 */
197
	private function detailsCircleMembers(Circle &$circle) {
198
		$members =
199
			$this->membersRequest->getMembers($circle->getUniqueId(), $circle->getHigherViewer());
200
201
		$circle->setMembers($members);
202
	}
203
204
205
	/**
206
	 * get the Linked Group list and add the result to the Circle.
207
	 *
208
	 * @param Circle $circle
209
	 */
210
	private function detailsCircleLinkedGroups(Circle &$circle) {
211
		$groups = [];
212
		if ($this->configService->isLinkedGroupsAllowed()) {
213
			$groups =
214
				$this->membersRequest->getGroupsFromCircle(
215
					$circle->getUniqueId(), $circle->getHigherViewer()
216
				);
217
		}
218
219
		$circle->setGroups($groups);
220
	}
221
222
223
	/**
224
	 * get the Federated Circles list and add the result to the Circle.
225
	 *
226
	 * @param Circle $circle
227
	 */
228
	private function detailsCircleFederatedCircles(Circle &$circle) {
229
		$links = [];
230
231
		try {
232
			if ($this->configService->isFederatedCirclesAllowed()) {
233
				$circle->hasToBeFederated();
234
				$links = $this->circlesRequest->getLinksFromCircle($circle->getUniqueId());
235
			}
236
		} catch (FederatedCircleNotAllowedException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
237
		}
238
239
		$circle->setLinks($links);
240
	}
241
242
243
	/**
244
	 * save new settings if current user is admin.
245
	 *
246
	 * @param string $circleUniqueId
247
	 * @param array $settings
248
	 *
249
	 * @return Circle
250
	 * @throws \Exception
251
	 */
252
	public function settingsCircle($circleUniqueId, $settings) {
253
254
		try {
255
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
256
			$circle->getHigherViewer()
257
				   ->hasToBeOwner();
258
259
			$ak = array_keys($settings);
260
			foreach ($ak AS $k) {
261
				$circle->setSetting($k, $settings[$k]);
262
			}
263
264
			$this->circlesRequest->updateCircle($circle);
265
		} catch (\Exception $e) {
266
			throw $e;
267
		}
268
269
		return $circle;
270
	}
271
272
273
	/**
274
	 * Join a circle.
275
	 *
276
	 * @param string $circleUniqueId
277
	 *
278
	 * @return null|Member
279
	 * @throws \Exception
280
	 */
281 View Code Duplication
	public function joinCircle($circleUniqueId) {
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...
282
283
		try {
284
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
285
286
			$member = $this->membersRequest->getFreshNewMember($circleUniqueId, $this->userId, Member::TYPE_USER);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
287
			$member->hasToBeAbleToJoinTheCircle();
288
			$member->joinCircle($circle->getType());
289
			$this->membersRequest->updateMember($member);
290
291
			$this->eventsService->onMemberNew($circle, $member);
292
		} catch (\Exception $e) {
293
			throw $e;
294
		}
295
296
		return $member;
297
	}
298
299
300
	/**
301
	 * Leave a circle.
302
	 *
303
	 * @param string $circleUniqueId
304
	 *
305
	 * @return null|Member
306
	 * @throws \Exception
307
	 */
308 View Code Duplication
	public function leaveCircle($circleUniqueId) {
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...
309
310
		try {
311
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
312
			$member = $circle->getViewer();
313
314
			$member->hasToBeMemberOrAlmost();
315
			$member->cantBeOwner();
316
317
			$this->eventsService->onMemberLeaving($circle, $member);
318
319
			$member->setStatus(Member::STATUS_NONMEMBER);
320
			$member->setLevel(Member::LEVEL_NONE);
321
			$this->membersRequest->updateMember($member);
322
		} catch (\Exception $e) {
323
			throw $e;
324
		}
325
326
		return $member;
327
	}
328
329
330
	/**
331
	 * destroy a circle.
332
	 *
333
	 * @param string $circleUniqueId
334
	 *
335
	 * @throws MemberIsNotOwnerException
336
	 */
337
	public function removeCircle($circleUniqueId) {
338
339
		try {
340
			$circle = $this->circlesRequest->getCircle($circleUniqueId, $this->userId);
341
			$circle->getHigherViewer()
342
				   ->hasToBeOwner();
343
344
			$this->eventsService->onCircleDestruction($circle);
345
346
			$this->membersRequest->removeAllFromCircle($circleUniqueId);
347
			$this->circlesRequest->destroyCircle($circleUniqueId);
348
349
		} catch (MemberIsNotOwnerException $e) {
350
			throw $e;
351
		}
352
	}
353
354
355
	/**
356
	 * @param $circleName
357
	 *
358
	 * @return Circle|null
359
	 */
360
	public function infoCircleByName($circleName) {
361
		return $this->circlesRequest->forceGetCircleByName($circleName);
362
	}
363
364
	/**
365
	 * Convert a Type in String to its Bit Value
366
	 *
367
	 * @param string $type
368
	 */
369
	public static function convertTypeStringToBitValue(&$type) {
370
		if (strtolower($type) === 'personal') {
371
			$type = Circle::CIRCLES_PERSONAL;
372
		}
373
		if (strtolower($type) === 'secret') {
374
			$type = Circle::CIRCLES_SECRET;
375
		}
376
		if (strtolower($type) === 'closed') {
377
			$type = Circle::CIRCLES_CLOSED;
378
		}
379
		if (strtolower($type) === 'public') {
380
			$type = Circle::CIRCLES_PUBLIC;
381
		}
382
		if (strtolower($type) === 'all') {
383
			$type = Circle::CIRCLES_ALL;
384
		}
385
	}
386
387
388
	/**
389
	 * getCircleIcon()
390
	 *
391
	 * Return the right imagePath for a type of circle.
392
	 *
393
	 * @param string $type
394
	 * @param bool $png
395
	 *
396
	 * @return string
397
	 */
398
	public static function getCircleIcon($type, $png = false) {
399
400
		$ext = '.svg';
401
		if ($png === true) {
402
			$ext = '.png';
403
		}
404
405
		$urlGen = \OC::$server->getURLGenerator();
406
		switch ($type) {
407
			case Circle::CIRCLES_PERSONAL:
408
				return $urlGen->getAbsoluteURL(
409
					$urlGen->imagePath('circles', 'personal' . $ext)
410
				);
411
			case Circle::CIRCLES_CLOSED:
412
				return $urlGen->getAbsoluteURL(
413
					$urlGen->imagePath('circles', 'closed' . $ext)
414
				);
415
			case Circle::CIRCLES_SECRET:
416
				return $urlGen->getAbsoluteURL(
417
					$urlGen->imagePath('circles', 'secret' . $ext)
418
				);
419
			case Circle::CIRCLES_PUBLIC:
420
				return $urlGen->getAbsoluteURL(
421
					$urlGen->imagePath('circles', 'public' . $ext)
422
				);
423
		}
424
425
		return $urlGen->getAbsoluteURL($urlGen->imagePath('circles', 'black_circle' . $ext));
426
	}
427
428
}