Completed
Push — master ( ecb5b1...034882 )
by Maxence
05:31
created

CirclesRequest::forceGetCircles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
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
28
namespace OCA\Circles\Db;
29
30
31
use OCA\Circles\Exceptions\CircleAlreadyExistsException;
32
use OCA\Circles\Exceptions\CircleDoesNotExistException;
33
use OCA\Circles\Exceptions\FederatedLinkDoesNotExistException;
34
use OCA\Circles\Exceptions\SharingFrameDoesNotExistException;
35
use OCA\Circles\Model\Circle;
36
use OCA\Circles\Model\FederatedLink;
37
use OCA\Circles\Model\Member;
38
use OCA\Circles\Model\SharingFrame;
39
40
class CirclesRequest extends CirclesRequestBuilder {
41
42
43
	/**
44
	 * forceGetCircle();
45
	 *
46
	 * returns data of a circle from its Id.
47
	 *
48
	 * WARNING: This function does not filters data regarding the current user/viewer.
49
	 *          In case of interaction with users, Please use getCircle() instead.
50
	 *
51
	 * @param string $circleUniqueId
52
	 *
53
	 * @return Circle
54
	 * @throws CircleDoesNotExistException
55
	 */
56
	public function forceGetCircle($circleUniqueId) {
57
		$qb = $this->getCirclesSelectSql();
58
59
		$this->limitToShortenUniqueId($qb, $circleUniqueId, Circle::SHORT_UNIQUE_ID_LENGTH);
60
61
		$cursor = $qb->execute();
62
		$data = $cursor->fetch();
63
		$cursor->closeCursor();
64
65
		if ($data === false) {
66
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
67
		}
68
69
		$entry = $this->parseCirclesSelectSql($data);
70
71
		return $entry;
72
	}
73
74
75
	/**
76
	 * forceGetCircles();
77
	 *
78
	 * returns data of a all circles.
79
	 *
80
	 * WARNING: This function does not filters data regarding the current user/viewer.
81
	 *          In case of interaction with users, Please use getCircles() instead.
82
	 *
83
	 * @return Circle[]
84
	 */
85
	public function forceGetCircles() {
86
87
		$qb = $this->getCirclesSelectSql();
88
		$this->leftJoinOwner($qb);
89
90
		$circles = [];
91
		$cursor = $qb->execute();
92
		while ($data = $cursor->fetch()) {
93
			$circles[] = $this->parseCirclesSelectSql($data);
94
		}
95
		$cursor->closeCursor();
96
97
		return $circles;
98
	}
99
100
101
	/**
102
	 * forceGetCircleByName();
103
	 *
104
	 * returns data of a circle from its Name.
105
	 *
106
	 * WARNING: This function does not filters data regarding the current user/viewer.
107
	 *          In case of interaction with users, do not use this method.
108
	 *
109
	 * @param $name
110
	 *
111
	 * @return null|Circle
112
	 * @throws CircleDoesNotExistException
113
	 */
114
	public function forceGetCircleByName($name) {
115
116
		$qb = $this->getCirclesSelectSql();
117
118
		$this->limitToName($qb, $name);
119
120
		$cursor = $qb->execute();
121
		$data = $cursor->fetch();
122
		$cursor->closeCursor();
123
124
		if ($data === false) {
125
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
126
		}
127
128
		$entry = $this->parseCirclesSelectSql($data);
129
130
		return $entry;
131
	}
132
133
134
	/**
135
	 * @param string $userId
136
	 * @param int $type
137
	 * @param string $name
138
	 * @param int $level
139
	 *
140
	 * @return Circle[]
141
	 */
142
	public function getCircles($userId, $type = 0, $name = '', $level = 0) {
143
		if ($type === 0) {
144
			$type = Circle::CIRCLES_ALL;
145
		}
146
147
		$qb = $this->getCirclesSelectSql();
148
		$this->leftJoinUserIdAsViewer($qb, $userId);
149
		$this->leftJoinOwner($qb);
150
		$this->leftJoinNCGroupAndUser($qb, $userId, '`c`.`unique_id`');
151
152
		if ($level > 0) {
153
			$this->limitToLevel($qb, $level, ['u', 'g']);
154
		}
155
		$this->limitRegardingCircleType($qb, $userId, -1, $type, $name);
156
157
		$circles = [];
158
		$cursor = $qb->execute();
159
		while ($data = $cursor->fetch()) {
160
			if ($name === '' || stripos(strtolower($data['name']), strtolower($name)) !== false) {
161
				$circles[] = $this->parseCirclesSelectSql($data);
162
			}
163
		}
164
		$cursor->closeCursor();
165
166
		return $circles;
167
	}
168
169
170
	/**
171
	 *
172
	 * @param string $circleUniqueId
173
	 * @param string $viewerId
174
	 *
175
	 * @return Circle
176
	 * @throws CircleDoesNotExistException
177
	 */
178
	public function getCircle($circleUniqueId, $viewerId) {
179
		$qb = $this->getCirclesSelectSql();
180
181
		$this->limitToShortenUniqueId($qb, $circleUniqueId, Circle::SHORT_UNIQUE_ID_LENGTH);
182
183
		$this->leftJoinUserIdAsViewer($qb, $viewerId);
184
		$this->leftJoinOwner($qb);
185
		$this->leftJoinNCGroupAndUser($qb, $viewerId, '`c`.`unique_id`');
186
187
		$this->limitRegardingCircleType($qb, $viewerId, $circleUniqueId, Circle::CIRCLES_ALL, '');
188
189
		$cursor = $qb->execute();
190
		$data = $cursor->fetch();
191
		$cursor->closeCursor();
192
193
		if ($data === false) {
194
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
195
		}
196
197
		$circle = $this->parseCirclesSelectSql($data);
198
		$circle->setGroupViewer(
199
			$this->membersRequest->forceGetHigherLevelGroupFromUser($circleUniqueId, $viewerId)
200
		);
201
202
		return $circle;
203
	}
204
205
206
	/**
207
	 * createCircle();
208
	 *
209
	 * Create a circle with $userId as its owner.
210
	 * Will returns the circle
211
	 *
212
	 * @param Circle $circle
213
	 * @param $userId
214
	 *
215
	 * @throws CircleAlreadyExistsException
216
	 */
217
	public function createCircle(Circle &$circle, $userId) {
218
219
		if (!$this->isCircleUnique($circle, $userId)) {
220
			throw new CircleAlreadyExistsException(
221
				$this->l10n->t('A circle with that name exists')
222
			);
223
		}
224
225
		$circle->generateUniqueId();
226
		$qb = $this->getCirclesInsertSql();
227
		$qb->setValue('unique_id', $qb->createNamedParameter($circle->getUniqueId(true)))
228
		   ->setValue('name', $qb->createNamedParameter($circle->getName()))
229
		   ->setValue('description', $qb->createNamedParameter($circle->getDescription()))
230
		   ->setValue('settings', $qb->createNamedParameter($circle->getSettings(true)))
231
		   ->setValue('type', $qb->createNamedParameter($circle->getType()));
232
		$qb->execute();
233
234
		$owner = new Member($userId, Member::TYPE_USER);
235
		$owner->setCircleId($circle->getUniqueId())
236
			  ->setLevel(Member::LEVEL_OWNER)
237
			  ->setStatus(Member::STATUS_MEMBER);
238
		$circle->setOwner($owner)
239
			   ->setViewer($owner);
240
	}
241
242
243
	/**
244
	 * remove a circle
245
	 *
246
	 * @param string $circleUniqueId
247
	 */
248
	public function destroyCircle($circleUniqueId) {
249
		$qb = $this->getCirclesDeleteSql($circleUniqueId);
250
251
252
		$qb->execute();
253
	}
254
255
256
	/**
257
	 * returns if the circle is already in database
258
	 *
259
	 * @param Circle $circle
260
	 * @param string $userId
261
	 *
262
	 * @return bool
263
	 */
264
	private function isCircleUnique(Circle $circle, $userId) {
265
266
		if ($circle->getType() === Circle::CIRCLES_PERSONAL) {
267
			return $this->isPersonalCircleUnique($circle, $userId);
268
		}
269
270
		$qb = $this->getCirclesSelectSql();
271
		$this->limitToNonPersonalCircle($qb);
272
273
		$cursor = $qb->execute();
274
		while ($data = $cursor->fetch()) {
275
			if (strtolower($data['name']) === strtolower($circle->getName())) {
276
				return false;
277
			}
278
		}
279
		$cursor->closeCursor();
280
281
		return true;
282
	}
283
284
285
	/**
286
	 * return if the personal circle is unique
287
	 *
288
	 * @param Circle $circle
289
	 * @param string $userId
290
	 *
291
	 * @return bool
292
	 */
293
	private function isPersonalCircleUnique(Circle $circle, $userId) {
294
295
		$list = $this->getCircles(
296
			$userId, Circle::CIRCLES_PERSONAL, $circle->getName(),
297
			Member::LEVEL_OWNER
298
		);
299
300
		foreach ($list as $test) {
301
			if (strtolower($test->getName()) === strtolower($circle->getName())) {
302
				return false;
303
			}
304
		}
305
306
		return true;
307
	}
308
309
310
	/**
311
	 * saveFrame()
312
	 *
313
	 * Insert a new entry in the database to save the SharingFrame.
314
	 *
315
	 * @param SharingFrame $frame
316
	 */
317 View Code Duplication
	public function saveFrame(SharingFrame $frame) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
318
		$qb = $this->getSharesInsertSql();
319
		$circle = $frame->getCircle();
320
		$qb->setValue('circle_id', $qb->createNamedParameter($circle->getUniqueId()))
321
		   ->setValue('source', $qb->createNamedParameter($frame->getSource()))
322
		   ->setValue('type', $qb->createNamedParameter($frame->getType()))
323
		   ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true)))
