Completed
Push — master ( fa4c5d...fa4c5d )
by Maxence
05:16 queued 02:40
created

CirclesService::settingsCircle()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 13
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\CirclesMapper;
31
use OCA\Circles\Db\CirclesRequest;
32
use OCA\Circles\Db\MembersMapper;
33
use OCA\Circles\Exceptions\CircleTypeDisabledException;
34
use OCA\Circles\Exceptions\MemberDoesNotExistException;
35
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
36
use \OCA\Circles\Model\Circle;
37
use \OCA\Circles\Model\Member;
38
use OCP\IL10N;
39
40
class CirclesService {
41
42
	/** @var string */
43
	private $userId;
44
45
	/** @var IL10N */
46
	private $l10n;
47
48
	/** @var ConfigService */
49
	private $configService;
50
51
	/** @var CirclesRequest */
52
	private $circlesRequest;
53
54
	/** @var CirclesMapper */
55
	private $dbCircles;
56
57
	/** @var MembersMapper */
58
	private $dbMembers;
59
60
	/** @var EventsService */
61
	private $eventsService;
62
63
	/** @var MiscService */
64
	private $miscService;
65
66
67
	/**
68
	 * CirclesService constructor.
69
	 *
70
	 * @param $userId
71
	 * @param IL10N $l10n
72
	 * @param ConfigService $configService
73
	 * @param CirclesRequest $circlesRequest
74
	 * @param DatabaseService $databaseService
75
	 * @param EventsService $eventsService
76
	 * @param MiscService $miscService
77
	 */
78 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...
79
		$userId,
80
		IL10N $l10n,
81
		ConfigService $configService,
82
		CirclesRequest $circlesRequest,
83
		DatabaseService $databaseService,
84
		EventsService $eventsService,
85
		MiscService $miscService
86
	) {
87
		$this->userId = $userId;
88
		$this->l10n = $l10n;
89
		$this->configService = $configService;
90
		$this->circlesRequest = $circlesRequest;
91
		$this->eventsService = $eventsService;
92
		$this->miscService = $miscService;
93
94
		$this->dbCircles = $databaseService->getCirclesMapper();
95
		$this->dbMembers = $databaseService->getMembersMapper();
96
	}
97
98
99
	/**
100
	 * Create circle using this->userId as owner
101
	 *
102
	 * @param int $type
103
	 * @param string $name
104
	 *
105
	 * @return Circle
106
	 * @throws CircleTypeDisabledException
107
	 * @throws \Exception
108
	 */
109
	public function createCircle($type, $name) {
110
		self::convertTypeStringToBitValue($type);
111
112
		if ($type === "") {
113
			throw new CircleTypeDisabledException(
114
				$this->l10n->t('You need a specify a type of circle')
115
			);
116
		}
117
118
		if (!$this->configService->isCircleAllowed($type)) {
119
			throw new CircleTypeDisabledException(
120
				$this->l10n->t('You cannot create this type of circle')
121
			);
122
		}
123
124
		$circle = new Circle($this->l10n, $type, $name);
125
		$owner = new Member($this->l10n, $this->userId);
126
		$circle->setOwner($owner);
127
128
		try {
129
			$this->dbCircles->create($circle);
130
			$circle->getOwner()
131
				   ->setCircleId($circle->getId())
132
				   ->setLevel(Member::LEVEL_OWNER)
133
				   ->setStatus(Member::STATUS_MEMBER);
134
			$this->dbMembers->add($circle->getOwner());
135
		} catch (\Exception $e) {
136
			$this->dbCircles->destroy($circle->getId());
137
			throw $e;
138
		}
139
140
		$this->eventsService->onCircleCreation($circle);
141
142
		return $circle;
143
	}
144
145
146
	/**
147
	 * list Circles depends on type (or all) and name (parts) and minimum level.
148
	 *
149
	 * @param $type
150
	 * @param string $name
151
	 * @param int $level
152
	 *
153
	 * @return Circle[]
154
	 * @throws CircleTypeDisabledException
155
	 */
156
	public function listCircles($type, $name = '', $level = 0) {
157
		self::convertTypeStringToBitValue($type);
158
159
		if (!$this->configService->isCircleAllowed((int)$type)) {
160
			throw new CircleTypeDisabledException(
161
				$this->l10n->t('You cannot display this type of circle')
162
			);
163
		}
164
165
		$data = [];
166
		$result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level);
167
		foreach ($result as $item) {
168
			$data[] = $item;
169
		}
170
171
		return $data;
172
	}
173
174
175
	/**
176
	 * returns details on circle and its members if this->userId is a member itself.
177
	 *
178
	 * @param $circleId
179
	 *
180
	 * @return Circle
181
	 * @throws \Exception
182
	 * @internal param $circleId
183
	 * @internal param string $iError
184
	 */
185
	public function detailsCircle($circleId) {
186
187
		try {
188
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
189
			if ($circle->getUser()
190
					   ->isLevel(Member::LEVEL_MEMBER)
191
			) {
192
				$members = $this->dbMembers->getMembersFromCircle(
193
					$circleId, $circle->getUser()
194
				);
195
				$circle->setMembers($members);
196
			}
197
		} catch (\Exception $e) {
198
			throw $e;
199
		}
200
201
		return $circle;
202
	}
203
204
	/**
205
	 * save new settings if current user is admin.
206
	 *
207
	 * @param $circleId
208
	 * @param array $settings
209
	 *
210
	 * @return Circle
211
	 * @throws \Exception
212
	 */
