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

CirclesRequestBuilder::getSharesUpdateSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
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
	/**
315
	 * Base of the Sql Insert request for Shares
316
	 *
317
	 *
318
	 * @return IQueryBuilder
319
	 */
320 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...
321
		$qb = $this->dbConnection->getQueryBuilder();
322
		$qb->insert(self::TABLE_CIRCLES)
323
		   ->setValue('creation', $qb->createFunction('NOW()'));
324
325
		return $qb;
326
	}
327
328
329
	/**
330
	 * Base of the Sql Update request for Shares
331
	 *
332
	 * @param int $uniqueId
333
	 *
334
	 * @return IQueryBuilder
335
	 */
336 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...
337
		$qb = $this->dbConnection->getQueryBuilder();
338
		$qb->update(self::TABLE_CIRCLES)
339
		   ->where(
340
			   $qb->expr()
341
				  ->eq('unique_id', $qb->createNamedParameter($uniqueId))
342
		   );
343
344
		return $qb;
345
	}
346
347
348
	/**
349
	 * Base of the Sql Delete request
350
	 *
351
	 * @param string $circleUniqueId
352
	 *
353
	 * @return IQueryBuilder
354
	 */
355 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...
356
		$qb = $this->dbConnection->getQueryBuilder();
357
		$qb->delete(self::TABLE_CIRCLES)
358
		   ->where(
359
			   $qb->expr()
360
				  ->eq(
361
					  $qb->createFunction(
362
						  'SUBSTR(`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
363
					  ),
364
					  $qb->createNamedParameter($circleUniqueId)
365
				  )
366
		   );
367
368
		return $qb;
369
	}
370
371
372
	/**
373
	 * @return IQueryBuilder
374
	 */
375
	protected function getCirclesSelectSql() {
376
		$qb = $this->dbConnection->getQueryBuilder();
377
378
		/** @noinspection PhpMethodParametersCountMismatchInspection */
379
		$qb->selectDistinct('c.unique_id')
380
		   ->addSelect(
381
			   'c.id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation'
382
		   )
383
		   ->from(CoreRequestBuilder::TABLE_CIRCLES, 'c');
384
		$this->default_select_alias = 'c';
385
386
		return $qb;
387
	}
388
389
390
	/**
391
	 * @param array $data
392
	 *
393
	 * @return Circle
394
	 */
395
	protected function parseCirclesSelectSql($data) {
396
397
		$circle = new Circle();
398
		$circle->setId($data['id']);
399
		$circle->setUniqueId($data['unique_id']);
400
		$circle->setName($data['name']);
401
		$circle->setDescription($data['description']);
402
		$circle->setSettings($data['settings']);
403
		$circle->setType($data['type']);
404
		$circle->setCreation($data['creation']);
405
406 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...
407
			$user = new Member($data['viewer_userid'], Member::TYPE_USER, $circle->getUniqueId());
408
			$user->setStatus($data['viewer_status']);
409
			$user->setLevel($data['viewer_level']);
410
			$circle->setViewer($user);
411
		}
412
413 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...
414
			$owner = new Member($data['owner_userid'], Member::TYPE_USER, $circle->getUniqueId());
415
			$owner->setStatus($data['owner_status']);
416
			$owner->setLevel($data['owner_level']);
417
			$circle->setOwner($owner);
418
		}
419
420
		return $circle;
421
	}
422
423
424
}