Completed
Push — master ( 7a51c1...7a51c1 )
by Maxence
04:21 queued 02:15
created

CirclesService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 15
Ratio 100 %

Importance

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