Completed
Pull Request — master (#105)
by Maxence
02:19
created

CoreRequestBuilder::limitToNCGroupUser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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