324
		   ->setValue('author', $qb->createNamedParameter($frame->getAuthor()))
325
		   ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
326
		   ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
327
		   ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true)));
328
329
		$qb->execute();
330
	}
331
332
333 View Code Duplication
	public function updateFrame(SharingFrame $frame) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
334
		$qb = $this->getSharesUpdateSql($frame->getUniqueId());
335
		$circle = $frame->getCircle();
336
		$qb->set('circle_id', $qb->createNamedParameter($circle->getUniqueId()))
337
		   ->set('source', $qb->createNamedParameter($frame->getSource()))
338
		   ->set('type', $qb->createNamedParameter($frame->getType()))
339
		   ->set('headers', $qb->createNamedParameter($frame->getHeaders(true)))
340
		   ->set('author', $qb->createNamedParameter($frame->getAuthor()))
341
		   ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
342
		   ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
343
		   ->set('payload', $qb->createNamedParameter($frame->getPayload(true)));
344
345
		$qb->execute();
346
	}
347
348
349
	public function updateCircle(Circle $circle) {
350
		$qb = $this->getCirclesUpdateSql($circle->getUniqueId(true));
351
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
352
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
353
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
354
355
		$qb->execute();
356
	}
357
358
359
	/**
360
	 * @param string $uniqueId
361
	 *
362
	 * @return Circle
363
	 * @throws CircleDoesNotExistException
364
	 */
