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

CirclesRequestBuilder::parseSharesSelectSql()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Db;
29
30
31
use Doctrine\DBAL\Query\QueryBuilder;
32
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
33
use OCA\Circles\Model\Circle;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Model\SharingFrame;
36
use OCA\Circles\Service\ConfigService;
37
use OCA\Circles\Service\MiscService;
38
use OCP\DB\QueryBuilder\IQueryBuilder;
39
use OCP\IDBConnection;
40
use OCP\IL10N;
41
42
class CirclesRequestBuilder extends CoreRequestBuilder {
43
44
45
	/** @var MembersRequest */
46
	protected $membersRequest;
47
48
	/**
49
	 * CirclesRequestBuilder constructor.
50
	 *
51
	 * {@inheritdoc}
52
	 * @param MembersRequest $membersRequest
53
	 */
54
	public function __construct(
55
		IL10N $l10n, IDBConnection $connection, MembersRequest $membersRequest,
56
		ConfigService $configService, MiscService $miscService
57
	) {
58
		parent::__construct($l10n, $connection, $configService, $miscService);
59
		$this->membersRequest = $membersRequest;
60
	}
61
62
63
	/**
64
	 * Left Join the Groups table
65
	 *
66
	 * @param IQueryBuilder $qb
67
	 * @param string $field
68
	 */
69
	protected function leftJoinGroups(IQueryBuilder &$qb, $field) {
70
		$expr = $qb->expr();
71
72
		$qb->leftJoin(
73
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
74
			$expr->eq($field, 'g.circle_id')
75
		);
76
	}
77
78
	/**
79
	 * Limit the search to a non-personal circle
80
	 *
81
	 * @param IQueryBuilder $qb
82
	 */
83
	protected function limitToNonPersonalCircle(IQueryBuilder &$qb) {
84
		$expr = $qb->expr();
85
86
		$qb->andWhere(
87
			$expr->neq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL))
88
		);
89
	}
90
91
92
	/**
93
	 * @param IQueryBuilder $qb
94
	 * @param string $circleUniqueId
95
	 * @param $userId
96
	 * @param $type
97
	 * @param $name
98
	 *
99
	 * @throws ConfigNoCircleAvailableException
100
	 */
101
	protected function limitRegardingCircleType(
102
		IQueryBuilder &$qb, $userId, $circleUniqueId, $type, $name
103
	) {
104
		$orTypes = $this->generateLimit($qb, $circleUniqueId, $userId, $type, $name);
105
		if (sizeof($orTypes) === 0) {
106
			throw new ConfigNoCircleAvailableException(
107
				$this->l10n->t(
108
					'You cannot use the Circles Application until your administrator has allowed at least one type of circles'
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 111 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
109
				)
110
			);
111
		}
112
113
		$orXTypes = $qb->expr()
114
					   ->orX();
115
		foreach ($orTypes as $orType) {
116
			$orXTypes->add($orType);
117
		}
118
119
		$qb->andWhere($orXTypes);
120
	}
121
122
123
	/**
124
	 * @param IQueryBuilder $qb
125
	 * @param string $circleUniqueId
126
	 * @param $userId
127
	 * @param $type
128
	 * @param $name
129
	 *
130
	 * @return array
131
	 */
132
	private function generateLimit(IQueryBuilder &$qb, $circleUniqueId, $userId, $type, $name) {
133
		$orTypes = [];
134
		array_push($orTypes, $this->generateLimitPersonal($qb, $userId, $type));
135
		array_push($orTypes, $this->generateLimitSecret($qb, $circleUniqueId, $type, $name));
136
		array_push($orTypes, $this->generateLimitClosed($qb, $type));
137
		array_push($orTypes, $this->generateLimitPublic($qb, $type));
138
139
		return array_filter($orTypes);
140
	}
141
142
143
	/**
144
	 * @param IQueryBuilder $qb
145
	 * @param int|string $userId
146
	 * @param int $type
147
	 *
148
	 * @return \OCP\DB\QueryBuilder\ICompositeExpression
149
	 */
150
	private function generateLimitPersonal(IQueryBuilder $qb, $userId, $type) {
151
		if (!(Circle::CIRCLES_PERSONAL & (int)$type)) {
152
			return null;
153
		}
154
		$expr = $qb->expr();
155
156
		/** @noinspection PhpMethodParametersCountMismatchInspection */
157
		return $expr->andX(
158
			$expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_PERSONAL)),
159
			$expr->eq('o.user_id', $qb->createNamedParameter((string)$userId))
160
		);
161
	}
162
163
164
	/**
165
	 * @param IQueryBuilder $qb
166
	 * @param string $circleUniqueId
167
	 * @param int $type
168
	 * @param string $name
169
	 *
170
	 * @return string
171
	 */