213
	public function settingsCircle($circleId, $settings) {
214
215
		$this->miscService->log("____");
216
		try {
217
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
218
			$circle->getUser()
219
				   ->hasToBeAdmin();
220
221
			$ak = array_keys($settings);
222
			foreach ($ak AS $k) {
223
				$circle->setSetting($k, $settings[$k]);
224
			}
225
226
			$this->circlesRequest->updateCircle($circle);
227
		} catch (\Exception $e) {
228
			throw $e;
229
		}
230
231
		return $circle;
232
	}
233
234
235
	/**
236
	 * Join a circle.
237
	 *
238
	 * @param $circleId
239
	 *
240
	 * @return null|Member
241
	 * @throws \Exception
242
	 */
243 View Code Duplication
	public function joinCircle($circleId) {
244
245
		try {
246
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
247
248
			try {
249
				$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId);
250
			} catch (MemberDoesNotExistException $m) {
251
				$member = new Member($this->l10n, $this->userId, $circle->getId());
252
				$this->dbMembers->add($member);
253
			}
254
255
			$member->hasToBeAbleToJoinTheCircle();
256
			$member->joinCircle($circle->getType());
257
			$this->dbMembers->editMember($member);
258
			$this->eventsService->onMemberNew($circle, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->dbMembers->getMem...getId(), $this->userId) on line 249 can be null; however, OCA\Circles\Service\EventsService::onMemberNew() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
259
		} catch (\Exception $e) {
260
			throw $e;
261
		}
262
263
		return $member;
264
	}
265
266
267
	/**
268
	 * Leave a circle.
269
	 *
270
	 * @param $circleId
271
	 *
272
	 * @return null|Member
273
	 * @throws \Exception
274
	 */
275
	public function leaveCircle($circleId) {
276
277
		try {
278
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
279
			$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false);
280
281
			if (!$member->isAlmostMember()) {
282
				$member->hasToBeMember();
283
			}
284
285
			$member->cantBeOwner();
286
			$member->setStatus(Member::STATUS_NONMEMBER);
287
			$member->setLevel(Member::LEVEL_NONE);
288
			$this->dbMembers->editMember($member);
289
			$this->eventsService->onMemberLeaving($circle, $member);
0 ignored issues
show
Bug introduced by
It seems like $member defined by $this->dbMembers->getMem..., $this->userId, false) on line 279 can be null; however, OCA\Circles\Service\Even...vice::onMemberLeaving() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
290
		} catch (\Exception $e) {
291
			throw $e;
292
		}
293
294
		return $member;
295
	}
296
297
298
	/**
299
	 * destroy a circle.
300
	 *
301
	 * @param int $circleId
302
	 *
303
	 * @throws MemberIsNotOwnerException
304
	 */
305
	public function removeCircle($circleId) {
306
307
		try {
308
			$member = $this->dbMembers->getMemberFromCircle($circleId, $this->userId, false);
309
			$member->hasToBeOwner();
310
311
			$circle = $this->dbCircles->getDetailsFromCircle($circleId, $this->userId);
312
			$this->eventsService->onCircleDestruction($circle);
313
314
			$this->dbMembers->removeAllFromCircle($circleId);
315
			$this->dbCircles->destroy($circleId);
316
317
		} catch (MemberIsNotOwnerException $e) {
318
			throw $e;
319
		}
320
	}
321
322
323
	/**
324
	 * @param $circleName
325
	 *
326
	 * @return Circle|null
327
	 */
328
	public function infoCircleByName($circleName) {
329
		return $this->dbCircles->getDetailsFromCircleByName($circleName);
330
	}
331
332
	/**
333
	 * Convert a Type in String to its Bit Value
334
	 *
335
	 * @param $type
336
	 *
337
	 * @return int
338
	 */
339
	public static function convertTypeStringToBitValue(& $type) {
340
		if (strtolower($type) === 'personal') {
341
			$type = Circle::CIRCLES_PERSONAL;
342
		}
343
		if (strtolower($type) === 'hidden') {
344
			$type = Circle::CIRCLES_HIDDEN;
345
		}
346
		if (strtolower($type) === 'private') {
347
			$type = Circle::CIRCLES_PRIVATE;
348
		}
349
		if (strtolower($type) === 'public') {
350
			$type = Circle::CIRCLES_PUBLIC;
351
		}
352
		if (strtolower($type) === 'all') {
353
			$type = Circle::CIRCLES_ALL;
354
		}
355
356
		return 0;
357
	}
358
359
360
	/**
361
	 * getCircleIcon()
362
	 *
363
	 * Return the right imagePath for a type of circle.
364
	 *
365
	 * @param string $type
366
	 *
367
	 * @return string
368
	 */
369
	public static function getCircleIcon($type) {
370
		$urlGen = \OC::$server->getURLGenerator();
371
		switch ($type) {
372
			case Circle::CIRCLES_PERSONAL:
373
				return $urlGen->imagePath('circles', 'personal.svg');
374
			case Circle::CIRCLES_PRIVATE:
375
				return $urlGen->imagePath('circles', 'private.svg');
376
			case Circle::CIRCLES_HIDDEN:
377
				return $urlGen->imagePath('circles', 'hidden.svg');
378
			case Circle::CIRCLES_PUBLIC:
379
				return $urlGen->imagePath('circles', 'public.svg');
380
		}
381
382
		return $urlGen->imagePath('circles', 'black_circle.svg');
383
	}
384
385
}