Completed
Push — master ( 703448...70edb2 )
by Maxence
02:33
created

CoreRequestBuilder::leftJoinNCGroupAndUser()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 26
rs 8.8571
c 2
b 0
f 0
cc 2
eloc 15
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\Service\ConfigService;
14
use OCP\DB\QueryBuilder\IQueryBuilder;
15
use Doctrine\DBAL\Query\QueryBuilder;
16
use OC\L10N\L10N;
17
use OCA\Circles\Service\MiscService;
18
use OCP\IDBConnection;
19
20
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...
21
22
	const TABLE_CIRCLES = 'circles_circles';
23
	const TABLE_MEMBERS = 'circles_members';
24
	const TABLE_GROUPS = 'circles_groups';
25
	const TABLE_SHARES = 'circles_shares';
26
	const TABLE_LINKS = 'circles_links';
27
28
	const NC_TABLE_GROUP_USER = 'group_user';
29
30
	/** @var IDBConnection */
31
	protected $dbConnection;
32
33
	/** @var L10N */
34
	protected $l10n;
35
36
	/** @var ConfigService */
37
	protected $configService;
38
39
	/** @var MiscService */
40
	protected $miscService;
41
42
	/** @var string */
43
	protected $default_select_alias;
44
45
	/** @var bool */
46
	protected $leftJoinedNCGroupAndUser = false;
47
48
	/**
49
	 * RequestBuilder constructor.
50
	 *
51
	 * @param L10N $l10n
52
	 * @param IDBConnection $connection
53
	 * @param ConfigService $configService
54
	 * @param MiscService $miscService
55
	 */
56
	public function __construct(
57
		L10N $l10n, IDBConnection $connection, ConfigService $configService,
58
		MiscService $miscService
59
	) {
60
		$this->l10n = $l10n;
61
		$this->dbConnection = $connection;
62
		$this->configService = $configService;
63
		$this->miscService = $miscService;
64
	}
65
66
67
	/**
68
	 * Limit the request by its Id.
69
	 *
70
	 * @param IQueryBuilder $qb
71
	 * @param int $id
72
	 */
73
	protected function limitToId(IQueryBuilder &$qb, $id) {
74
		$this->limitToDBField($qb, 'id', $id);
75
	}
76
77
78
	/**
79
	 * Limit the request by its UniqueId.
80
	 *
81
	 * @param IQueryBuilder $qb
82
	 * @param int $uniqueId
83
	 */
84
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
85
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
86
	}
87
88
89
	/**
90
	 * Limit the request by its Token.
91
	 *
92
	 * @param IQueryBuilder $qb
93
	 * @param string $token
94
	 */
95
	protected function limitToToken(IQueryBuilder &$qb, $token) {
96
		$this->limitToDBField($qb, 'token', $token);
97
	}
98
99
100
	/**
101
	 * Limit the request to the User by its Id.
102
	 *
103
	 * @param IQueryBuilder $qb
104
	 * @param $userId
105
	 *
106
	 * @internal param int $circleId
107
	 */
108
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
109
		$this->limitToDBField($qb, 'user_id', $userId);
110
	}
111
112
113
	/**
114
	 * Limit the request to the Circle by its Id.
115
	 *
116
	 * @param IQueryBuilder $qb
117
	 * @param string $circleUniqueId
118
	 */
119
	protected function limitToCircleId(IQueryBuilder &$qb, $circleUniqueId) {
120
		$this->limitToDBField($qb, 'circle_id', $circleUniqueId);
121
122
	}
123
124
125
	/**
126
	 * Limit the request to the Circle by its Shorten Unique Id.
127
	 *
128
	 * @param IQueryBuilder $qb
129
	 * @param string $circleUniqueId
130
	 */
131
	protected function limitToShortenUniqueId(IQueryBuilder &$qb, $circleUniqueId) {
132
		$expr = $qb->expr();
133
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
134
135
		$qb->andWhere(
136
			$expr->eq(
137
				$qb->createNamedParameter($circleUniqueId),
138
				$qb->createFunction(
139
					'LEFT(' . $pf . 'unique_id' . ', ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
140
				)
141
			)
142
		);
143
144
	}
