Completed
Push — master ( 1990ee...212490 )
by Maxence
04:38 queued 01:45
created

CoreRequestBuilder::limitToShortenUniqueId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: maxence
5
 * Date: 7/4/17
6
 * Time: 5:01 PM
7
 */
8
9
namespace OCA\Circles\Db;
10
11
12
use OCA\Circles\Model\Circle;
13
use OCA\Circles\Model\Member;
14
use OCA\Circles\Service\ConfigService;
15
use OCP\DB\QueryBuilder\IQueryBuilder;
16
use Doctrine\DBAL\Query\QueryBuilder;
17
use OCA\Circles\Service\MiscService;
18
use OCP\IDBConnection;
19
use OCP\IL10N;
20
21
class CoreRequestBuilder {
0 ignored issues
show
Coding Style introduced by
The property $default_select_alias is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
22
23
	const TABLE_CIRCLES = 'circles_circles';
24
	const TABLE_MEMBERS = 'circles_members';
25
	const TABLE_GROUPS = 'circles_groups';
26
	const TABLE_SHARES = 'circles_shares';
27
	const TABLE_LINKS = 'circles_links';
28
29
	const NC_TABLE_GROUP_USER = 'group_user';
30
31
	/** @var IDBConnection */
32
	protected $dbConnection;
33
34
	/** @var IL10N */
35
	protected $l10n;
36
37
	/** @var ConfigService */
38
	protected $configService;
39
40
	/** @var MiscService */
41
	protected $miscService;
42
43
	/** @var string */
44
	protected $default_select_alias;
45
46
	/** @var bool */
47
	protected $leftJoinedNCGroupAndUser = false;
48
49
	/**
50
	 * RequestBuilder constructor.
51
	 *
52
	 * @param IL10N $l10n
53
	 * @param IDBConnection $connection
54
	 * @param ConfigService $configService
55
	 * @param MiscService $miscService
56
	 */
57
	public function __construct(
58
		IL10N $l10n, IDBConnection $connection, ConfigService $configService,
59
		MiscService $miscService
60
	) {
61
		$this->l10n = $l10n;
62
		$this->dbConnection = $connection;
63
		$this->configService = $configService;
64
		$this->miscService = $miscService;
65
	}
66
67
68
	/**
69
	 * Limit the request by its Id.
70
	 *
71
	 * @param IQueryBuilder $qb
72
	 * @param int $id
73
	 */
74
	protected function limitToId(IQueryBuilder &$qb, $id) {
75
		$this->limitToDBField($qb, 'id', $id);
76
	}
77
78
79
	/**
80
	 * Limit the request by its UniqueId.
81
	 *
82
	 * @param IQueryBuilder $qb
83
	 * @param int $uniqueId
84
	 */
85
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
86
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
87
	}
88
89
90
	/**
91
	 * Limit the request by its Token.
92
	 *
93
	 * @param IQueryBuilder $qb
94
	 * @param string $token
95
	 */
96
	protected function limitToToken(IQueryBuilder &$qb, $token) {
97
		$this->limitToDBField($qb, 'token', $token);
98
	}
99
100
101
	/**
102
	 * Limit the request to the User by its Id.
103
	 *
104
	 * @param IQueryBuilder $qb
105
	 * @param $userId
106
	 */
107
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
108
		$this->limitToDBField($qb, 'user_id', $userId);
109
	}
110
111
112
	/**
113
	 * Limit the request to the Type entry.
114
	 *
115
	 * @param IQueryBuilder $qb
116
	 * @param int $type
117
	 */
118
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
119
		$this->limitToDBField($qb, 'user_type', $type);
120
	}
121
122
123
	/**
124
	 * Limit the request to the Circle by its Id.
125
	 *
126
	 * @param IQueryBuilder $qb
127
	 * @param string $circleUniqueId
128
	 */
129
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
130
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
131
	}
132
133
134
	/**
135
	 * Limit the request to the Circle by its Shorten Unique Id.
136
	 *
137
	 * @param IQueryBuilder $qb
138
	 * @param string $circleUniqueId
139
	 * @param $length
140
	 */
141
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
142
		$expr = $qb->expr();
143
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
144
145
		$qb->andWhere(
146
			$expr->eq(
147
				$qb->createNamedParameter($circleUniqueId),
148
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
149
			)
150
		);
151
152
	}
153
154
155
	/**
156
	 * Limit the request to the Group by its Id.
157
	 *
158
	 * @param IQueryBuilder $qb
159
	 * @param int $groupId
160
	 */
161
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
162
		$this->limitToDBField($qb, 'group_id', $groupId);
163
	}
164
165
166
	/**
167
	 * Limit the search by its Name
168
	 *
169
	 * @param IQueryBuilder $qb
170
	 * @param string $name
171
	 */
172
	protected function limitToName(IQueryBuilder &$qb, $name) {
173
		$this->limitToDBField($qb, 'name', $name);
174
	}
