Completed
Pull Request — master (#139)
by Maxence
02:40
created

CirclesRequest::saveFrame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
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 View Code Duplication
	public function forceGetCircle($circleUniqueId) {
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...
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
				return false;
273
			}
274
		}
275
		$cursor->closeCursor();
276
277
		return true;
278
	}
279
280
281
	/**
282
	 * return if the personal circle is unique
283
	 *
284
	 * @param Circle $circle
285
	 * @param string $userId
286
	 *
287
	 * @return bool
288
	 */
289
	private function isPersonalCircleUnique(Circle $circle, $userId) {
290
291
		$list = $this->getCircles(
292
			$userId, Circle::CIRCLES_PERSONAL, $circle->getName(),
293
			Member::LEVEL_OWNER
294
		);
295
296
		foreach ($list as $test) {
297
			if (strtolower($test->getName()) === strtolower($circle->getName())) {
298
				return false;
299
			}
300
		}
301
302
		return true;
303
	}
304
305
306
307
308
	public function updateCircle(Circle $circle) {
309
		$qb = $this->getCirclesUpdateSql($circle->getUniqueId(true));
310
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
311
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
312
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
313
314
		$qb->execute();
315
	}
316
317
318
	/**
319
	 * @param string $uniqueId
320
	 *
321
	 * @return Circle
322
	 * @throws CircleDoesNotExistException
323
	 */
324 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...
325
		$qb = $this->getCirclesSelectSql();
326
		$this->limitToUniqueId($qb, (string)$uniqueId);
327
328
		$cursor = $qb->execute();
329
		$data = $cursor->fetch();
330
		$cursor->closeCursor();
331
332
		if ($data === false) {
333
			throw new CircleDoesNotExistException($this->l10n->t('Circle not found'));
334
		}
335
336
		$entry = $this->parseCirclesSelectSql($data);
337
338
		return $entry;
339
	}
340
341
342
343
344
}