Completed
Push — master ( 42194a...d78ef0 )
by Maxence
02:20
created

CoreRequestBuilder::limitToMembersAndAlmost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
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 OC\L10N\L10N;
18
use OCA\Circles\Service\MiscService;
19
use OCP\IDBConnection;
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 L10N */
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 L10N $l10n
53
	 * @param IDBConnection $connection
54
	 * @param ConfigService $configService
55
	 * @param MiscService $miscService
56
	 */
57
	public function __construct(
58
		L10N $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 Circle by its Id.
116
	 *
117
	 * @param IQueryBuilder $qb
118
	 * @param string $circleUniqueId
119
	 */
120
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
121
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
122
123
	}
124
125
126
	/**
127
	 * Limit the request to the Circle by its Shorten Unique Id.
128
	 *
129
	 * @param IQueryBuilder $qb
130
	 * @param string $circleUniqueId
131
	 */
132
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId) {
133
		$expr = $qb->expr();
134
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
135
136
		$qb->andWhere(
137
			$expr->eq(
138
				$qb->createNamedParameter($circleUniqueId),
139
				$qb->createFunction(
140
					'LEFT(' . $pf . 'unique_id' . ', ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
141
				)
142
			)
143
		);
144
145
	}
146
147
148
	/**
149
	 * Limit the request to the Group by its Id.
150
	 *
151
	 * @param IQueryBuilder $qb
152
	 * @param int $groupId
153
	 */
154
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
155
		$this->limitToDBField($qb, 'group_id', $groupId);
156
	}
157
158
159
	/**
160
	 * Limit the search by its Name
161
	 *
162
	 * @param IQueryBuilder $qb
163
	 * @param string $name
164
	 */
165
	protected function limitToName(IQueryBuilder &$qb, $name) {
166
		$this->limitToDBField($qb, 'name', $name);
167
	}
168
169
170
	/**
171
	 * Limit the request to a minimum member level.
172
	 *
173
	 * @param IQueryBuilder $qb
174
	 * @param int $level
175
	 * @param string|array $pf
176
	 */
177
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
178
		$expr = $qb->expr();
179
180
		if ($pf === '') {
181
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
182
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
183
184
			return;
185
		}
186
187
		$orX = $expr->orX();
188
189
		if (!is_array($pf)) {
190
			$pf = [$pf];
191
		}
192
193
		foreach ($pf as $p) {
194
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
195
				continue;
196
			}
197
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
198
		}
199
200
		$qb->andWhere($orX);
201
	}
202
203
204
	/**
205
	 * Limit the search to Members and Almost members
206
	 *
207
	 * @param IQueryBuilder $qb
208
	 */
209
	protected function limitToMembersAndAlmost(IQueryBuilder &$qb) {
210
		$expr = $qb->expr();
211
212
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
213
214
		$orX = $expr->orX();
215
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_MEMBER)));
216
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_INVITED)));
217
		$orX->add($expr->eq($pf . 'status', $qb->createNamedParameter(Member::STATUS_REQUEST)));
218
219
		$qb->andWhere($orX);
220
221
	}
222
223
224
	/**
225
	 * @param IQueryBuilder $qb
226
	 * @param string $field
227
	 * @param string|integer $value
228
	 */
229
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
230
		$expr = $qb->expr();
231
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
232
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
233
	}
234
235
236
	/**
237
	 * link to the groupId/UserId of the NC DB.
238
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
239
	 * alias
240
	 *
241
	 * @param IQueryBuilder $qb
242
	 * @param string $userId
243
	 */
244
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
245
		$expr = $qb->expr();
246
247
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
248
249
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
250
		if ($userId !== '') {
251
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
252
		} else {
253
			$qb->selectAlias('ncgu.uid', 'user_id');
254
		}
255
256
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
257
		$qb->andWhere($and);
258
	}
259
260
261
	/**
262
	 * Right Join the Circles table
263
	 *
264
	 * @param IQueryBuilder $qb
265
	 *
266
	 * @deprecated not used (14/07/17)
267
	 */
268
	protected function rightJoinCircles(IQueryBuilder &$qb) {
269
		$expr = $qb->expr();
270
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
271
272
		$qb->from(self::TABLE_CIRCLES, 'c')
273
		   ->andWhere(
274
			   $expr->eq(
275
				   $pf . 'circle_id',
276
				   $qb->createFunction('LEFT(c.unique_id, ' . Circle::UNIQUEID_SHORT_LENGTH . ')')
277
			   )
278
		   );
279
	}
280
281
282
	/**
283
	 * link to the groupId/UserId of the NC DB.
284
	 *
285
	 * @param IQueryBuilder $qb
286
	 * @param string $userId
287
	 * @param string $field
288
	 */
289
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
290
		if (!$this->configService->isLinkedGroupsAllowed()) {
291
			return;
292
		}
293
294
		$expr = $qb->expr();
295
		$qb->leftJoin(
296
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
297
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
298
		);
299
300
		/** @noinspection PhpMethodParametersCountMismatchInspection */
301
		$qb->leftJoin(
302
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
303
			$expr->andX(
304
				$expr->eq('ncgu.gid', 'g.group_id'),
305
				$expr->eq(
306
					'g.circle_id', $qb->createFunction(
307
					'LEFT(' . $field . ', ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
308
				)
309
				)
310
			)
311
		);
312
313
		$this->leftJoinedNCGroupAndUser = true;
314
	}
315
}
316
317
318
319