172
	private function generateLimitSecret(IQueryBuilder $qb, $circleUniqueId, $type, $name) {
173
		if (!(Circle::CIRCLES_SECRET & (int)$type)) {
174
			return null;
175
		}
176
		$expr = $qb->expr();
177
178
		$orX = $expr->orX($expr->gte('u.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)));
179
		$orX->add($expr->eq('c.name', $qb->createNamedParameter($name)))
180
			->add(
181
				$expr->eq(
182
					$qb->createNamedParameter($circleUniqueId),
183
					$qb->createFunction(
184
						'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
185
					)
186
				)
187
			);
188
189
		if ($this->leftJoinedNCGroupAndUser) {
190
			$orX->add($expr->gte('g.level', $qb->createNamedParameter(Member::LEVEL_MEMBER)));
191
		}
192
193
		/** @noinspection PhpMethodParametersCountMismatchInspection */
194
		$sqb = $expr->andX(
195
			$expr->eq('c.type', $qb->createNamedParameter(Circle::CIRCLES_SECRET)),
196
			$expr->orX($orX)
197
		);
198
199
		return $sqb;
200
	}
201
202
203
	/**
204
	 * @param IQueryBuilder $qb
205
	 * @param int $type
206
	 *
207
	 * @return string
208
	 */
209 View Code Duplication
	private function generateLimitClosed(IQueryBuilder $qb, $type) {
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...
210
		if (!(Circle::CIRCLES_CLOSED & (int)$type)) {
211
			return null;
212
		}
213
214
		return $qb->expr()
215
				  ->eq(
216
					  'c.type',
217
					  $qb->createNamedParameter(Circle::CIRCLES_CLOSED)
218
				  );
219
	}
220
221
222
	/**
223
	 * @param IQueryBuilder $qb
224
	 * @param int $type
225
	 *
226
	 * @return string
227
	 */
228 View Code Duplication
	private function generateLimitPublic(IQueryBuilder $qb, $type) {
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...
229
		if (!(Circle::CIRCLES_PUBLIC & (int)$type)) {
230
			return null;
231
		}
232
233
		return $qb->expr()
234
				  ->eq(
235
					  'c.type',
236
					  $qb->createNamedParameter(Circle::CIRCLES_PUBLIC)
237
				  );
238
	}
239
240
241
	/**
242
	 * add a request to the members list, using the current user ID.
243
	 * will returns level and stuff.
244
	 *
245
	 * @param IQueryBuilder $qb
246
	 * @param string $userId
247
	 */
248 View Code Duplication
	public function leftJoinUserIdAsViewer(IQueryBuilder &$qb, $userId) {
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...
249
250
		if ($qb->getType() !== QueryBuilder::SELECT) {
251
			return;
252
		}
253
254
		$expr = $qb->expr();
255
		$pf = '`' . $this->default_select_alias . '`.';
256
257
		/** @noinspection PhpMethodParametersCountMismatchInspection */
258
		$qb->selectAlias('u.user_id', 'viewer_userid')
259
		   ->selectAlias('u.status', 'viewer_status')
260
		   ->selectAlias('u.level', 'viewer_level')
261
		   ->leftJoin(
262
			   $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'u',
263
			   $expr->andX(
264
				   $expr->eq(
265
					   'u.circle_id',
266
					   $qb->createFunction(
267
						   'SUBSTR(' . $pf . '`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH
268
						   . ')'
269
					   )
270
				   ),
271
				   $expr->eq('u.user_id', $qb->createNamedParameter($userId)),
272
				   $expr->eq('u.user_type', $qb->createNamedParameter(Member::TYPE_USER))
273
			   )
274
		   );
275
	}
276
277
278
	/**
279
	 * Left Join members table to get the owner of the circle.
280
	 *
281
	 * @param IQueryBuilder $qb
282
	 */
283 View Code Duplication
	public function leftJoinOwner(IQueryBuilder &$qb) {
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...
284
285
		if ($qb->getType() !== QueryBuilder::SELECT) {
286
			return;
287
		}
288
289
		$expr = $qb->expr();
290
		$pf = '`' . $this->default_select_alias . '`.';
291
292
		/** @noinspection PhpMethodParametersCountMismatchInspection */
293
		$qb->selectAlias('o.user_id', 'owner_userid')
294
		   ->selectAlias('o.status', 'owner_status')
295
		   ->selectAlias('o.level', 'owner_level')
296
		   ->leftJoin(
297
			   $this->default_select_alias, CoreRequestBuilder::TABLE_MEMBERS, 'o',
298
			   $expr->andX(
299
				   $expr->eq(
300
					   $qb->createFunction(
301
						   'SUBSTR(' . $pf . '`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH
302
						   . ')'
303
					   )
304
					   , 'o.circle_id'
305
				   ),
306
				   $expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)),
307
				   $expr->eq('o.user_type', $qb->createNamedParameter(Member::TYPE_USER))
308
			   )
309
		   );
310
	}
