Completed
Push — master ( 83eee3...69619d )
by Maxence
03:04
created

Circles   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 237
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getContainer() 0 5 1
A createCircle() 0 6 1
A joinCircle() 0 6 1
A leaveCircle() 0 6 1
A listCircles() 0 6 1
A detailsCircle() 0 6 1
A destroyCircle() 0 6 1
A addMember() 0 6 1
A removeMember() 0 6 1
A levelMember() 0 6 1
A shareToCircle() 0 12 1
A linkCircle() 0 6 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\Api\v1;
28
29
30
use OCA\Circles\AppInfo\Application;
31
use OCA\Circles\Model\Circle;
32
use OCA\Circles\Model\FederatedLink;
33
use OCA\Circles\Model\Member;
34
use OCA\Circles\Model\SharingFrame;
35
36
class Circles {
37
38
39
	protected static function getContainer() {
40
		$app = new Application();
41
42
		return $app->getContainer();
43
	}
44
45
46
	/**
47
	 * Circles::createCircle();
48
	 *
49
	 * Create a new circle and make the current user its owner.
50
	 * You must specify type and name. type is one of this value:
51
	 *
52
	 * CIRCLES_PERSONAL is 1 or 'personal'
53
	 * CIRCLES_HIDDEN is 2 or 'hidden'
54
	 * CIRCLES_PRIVATE is 4 or 'private'
55
	 * CIRCLES_PUBLIC is 8 or 'public'
56
	 *
57
	 * @param $type
58
	 * @param $name
59
	 *
60
	 * @return Circle
61
	 */
62
	public static function createCircle($type, $name) {
63
		$c = self::getContainer();
64
65
		return $c->query('CirclesService')
66
				 ->createCircle($type, $name);
67
	}
68
69
70
	/**
71
	 * Circles::joinCircle();
72
	 *
73
	 * This function will make the current user joining a circle identified by its Id.
74
	 *
75
	 * @param $circleId
76
	 *
77
	 * @return Member
78
	 */
79
	public static function joinCircle($circleId) {
80
		$c = self::getContainer();
81
82
		return $c->query('CirclesService')
83
				 ->joinCircle($circleId);
84
	}
85
86
87
	/**
88
	 * Circles::leaveCircle();
89
	 *
90
	 * This function will make the current user leaving the circle identified by its Id. Will fail
91
	 * if user is the owner of the circle.
92
	 *
93
	 * @param $circleId
94
	 *
95
	 * @return Member
96
	 */
97
	public static function leaveCircle($circleId) {
98
		$c = self::getContainer();
99
100
		return $c->query('CirclesService')
101
				 ->joinCircle($circleId);
102
	}
103
104
105
	/**
106
	 * Circles::listCircles();
107
	 *
108
	 * This function list all circles fitting a search regarding its name and the level and the
109
	 * rights from the current user. In case of Hidden circle, name needs to be complete so the
110
	 * circle is included in the list (or if the current user is the owner)
111
	 *
112
	 * example: Circles::listCircles(Circle::CIRCLES_ALL, '', 8, callback); will returns all
113
	 * circles when the current user is at least an Admin.
114
	 *
115
	 * @param $type
116
	 * @param string $name
117
	 * @param int $level
118
	 *
119
	 * @return Circle[]
120
	 */
121
	public static function listCircles($type, $name = '', $level = 0) {
122
		$c = self::getContainer();
123
124
		return $c->query('CirclesService')
125
				 ->listCircles($type, $name, $level);
126
	}
127
128
129
	/**
130
	 * Circles::detailsCircle();
131
	 *
132
	 * Returns details on the circle. If the current user is a member, the members list will be
133
	 * return as well.
134
	 *
135
	 * @param $circleId
136
	 *
137
	 * @return Circle
138
	 */
139
	public static function detailsCircle($circleId) {
140
		$c = self::getContainer();
141
142
		return $c->query('CirclesService')
143
				 ->detailsCircle($circleId);
144
	}
145
146
147
	/**
148
	 * Circles::destroyCircle();
149
	 *
150
	 * This function will destroy the circle if the current user is the Owner.
151
	 *
152
	 * @param $circleId
153
	 *
154
	 * @return mixed
155
	 */
156
	public static function destroyCircle($circleId) {
157
		$c = self::getContainer();
158
159
		return $c->query('CirclesService')
160
				 ->removeCircle($circleId);
161
	}
162
163
164
	/**
165
	 * Circles::addMember();
166
	 *
167
	 * This function will add a user as member of the circle. Current user need at least to be
168
	 * Moderator.
169
	 *
170
	 * @param $circleId
171
	 * @param $userId
172
	 *
173
	 * @return Member[]
174
	 */
175
	public static function addMember($circleId, $userId) {
176
		$c = self::getContainer();
177
178
		return $c->query('MembersService')
179
				 ->addMember($circleId, $userId);
180
	}
181
182
183
	/**
184
	 * Circles::removeMember();
185
	 *
186
	 * This function will remove a member from the circle. Current user needs to be at least
187
	 * Moderator and have a higher level that the targeted member.
188
	 *
189
	 * @param $circleId
190
	 * @param $userId
191
	 *
192
	 * @return Member[]
193
	 */
194
	public static function removeMember($circleId, $userId) {
195
		$c = self::getContainer();
196
197
		return $c->query('MembersService')
198
				 ->removeMember($circleId, $userId);
199
	}
200
201
202
	/**
203
	 * Circles::levelMember();
204
	 *
205
	 * Edit the level of a member of the circle. The current level of the target needs to be lower
206
	 * than the user that initiate the process (ie. the current user). The new level of the target
207
	 * cannot be the same than the current level of the user that initiate the process (ie. the
208
	 * current user).
209
	 *
210
	 * @param $circleId
211
	 * @param $userId
212
	 * @param $level
213
	 *
214
	 * @return Member[]
215
	 */
216
	public static function levelMember($circleId, $userId, $level) {
217
		$c = self::getContainer();
218
219
		return $c->query('MembersService')
220
				 ->levelMember($circleId, $userId, $level);
221
	}
222
223
224
	/**
225
	 * Circles::shareToCircle();
226
	 *
227
	 * This function will share an item (array) to the circle identified by its Id.
228
	 * Source is the app that is sharing the item and type can be used by the app to identified the
229
	 * payload.
230
	 *
231
	 * @param $circleId
232
	 * @param $source
233
	 * @param $type
234
	 * @param array $payload
235
	 * @param $broadcaster
236
	 *
237
	 * @return mixed
238
	 */
239
	public static function shareToCircle(
240
		$circleId, $source, $type, array $payload, $broadcaster
241
	) {
242
		$c = self::getContainer();
243
244
		$frame = new SharingFrame((string)$source, (string)$type);
245
		$frame->setCircleId((int)$circleId);
246
		$frame->setPayload($payload);
247
248
		return $c->query('SharesService')
249
				 ->createFrame($frame, (string)$broadcaster);
250
	}
251
252
253
	/**
254
	 * Circles::linkCircle();
255
	 *
256
	 * Initiate a link procedure. Current user must be at least Admin of the circle.
257
	 * circleId is the local circle and remote is the target for the link.
258
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
259
	 *
260
	 * @param $circleId
261
	 * @param $remote
262
	 *
263
	 * @return FederatedLink
264
	 */
265
	public static function linkCircle($circleId, $remote) {
266
		$c = self::getContainer();
267
268
		return $c->query('FederatedService')
269
				 ->linkCircle($circleId, $remote);
270
	}
271
272
}