Completed
Push — master ( e1a7c6...450dea )
by Maxence
02:34
created

CirclesRequest::getCircleFromUniqueId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

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