311
312
313
	/**
314
	 * Left Join circle table to get more information about the circle.
315
	 *
316
	 * @param IQueryBuilder $qb
317
	 */
318
	public function leftJoinCircle(IQueryBuilder &$qb) {
319
320
		if ($qb->getType() !== QueryBuilder::SELECT) {
321
			return;
322
		}
323
324
		$expr = $qb->expr();
325
		$pf = $this->default_select_alias . '.';
326
327
		/** @noinspection PhpMethodParametersCountMismatchInspection */
328
		$qb->selectAlias('lc.type', 'circle_type')
329
		   ->selectAlias('lc.name', 'circle_name')
330
		   ->leftJoin(
331
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
332
			   $expr->eq(
333
				   $pf . 'circle_id',
334
				   $qb->createFunction(
335
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
336
				   )
337
			   )
338
		   );
339
	}
340
341
342
	/**
343
	 * Base of the Sql Insert request for Shares
344
	 *
345
	 *
346
	 * @return IQueryBuilder
347
	 */
348 View Code Duplication
	protected function getCirclesInsertSql() {
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...
349
		$qb = $this->dbConnection->getQueryBuilder();
350
		$qb->insert(self::TABLE_CIRCLES)
351
		   ->setValue('creation', $qb->createFunction('NOW()'));
352
353
		return $qb;
354
	}
355
356
357
	/**
358
	 * Base of the Sql Update request for Shares
359
	 *
360
	 * @param int $uniqueId
361
	 *
362
	 * @return IQueryBuilder
363
	 */
364 View Code Duplication
	protected function getCirclesUpdateSql($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...
365
		$qb = $this->dbConnection->getQueryBuilder();
366
		$qb->update(self::TABLE_CIRCLES)
367
		   ->where(
368
			   $qb->expr()
369
				  ->eq('unique_id', $qb->createNamedParameter($uniqueId))
370
		   );
371
372
		return $qb;
373
	}
374
375
376
	/**
377
	 * Base of the Sql Delete request
378
	 *
379
	 * @param string $circleUniqueId
380
	 *
381
	 * @return IQueryBuilder
382
	 */
383 View Code Duplication
	protected function getCirclesDeleteSql($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...
384
		$qb = $this->dbConnection->getQueryBuilder();
385
		$qb->delete(self::TABLE_CIRCLES)
386
		   ->where(
387
			   $qb->expr()
388
				  ->eq(
389
					  $qb->createFunction(
390
						  'SUBSTR(`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
391
					  ),
392
					  $qb->createNamedParameter($circleUniqueId)
393
				  )
394
		   );
395
396
		return $qb;
397
	}
398
399
400
	/**
401
	 * @return IQueryBuilder
402
	 */
403
	protected function getCirclesSelectSql() {
404
		$qb = $this->dbConnection->getQueryBuilder();
405
406
		/** @noinspection PhpMethodParametersCountMismatchInspection */
407
		$qb->selectDistinct('c.unique_id')
408
		   ->addSelect(
409
			   'c.id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation'
410
		   )
411
		   ->from(CoreRequestBuilder::TABLE_CIRCLES, 'c');
412
		$this->default_select_alias = 'c';
413
414
		return $qb;
415
	}
416
417
418
	/**
419
	 * @param array $data
420
	 *
421
	 * @return Circle
422
	 */
423
	protected function parseCirclesSelectSql($data) {
424
425
		$circle = new Circle();
426
		$circle->setId($data['id']);
427
		$circle->setUniqueId($data['unique_id']);
428
		$circle->setName($data['name']);
429
		$circle->setDescription($data['description']);
430
		$circle->setSettings($data['settings']);
431
		$circle->setType($data['type']);
432
		$circle->setCreation($data['creation']);
433
434 View Code Duplication
		if (key_exists('viewer_level', $data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
435
			$user = new Member($data['viewer_userid'], Member::TYPE_USER, $circle->getUniqueId());
436
			$user->setStatus($data['viewer_status']);
437
			$user->setLevel($data['viewer_level']);
438
			$circle->setViewer($user);
439
		}
440
441 View Code Duplication
		if (key_exists('owner_level', $data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
442
			$owner = new Member($data['owner_userid'], Member::TYPE_USER, $circle->getUniqueId());
443
			$owner->setStatus($data['owner_status']);
444
			$owner->setLevel($data['owner_level']);
445
			$circle->setOwner($owner);
446
		}
447
448
		return $circle;
449
	}
450
451
452
}