145
146
147
	/**
148
	 * Limit the request to the Group by its Id.
149
	 *
150
	 * @param IQueryBuilder $qb
151
	 * @param int $groupId
152
	 */
153
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
154
		$this->limitToDBField($qb, 'group_id', $groupId);
155
	}
156
157
158
	/**
159
	 * Limit the search by its Name
160
	 *
161
	 * @param IQueryBuilder $qb
162
	 * @param string $name
163
	 */
164
	protected function limitToName(IQueryBuilder &$qb, $name) {
165
		$this->limitToDBField($qb, 'name', $name);
166
	}
167
168
169
	/**
170
	 * Limit the request to a minimum member level.
171
	 *
172
	 * @param IQueryBuilder $qb
173
	 * @param int $level
174
	 * @param string|array $pf
175
	 */
176
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
177
		$expr = $qb->expr();
178
179
		if ($pf === '') {
180
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
181
			$qb->andWhere($expr->gte($p . 'level', $qb->createNamedParameter($level)));
182
183
			return;
184
		}
185
186
		$orX = $expr->orX();
187
188
		if (!is_array($pf)) {
189
			$pf = [$pf];
190
		}
191
192
		foreach ($pf as $p) {
193
			if ($p === 'g' && !$this->leftJoinedNCGroupAndUser) {
194
				continue;
195
			}
196
			$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
197
		}
198
199
		$qb->andWhere($orX);
200
	}
201
202
203
	/**
204
	 * @param IQueryBuilder $qb
205
	 * @param string $field
206
	 * @param string|integer $value
207
	 */
208
	private function limitToDBField(IQueryBuilder &$qb, $field, $value) {
209
		$expr = $qb->expr();
210
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
211
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
212
	}
213
214
215
	/**
216
	 * link to the groupId/UserId of the NC DB.
217
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
218
	 * alias
219
	 *
220
	 * @param IQueryBuilder $qb
221
	 * @param string $userId
222
	 */
223
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
224
		$expr = $qb->expr();
225
226
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
227
228
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
229
		if ($userId !== '') {
230
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
231
		} else {
232
			$qb->selectAlias('ncgu.uid', 'user_id');
233
		}
234
235
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
236
		$qb->andWhere($and);
237
	}
238
239
240
	/**
241
	 * Right Join the Circles table
242
	 *
243
	 * @param IQueryBuilder $qb
244
	 *
245
	 * @deprecated not used (14/07/17)
246
	 */
247
	protected function rightJoinCircles(IQueryBuilder &$qb) {
248
		$expr = $qb->expr();
249
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
250
251
		$qb->from(self::TABLE_CIRCLES, 'c')
252
		   ->andWhere(
253
			   $expr->eq(
254
				   $pf . 'circle_id',
255
				   $qb->createFunction('LEFT(c.unique_id, ' . Circle::UNIQUEID_SHORT_LENGTH . ')')
256
			   )
257
		   );
258
	}
259
260
261
	/**
262
	 * link to the groupId/UserId of the NC DB.
263
	 *
264
	 * @param IQueryBuilder $qb
265
	 * @param string $userId
266
	 * @param string $field
267
	 */
268
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $userId, $field) {
269
		if (!$this->configService->isLinkedGroupsAllowed()) {
270
			return;
271
		}
272
273
		$expr = $qb->expr();
274
		$qb->leftJoin(
275
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
276
			$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
277
		);
278
279
		/** @noinspection PhpMethodParametersCountMismatchInspection */
280
		$qb->leftJoin(
281
			$this->default_select_alias, CoreRequestBuilder::TABLE_GROUPS, 'g',
282
			$expr->andX(
283
				$expr->eq('ncgu.gid', 'g.group_id'),
284
				$expr->eq(
285
					'g.circle_id', $qb->createFunction(
286
					'LEFT(' . $field . ', ' . Circle::UNIQUEID_SHORT_LENGTH . ')'
287
				)
288
				)
289
			)
290
		);
291
292
		$this->leftJoinedNCGroupAndUser = true;
293
	}
294
}
295
296
297
298