Completed
Push — master ( 216249...401e97 )
by Maxence
03:07
created

Circles::listCircles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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