365 View Code Duplication
	public function getCircleFromUniqueId($uniqueId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
		$qb = $this->getCirclesSelectSql();
367
		$this->limitToUniqueId($qb, (string)$uniqueId);
368
369
		$cursor = $qb->execute();
370
		$data = $cursor->fetch();
371
		$cursor->closeCursor();
372
373
		if ($data === false) {
374
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
375
		}
376
377
		$entry = $this->parseCirclesSelectSql($data);
378
379
		return $entry;
380
	}
381
382
383
	/**
384
	 * @param string $circleUniqueId
385
	 * @param string $frameUniqueId
386
	 *
387
	 * @return SharingFrame
388
	 * @throws SharingFrameDoesNotExistException
389
	 */
390 View Code Duplication
	public function getFrame($circleUniqueId, $frameUniqueId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
391
		$qb = $this->getSharesSelectSql();
392
		$this->limitToUniqueId($qb, $frameUniqueId);
393
		$this->limitToCircleId($qb, $circleUniqueId);
394
		$this->leftJoinCircle($qb);
395
396
		$cursor = $qb->execute();
397
		$data = $cursor->fetch();
398
		$cursor->closeCursor();
399
400
		if ($data === false) {
401
			throw new SharingFrameDoesNotExistException($this->l10n->t('Sharing Frame does not exist'));
402
		}
403
404
		$entry = $this->parseSharesSelectSql($data);
405
406
		return $entry;
407
	}
408
409
410
}