Completed
Push — master ( f9a32b...1fc0d2 )
by Maxence
04:23
created

CoreRequestBuilder::limitToStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
	 * @internal param int $circleId
108
	 */
109
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
110
		$this->limitToDBField($qb, 'user_id', $userId);
111
	}
112
113
114
	/**
115
	 * Limit the request to the Type entry.
116
	 *
117
	 * @param IQueryBuilder $qb
118
	 * @param int $type
119
	 *
120
	 * @internal param int $circleId
121
	 */
122
	protected function limitToUserType(IQueryBuilder &$qb, $type) {
123
		$this->limitToDBField($qb, 'user_type', $type);
124
	}
125
126
127
	/**
128
	 * Limit the request to the Circle by its Id.
129
	 *
130
	 * @param IQueryBuilder $qb
131
	 * @param string $circleUniqueId
132
	 */
133
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
134
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
135
	}
136
137
138
	/**
139
	 * Limit the request to the Circle by its Shorten Unique Id.
140
	 *
141
	 * @param IQueryBuilder $qb
142
	 * @param string $circleUniqueId
143
	 * @param $length
144
	 */
145
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId, $length) {
146
		$expr = $qb->expr();
147
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
148
149
		$qb->andWhere(
150
			$expr->eq(
151
				$qb->createNamedParameter($circleUniqueId),
152
				$qb->createFunction('SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . $length . ')')
153
			)
154
		);
155
156
	}
157
158
159
	/**
160
	 * Limit the request to the Group by its Id.
161
	 *
162
	 * @param IQueryBuilder $qb
163
	 * @param int $groupId
164
	 */
165
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
166
		$this->limitToDBField($qb, 'group_id', $groupId);
167
	}
168
169
170
	/**
171
	 * Limit the search by its Name
172
	 *
173
	 * @param IQueryBuilder $qb
174
	 * @param string $name
175
	 */
176
	protected function limitToName(IQueryBuilder &$qb, $name) {
177
		$this->limitToDBField($qb, 'name', $name);
178
	}
179
180
181
	/**
182
	 * Limit the search by its Status (or greater)
183
	 *
184
	 * @param IQueryBuilder $qb
185
	 * @param string $name
186
	 */
187
	protected function limitToStatus(IQueryBuilder &$qb, $name) {
188
		$this->limitToDBFieldOrGreater($qb, 'status', $name);
189
	}
190
191
192
	/**
193
	 * Limit the request to a minimum member level.
194
	 *
195
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
196
	 *
197
	 * @param IQueryBuilder $qb
198
	 * @param int $level
199
	 * @param string|array $pf
200
	 */
201
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
202
		$expr = $qb->expr();
203
204
		if ($pf === '') {
205
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
206
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
207
208
			return;
209
		}
210
211
		if (!is_array($pf)) {
212
			$pf = [$pf];
213
		}
214
215
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
216
		$qb->andWhere($orX);
217
	}
218
219
220
	/**
221
	 * @param IQueryBuilder $qb
222
	 * @param array $pf
223
	 *
224
	 * @return mixed
225
	 */
226
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
227
		$expr = $qb->expr();
228
		$orX = $expr->orX();
229
230
		foreach ($pf as $p) {
231
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
232
				continue;
233
			}
234
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
235
		}
236
237
		return $orX;
238
	}
239
240
241
	/**
242
	 * Limit the search to Members and Almost members
243
	 *
244
	 * @param IQueryBuilder $qb
245
	 */
246
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
247
		$expr = $qb->expr();
248
249
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
250
251
		$orX = $expr->orX();
252
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
253
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
254
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
255
256
		$qb->andWhere($orX);
257
	}
258
259
260
	/**
261
	 * @param IQueryBuilder $qb
262
	 * @param string $field
263
	 * @param string|integer $value
264
	 */
265
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
266
		$expr = $qb->expr();
267
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
268
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
269
	}
270
271
272
	/**
273
	 * @param IQueryBuilder $qb
274
	 * @param string $field
275
	 * @param string|integer $value
276
	 */
277
	private function limitToDBFieldOrGreater(IQueryBuilder &$qb, $field, $value) {
278
		$expr = $qb->expr();
279
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
280
		$qb->andWhere($expr->gte($pf . $field, $qb->createNamedParameter($value)));
281
	}
282
283
284
	/**
285
	 * link to the groupId/UserId of the NC DB.
286
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
287
	 * alias
288
	 *
289
	 * @param IQueryBuilder $qb
290
	 * @param string $userId
291
	 */
292
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
293
		$expr = $qb->expr();
294
295
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
296
297
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
298
		if ($userId !== '') {
299
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
300
		} else {
301
			$qb->selectAlias('ncgu.uid', 'user_id');
302
		}
303
304
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
305
		$qb->andWhere($and);
306
	}
307
308
309
	/**
310
	 * Right Join the Circles table
311
	 *
312
	 * @param IQueryBuilder $qb
313
	 *
314
	 * @deprecated not used (14/07/17)
315
	 */
316
	protected function rightJoinCircles(IQueryBuilder &$qb) {
317
		$expr = $qb->expr();
318
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
319
320
		$qb->from(self::TABLE_CIRCLES, 'c')
321
		   ->andWhere(
322
			   $expr->eq(
323
				   $pf . 'circle_id',
324
				   $qb->createFunction(
325
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
326
				   )
327
			   )
328
		   );
329
	}
330
331
332
	/**
333
	 * link to the groupId/UserId of the NC DB.
334
	 *
335
	 * @param IQueryBuilder $qb
336
	 * @param string $userId
337
	 * @param string $field
338
	 */
339
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
340
		if (!$this->configService->isLinkedGroupsAllowed()) {
341
			return;
342
		}
343
344
		$expr = $qb->expr();
345
		$qb->leftJoin(
346
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
347
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
348
		);
349
350
		/** @noinspection PhpMethodParametersCountMismatchInspection */
351
		$qb->leftJoin(
352
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
353
			$expr->andX(
354
				$expr->eq('ncgu.gid', 'g.group_id'),
355
				$expr->eq(
356
					'g.circle_id', $qb->createFunction(
357
					'SUBSTR(' . $field . ', 1, ' . Circle::SHORT_UNIQUE_ID_LENGTH . ')'
358
				)
359
				)
360
			)
361
		);
362
363
		$this->leftJoinedNCGroupAndUser = true;
364
	}
365
}
366
367
368
369