Completed
Push — some-scrutinizing ( b2f84c...29b6ab )
by Maxence
23:41
created

CirclesService::joinCircle()   A

Complexity

Conditions 3
Paths 12

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
cc 3
eloc 14
nc 12
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\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
96
		if (!$this->configService->isCircleAllowed($type)) {
97
			throw new CircleTypeDisabledException();
98
		}
99
100
		$owner = new Member($this->userId);
101
		$owner->setStatus(Member::STATUS_MEMBER);
102
		$circle = new Circle($type, $name);
103
		$circle->setMembers([$owner]);
104
105
		try {
106
			$this->dbCircles->create($circle, $owner);
107
			$this->dbMembers->add($owner);
108
		} catch (\Exception $e) {
109
			$this->dbCircles->destroy($circle);
110
			throw $e;
111
		}
112
113
		return $circle;
114
	}
115
116
117
	/**
118
	 * list Circles depends on type (or all) and name (parts) and minimum level.
119
	 *
120
	 * @param $type
121
	 * @param string $name
122
	 * @param int $level
123
	 *
124
	 * @return array
125
	 * @throws CircleTypeDisabledException
126
	 */
127
	public function listCircles($type, $name = '', $level = 0) {
128
129
		if (!$this->configService->isCircleAllowed((int)$type)) {
130
			throw new CircleTypeDisabledException();
131
		}
132
133
		$result = $this->dbCircles->findCirclesByUser($this->userId, $type, $name, $level);
134
135
		$data = [];
136
		foreach ($result as $item) {
137
			$data[] = $item;
138
		}
139
140
		return $data;
141
	}
142
143
144
	/**
145
	 * returns details on circle and its members if this->userId is a member itself.
146
	 *
147
	 * @param $circleId
148
	 *
149
	 * @return Circle
150
	 * @throws \Exception
151
	 * @internal param $circleId
152
	 * @internal param string $iError
153
	 */
154
	public function detailsCircle($circleId) {
155
156
		try {
157
			$circle = $this->dbCircles->getDetailsFromCircle($this->userId, $circleId);
158
			if ($circle->getUser()
159
					   ->isMember()
160
			) {
161
				$members = $this->dbMembers->getMembersFromCircle(
162
					$circleId, $circle->getUser()
163
				);
164
				$circle->setMembers($members);
165
			}
166
		} catch (\Exception $e) {
167
			throw $e;
168
		}
169
170
		return $circle;
171
172
	}
173
174
	/**
175
	 * Join a circle.
176
	 *
177
	 * @param $circleId
178
	 *
179
	 * @return null|Member
180
	 * @throws \Exception
181
	 */
182
	public function joinCircle($circleId) {
183
184
		try {
185
			$circle = $this->dbCircles->getDetailsFromCircle($this->userId, $circleId);
186
187
			try {
188
				$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId);
189
			} catch (MemberDoesNotExistException $m) {
190
				$member = new Member($this->userId, $circle->getId());
191
				$this->dbMembers->add($member);
192
			}
193
194
			$member->hasToBeAbleToJoinTheCircle();
195
			$member->joinCircle($circle->getType());
196
			$this->dbMembers->editMember($member);
197
198
		} catch (\Exception $e) {
199
			throw $e;
200
		}
201
202
		return $member;
203
	}
204
205
206
	/**
207
	 * Leave a circle.
208
	 *
209
	 * @param $circleId
210
	 *
211
	 * @return null|Member
212
	 * @throws \Exception
213
	 */
214
	public function leaveCircle($circleId) {
215
216
		try {
217
			$circle = $this->dbCircles->getDetailsFromCircle($this->userId, $circleId);
218
			$member = $this->dbMembers->getMemberFromCircle($circle->getId(), $this->userId, false);
219
			$member->hasToBeMember();
220
			$member->cantBeOwner();
221
			$member->setStatus(Member::STATUS_NONMEMBER);
222
			$member->setLevel(Member::LEVEL_NONE);
223
			$this->dbMembers->editMember($member);
224
225
		} catch (\Exception $e) {
226
			throw $e;
227
		}
228
229
		return $member;
230
	}
231
232
233
	/**
234
	 * destroy a circle.
235
	 *
236
	 * @param $circle
237
	 */
238
	public function removeCircle($circle) {
239
		$this->dbMembers->removeAllFromCircle($circle);
240
		$this->dbCircles->destroy($circle);
241
	}
242
243
244
}