175
176
177
	/**
178
	 * Limit the search by its Status (or greater)
179
	 *
180
	 * @param IQueryBuilder $qb
181
	 * @param string $name
182
	 */
183
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
184
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
185
	}
186
187
188
	/**
189
	 * Limit the request to a minimum member level.
190
	 *
191
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
192
	 *
193
	 * @param IQueryBuilder $qb
194
	 * @param int $level
195
	 * @param string|array $pf
196
	 */
197
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
198
		$expr = $qb->expr();
199
200
		if ($pf === '') {
201
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
202
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
203
204
			return;
205
		}
206
207
		if (!is_array($pf)) {
208
			$pf = [$pf];
209
		}
210
211
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
212
		$qb->andWhere($orX);
213
	}
214
215
216
	/**
217
	 * @param IQueryBuilder $qb
218
	 * @param array $pf
219
	 *
220
	 * @return mixed
221
	 */
222
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
223
		$expr = $qb->expr();
224
		$orX = $expr->orX();
225
226
		foreach ($pf as $p) {
227
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
228
				continue;
229
			}
230
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
231
		}
232
233
		return $orX;
234
	}
235
236
237
	/**
238
	 * Limit the search to Members and Almost members
239
	 *
240
	 * @param IQueryBuilder $qb
241
	 */
242
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
243
		$expr = $qb->expr();
244
245
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
246
247
		$orX = $expr->orX();
248
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
249
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
250
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
251
252
		$qb->andWhere($orX);
253
	}
254
255
256
	/**
257
	 * @param IQueryBuilder $qb
258
	 * @param string $field
259
	 * @param string|integer $value
260
	 */
261
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
262
		$expr = $qb->expr();
263
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
264
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
265
	}
266
267
268
	/**
269
	 * @param IQueryBuilder $qb
270
	 * @param string $field
271
	 * @param string|integer $value
272
	 */
273
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
274
		$expr = $qb->expr();
275
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
276
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
277
	}
278
279
280
	/**
281
	 * link to the groupId/UserId of the NC DB.
282
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
283
	 * alias
284
	 *
285
	 * @param IQueryBuilder $qb
286
	 * @param string $userId
287
	 */
288
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
289
		$expr = $qb->expr();
290
291
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
292
293
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
294
		if ($userId !== '') {
295
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
296
		} else {
297
			$qb->selectAlias('ncgu.uid', 'user_id');
298
		}
299
300
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
301
		$qb->andWhere($and);
302
	}
303
304
305
	/**
306
	 * Right Join the Circles table
307
	 *
308
	 * @param IQueryBuilder $qb
309
	 *
310
	 * @deprecated not used (14/07/17)
311
	 */
312
	protected function rightJoinCircles(IQueryBuilder &$qb) {
313
		$expr = $qb->expr();
314
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
315
316
		$qb->from(self::TABLE_CIRCLES, 'c')
317
		   ->andWhere(
318
			   $expr->eq(
319
				   $pf . 'circle_id',
320
				   $qb->createFunction(
321
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
322
				   )
323
			   )
324
		   );
325
	}
326
327
328
329
	/**
330
	 * Left Join circle table to get more information about the circle.
331
	 *
332
	 * @param IQueryBuilder $qb
333
	 */
334
	protected function leftJoinCircle(IQueryBuilder &$qb) {
335
336
		if ($qb->getType() !== QueryBuilder::SELECT) {
337
			return;
338
		}
339
340
		$expr = $qb->expr();
341
		$pf = $this->default_select_alias . '.';
342
343
		/** @noinspection PhpMethodParametersCountMismatchInspection */
344
		$qb->selectAlias('lc.type', 'circle_type')
345
		   ->selectAlias('lc.name', 'circle_name')
346
		   ->leftJoin(
347
			   $this->default_select_alias, CoreRequestBuilder::TABLE_CIRCLES, 'lc',
348
			   $expr->eq(
349
				   $pf . 'circle_id',
350
				   $qb->createFunction(
351
					   'SUBSTR(`lc`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
352
				   )
353
			   )
354
		   );
355
	}
356
357
358
359
	/**
360
	 * link to the groupId/UserId of the NC DB.
361
	 *
362
	 * @param IQueryBuilder $qb
363
	 * @param string $userId
364
	 * @param string $field
365
	 */
366
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
367
		if (!$this->configService->isLinkedGroupsAllowed()) {
368
			return;
369
		}
370
371
		$expr = $qb->expr();
372
		$qb->leftJoin(
373
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
374
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
375
		);
376
377
		/** @noinspection PhpMethodParametersCountMismatchInspection */
378
		$qb->leftJoin(
379
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
380
			$expr->andX(
381
				$expr->eq('ncgu.gid', 'g.group_id'),
382
				$expr->eq(
383
					'g.circle_id', $qb->createFunction(
384
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
385
				)
386
				)
387
			)
388
		);
389
390
		$this->leftJoinedNCGroupAndUser = true;
391
	}
392
}
393
394
395
396