Completed
Push — master ( 853863...462815 )
by Maxence
02:39
created

CoreRequestBuilder::limitToGroupId()   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
	 */
144
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId) {
145
		$expr = $qb->expr();
146
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? '`' . $this->default_select_alias . '`.' : '';
147
148
		$qb->andWhere(
149
			$expr->eq(
150
				$qb->createNamedParameter($circleUniqueId),
151
				$qb->createFunction(
152
					'SUBSTR(' . $pf . '`unique_id`' . ', 1, ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
153
				)
154
			)
155
		);
156
157
	}
158
159
160
	/**
161
	 * Limit the request to the Group by its Id.
162
	 *
163
	 * @param IQueryBuilder $qb
164
	 * @param int $groupId
165
	 */
166
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
167
		$this->limitToDBField($qb, 'group_id', $groupId);
168
	}
169
170
171
	/**
172
	 * Limit the search by its Name
173
	 *
174
	 * @param IQueryBuilder $qb
175
	 * @param string $name
176
	 */
177
	protected function limitToName(IQueryBuilder &$qb, $name) {
178
		$this->limitToDBField($qb, 'name', $name);
179
	}
180
181
182
	/**
183
	 * Limit the request to a minimum member level.
184
	 *
185
	 * if $pf is an array, will generate an SQL OR request to limit level in multiple tables
186
	 *
187
	 * @param IQueryBuilder $qb
188
	 * @param int $level
189
	 * @param string|array $pf
190
	 */
191
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
192
		$expr = $qb->expr();
193
194
		if ($pf === '') {
195
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
196
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
197
198
			return;
199
		}
200
201
		if (!is_array($pf)) {
202
			$pf = [$pf];
203
		}
204
205
		$orX = $this->generateLimitToLevelMultipleTableRequest($qb, $level, $pf);
206
		$qb->andWhere($orX);
207
	}
208
209
210
	/**
211
	 * @param IQueryBuilder $qb
212
	 * @param array $pf
213
	 *
214
	 * @return mixed
215
	 */
216
	private function generateLimitToLevelMultipleTableRequest(IQueryBuilder $qb, $level, $pf) {
217
		$expr = $qb->expr();
218
		$orX = $expr->orX();
219
220
		foreach ($pf as $p) {
221
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
222
				continue;
223
			}
224
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
225
		}
226
227
		return $orX;
228
	}
229
230
231
	/**
232
	 * Limit the search to Members and Almost members
233
	 *
234
	 * @param IQueryBuilder $qb
235
	 */
236
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
237
		$expr = $qb->expr();
238
239
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
240
241
		$orX = $expr->orX();
242
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
243
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
244
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
245
246
		$qb->andWhere($orX);
247
	}
248
249
250
	/**
251
	 * @param IQueryBuilder $qb
252
	 * @param string $field
253
	 * @param string|integer $value
254
	 */
255
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
256
		$expr = $qb->expr();
257
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
258
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
259
	}
260
261
262
	/**
263
	 * link to the groupId/UserId of the NC DB.
264
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
265
	 * alias
266
	 *
267
	 * @param IQueryBuilder $qb
268
	 * @param string $userId
269
	 */
270
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
271
		$expr = $qb->expr();
272
273
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
274
275
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
276
		if ($userId !== '') {
277
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
278
		} else {
279
			$qb->selectAlias('ncgu.uid', 'user_id');
280
		}
281
282
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
283
		$qb->andWhere($and);
284
	}
285
286
287
	/**
288
	 * Right Join the Circles table
289
	 *
290
	 * @param IQueryBuilder $qb
291
	 *
292
	 * @deprecated not used (14/07/17)
293
	 */
294
	protected function rightJoinCircles(IQueryBuilder &$qb) {
295
		$expr = $qb->expr();
296
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
297
298
		$qb->from(self::TABLE_CIRCLES, 'c')
299
		   ->andWhere(
300
			   $expr->eq(
301
				   $pf . 'circle_id',
302
				   $qb->createFunction(
303
					   'SUBSTR(`c`.`unique_id`, 1, ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
304
				   )
305
			   )
306
		   );
307
	}
308
309
310
	/**
311
	 * link to the groupId/UserId of the NC DB.
312
	 *
313
	 * @param IQueryBuilder $qb
314
	 * @param string $userId
315
	 * @param string $field
316
	 */
317
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
318
		if (!$this->configService->isLinkedGroupsAllowed()) {
319
			return;
320
		}
321
322
		$expr = $qb->expr();
323
		$qb->leftJoin(
324
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
325
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
326
		);
327
328
		/** @noinspection PhpMethodParametersCountMismatchInspection */
329
		$qb->leftJoin(
330
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
331
			$expr->andX(
332
				$expr->eq('ncgu.gid', 'g.group_id'),
333
				$expr->eq(
334
					'g.circle_id', $qb->createFunction(
335
					'SUBSTR(' . $field . ', 1, ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
336
				)
337
				)
338
			)
339
		);
340
341
		$this->leftJoinedNCGroupAndUser = true;
342
	}
343
}
344
345
346
347