Completed
Push — some-scrutinizing ( 26da3a...6d8498 )
by Maxence
02:28
created

CirclesService::removeCircle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
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\Exceptions\CircleAlreadyExistsException;
31
use OCA\Circles\Exceptions\CircleCreationException;
32
use OCA\Circles\Exceptions\CircleDoesNotExistException;
33
use OCA\Circles\Exceptions\CircleTypeDisabledException;
34
use OCA\Circles\Exceptions\ConfigNoCircleAvailable;
35
use OCA\Circles\Exceptions\MemberCantJoinPersonalCircle;
36
use OCA\Circles\Exceptions\MemberDoesNotExistException;
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 DatabaseService */
53
	private $dbService;
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->dbService = $databaseService;
79
		$this->miscService = $miscService;
80
	}
81
82
83
	/**
84
	 * Create circle using this->userId as owner
85
	 *
86
	 * @param int $type
87
	 * @param string $name
88
	 *
89
	 * @return Circle
90
	 * @throws CircleTypeDisabledException
91
	 * @throws \Exception
92
	 */
93
	public function createCircle($type, $name) {
94
95
		if (!$this->configService->isCircleAllowed($type)) {
96
			throw new CircleTypeDisabledException();
97
		}
98
99
		$owner = new Member();
0 ignored issues
show
Bug introduced by
The call to Member::__construct() misses some required arguments starting with $circleId.
Loading history...
100
		$owner->setUserId($this->userId)
101
			  ->setStatus(Member::STATUS_MEMBER);
102
103
		$circle = new Circle();
104
		$circle->setName($name)
105
			   ->setType($type)
106
			   ->setMembers([$owner]);
107
108
		try {
109
			$this->dbService->getCirclesMapper()
110
							->create($circle, $owner);
111
			$this->dbService->getMembersMapper()
112
							->add($owner);
113
114
		} catch (\Exception $e) {
115
			$this->dbService->getCirclesMapper()
116
							->destroy($circle);
117
			throw $e;
118
		}
119
120
		return $circle;
121
	}
122
123
124
	/**
125
	 * list Circles depends on type (or all) and name (parts) and minimum level.
126
	 *
127
	 * @param $type
128
	 * @param string $name
129
	 * @param int $level
130
	 *
131
	 * @return array
132
	 * @throws CircleTypeDisabledException
133
	 */
134
	public function listCircles($type, $name = '', $level = 0) {
135
136
		if (!$this->configService->isCircleAllowed((int)$type)) {
137
			throw new CircleTypeDisabledException();
138
		}
139
140
		$result = $this->dbService->getCirclesMapper()
141
								  ->findCirclesByUser($this->userId, $type, $name, $level);
142
143
		$data = [];
144
		foreach ($result as $item) {
145
			$data[] = $item;
146
		}
147
148
		return $data;
149
	}
150
151
152
	/**
153
	 * returns details on circle and its members if this->userId is a member itself.
154
	 *
155
	 * @param $circleId
156
	 *
157
	 * @return Circle
158
	 * @throws \Exception
159
	 * @internal param $circleId
160
	 * @internal param string $iError
161
	 */
162
	public function detailsCircle($circleId) {
163
164
		try {
165
			$circle = $this->dbService->getCirclesMapper()
166
									  ->getDetailsFromCircle($this->userId, $circleId);
167
168
			if ($circle->getUser()
169
					   ->getLevel() >= Member::LEVEL_MEMBER
170
			) {
171
				$members = $this->dbService->getMembersMapper()
172
										   ->getMembersFromCircle(
173
											   $circleId, $circle->getUser()
174
										   );
175
				$circle->setMembers($members);
176
			}
177
		} catch (\Exception $e) {
178
			throw $e;
179
		}
180
181
		return $circle;
182
183
	}
184
185
	/**
186
	 * Join a circle.
187
	 *
188
	 * @param $circleId
189
	 *
190
	 * @return null|Member
191
	 * @throws \Exception
192
	 */
193
	public function joinCircle($circleId) {
194
195
		try {
196
			$circle = $this->dbService->getCirclesMapper()
197
									  ->getDetailsFromCircle(
198
										  $this->userId, $circleId
199
									  );
200
201
			try {
202
				$member = $this->dbService->getMembersMapper()
203
										  ->getMemberFromCircle(
204
											  $circle->getId(), $this->userId
205
										  );
206
			} catch (MemberDoesNotExistException $m) {
207
				$member = new Member($circle->getId(), $this->userId);
208
				$this->dbService->getMembersMapper()
209
								->add($member);
210
			}
211
212
			$member->hasToBeAbleToJoinTheCircle();
213
			$member->joinCircle($circle->getType());
214
			$this->dbService->getMembersMapper()
215
							->editMember($member);
216
217
		} catch (\Exception $e) {
218
			throw $e;
219
		}
220
221
		return $member;
222
	}
223
224
225
	/**
226
	 * Leave a circle.
227
	 *
228
	 * @param $circleId
229
	 *
230
	 * @return null|Member
231
	 * @throws \Exception
232
	 */
233
	public function leaveCircle($circleId) {
234
235
		try {
236
			$circle = $this->dbService->getCirclesMapper()
237
									  ->getDetailsFromCircle($this->userId, $circleId);
238
			$member = $this->dbService->getMembersMapper()
239
									  ->getMemberFromCircle($circle->getId(), $this->userId, false);
240
			$member->hasToBeMember();
241
			$member->cantBeOwner();
242
			$member->setStatus(Member::STATUS_NONMEMBER);
243
			$member->setLevel(Member::LEVEL_NONE);
244
			$this->dbService->getMembersMapper()
245
							->editMember($member);
246
		} catch (\Exception $e) {
247
			throw $e;
248
		}
249
250
		return $member;
251
	}
252
253
254
	/**
255
	 * destroy a circle.
256
	 *
257
	 * @param $circle
258
	 */
259
	public function removeCircle($circle) {
260
261
		$this->dbService->getMembersMapper()
262
						->removeAllFromCircle($circle);
263
		$this->dbService->getCirclesMapper()
264
						->destroy($circle);
265
	}
266
267
268
}