Completed
Push — master ( 473696...473696 )
by Maxence
05:29 queued 02:51
created

CirclesRequestBuilder::limitToUniqueId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/**
4
 * Circles - Bring cloud-users closer together.
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Maxence Lange <[email protected]>
10
 * @copyright 2017
11
 * @license GNU AGPL version 3 or any later version
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25
 *
26
 */
27
28
namespace OCA\Circles\Db;
29
30
31
use Doctrine\DBAL\Query\QueryBuilder;
32
use OC\L10N\L10N;
33
use OCA\Circles\Model\Circle;
34
use OCA\Circles\Model\FederatedLink;
35
use OCA\Circles\Model\Member;
36
use OCA\Circles\Model\SharingFrame;
37
use OCP\DB\QueryBuilder\IQueryBuilder;
38
use OCP\IDBConnection;
39
40
class CirclesRequestBuilder {
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...
41
42
	const TABLE_CIRCLES = 'circles_circles';
43
	const TABLE_MEMBERS = 'circles_members';
44
45
	/** @var IDBConnection */
46
	protected $dbConnection;
47
48
	/** @var L10N */
49
	protected $l10n;
50
51
	private $default_select_alias;
52
53
54
	/**
55
	 * Join the Circles table
56
	 *
57
	 * @param IQueryBuilder $qb
58
	 * @param string $field
59
	 */
60
	protected function joinCircles(& $qb, $field) {
61
		$expr = $qb->expr();
62
63
		$qb->from(self::TABLE_CIRCLES, 'c')
64
		   ->andWhere($expr->eq('c.id', $field));
65
	}
66
67
68
	/**
69
	 * Limit the request to the Share by its Id.
70
	 *
71
	 * @param IQueryBuilder $qb
72
	 * @param int $circleId
73
	 */
74
	protected function limitToCircleId(IQueryBuilder & $qb, $circleId) {
75
		$expr = $qb->expr();
76
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias.'.' : '';
77
78
		$qb->andWhere($expr->eq($pf.'circle_id', $qb->createNamedParameter($circleId)));
79
	}
80
81
82
	/**
83
	 * Limit the request by its Id.
84
	 *
85
	 * @param IQueryBuilder $qb
86
	 * @param int $id
87
	 */
88
	protected function limitToId(IQueryBuilder & $qb, $id) {
89
		$expr = $qb->expr();
90
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias.'.' : '';
91
92
		$qb->andWhere($expr->eq($pf.'id', $qb->createNamedParameter($id)));
93
	}
94
95
96
	/**
97
	 * Limit the request by its UniqueId.
98
	 *
99
	 * @param IQueryBuilder $qb
100
	 * @param int $uniqueId
101
	 */
102
	protected function limitToUniqueId(IQueryBuilder & $qb, $uniqueId) {
103
		$expr = $qb->expr();
104
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias.'.' : '';
105
106
		$qb->andWhere($expr->eq($pf.'unique_id', $qb->createNamedParameter($uniqueId)));
107
	}
108
109
110
	/**
111
	 * Limit the request by its Token.
112
	 *
113
	 * @param IQueryBuilder $qb
114
	 * @param string $token
115
	 */
116
	protected function limitToToken(IQueryBuilder & $qb, $token) {
117
		$expr = $qb->expr();
118
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias.'.' : '';
119
120
		$qb->andWhere($expr->eq($pf.'token', $qb->createNamedParameter($token)));
121
	}
122
123
124
	/**
125
	 * Limit the request to a minimum member level.
126
	 *
127
	 * @param IQueryBuilder $qb
128
	 * @param integer $level
129
	 */
130
	protected function limitToMemberLevel(IQueryBuilder & $qb, $level) {
131
		$qb->where(
132
			$qb->expr()
133
			   ->gte('m.level', $qb->createNamedParameter($level))
134
		);
135
	}
136
137
138
	/**
139
	 * add a request to the members list, using the current user ID.
140
	 * will returns level and stuff.
141
	 *
142
	 * @param IQueryBuilder $qb
143
	 * @param string $userId
144
	 */
145
	protected function leftJoinUserIdAsMember(IQueryBuilder & $qb, $userId) {
146
147
		if ($qb->getType() !== QueryBuilder::SELECT) {
148
			return;
149
		}
150
151
		$expr = $qb->expr();
152
		$pf = $this->default_select_alias.'.';
153
154
		$qb->selectAlias('u.level', 'user_level');
155
		$qb->leftJoin(
156
			$this->default_select_alias, MembersMapper::TABLENAME, 'u',
157
			$expr->andX(
158
				$expr->eq($pf.'id', 'u.circle_id'),
159
				$expr->eq('u.user_id', $qb->createNamedParameter($userId))
160
			)
161
		);
162
	}
163
164
	/**
165
	 * @param IQueryBuilder $qb
166
	 *
167
	 * @deprecated
168
	 * never used in fact.
169
	 */
170
	protected function leftJoinOwner(IQueryBuilder & $qb) {
171
172
		if ($qb->getType() !== QueryBuilder::SELECT) {
173
			return;
174
		}
175
176
		$expr = $qb->expr();
177
		$pf = $this->default_select_alias.'.';
178
179
		$qb->leftJoin(
180
			$this->default_select_alias, MembersMapper::TABLENAME, 'o',
181
			$expr->andX(
182
				$expr->eq($pf.'id', 'o.circle_id'),
183
				$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER))
184
			)
185
		);
186
	}
187
188
189
	/**
190
	 * Base of the Sql Select request for Shares
191
	 *
192
	 * @return IQueryBuilder
193
	 */
194
	protected function getLinksSelectSql() {
195
		$qb = $this->dbConnection->getQueryBuilder();
196
197
		$qb->select('id', 'status', 'address', 'token', 'circle_id', 'unique_id', 'creation')
198
		   ->from('circles_links', 's');
199
200
		$this->default_select_alias = 's';
201
202
		return $qb;
203
	}
204
205
206
	/**
207
	 * Base of the Sql Select request for Shares
208
	 *
209
	 * @return IQueryBuilder
210
	 */
211
	protected function getSharesSelectSql() {
212
		$qb = $this->dbConnection->getQueryBuilder();
213
214
		$qb->select(
215
			'circle_id', 'source', 'type', 'author', 'cloud_id', 'payload', 'creation', 'headers',
216
			'unique_id'
217
		)
218
		   ->from('circles_shares', 's');
219
220
		$this->default_select_alias = 's';
221
222
		return $qb;
223
	}
224
225
	/**
226
	 * Base of the Sql Insert request for Shares
227
	 *
228
	 * @return IQueryBuilder
229
	 */
230
	protected function getSharesInsertSql() {
231
		$qb = $this->dbConnection->getQueryBuilder();
232
		$qb->insert('circles_shares')
233
		   ->setValue('creation', $qb->createFunction('NOW()'));
234
235
		return $qb;
236
	}
237
238
239
	/**
240
	 * Base of the Sql Update request for Shares
241
	 *
242
	 * @param string $uniqueId
243
	 *
244
	 * @return IQueryBuilder
245
	 */
246
	protected function getSharesUpdateSql($uniqueId) {
247
		$qb = $this->dbConnection->getQueryBuilder();
248
		$qb->update('circles_shares')
249
		   ->where(
250
			   $qb->expr()
251
				  ->eq('unique_id', $qb->createNamedParameter((string)$uniqueId))
252
		   );
253
254
		return $qb;
255
	}
256
257
258
	/**
259
	 * @return IQueryBuilder
260
	 */
261
	protected function getMembersSelectSql() {
262
		$qb = $this->dbConnection->getQueryBuilder();
263
264
		$qb->select('user_id', 'circle_id', 'level', 'status', 'joined')
265
		   ->from('circles_members', 'm');
266
267
		$this->default_select_alias = 'm';
268
269
		return $qb;
270
	}
271
272
273
	/**
274
	 * @return IQueryBuilder
275
	 */
276
	protected function getCirclesSelectSql() {
277
		$qb = $this->dbConnection->getQueryBuilder();
278
279
		$qb->select('c.id', 'c.unique_id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
280
		   ->from('circles_circles', 'c');
281
		$this->default_select_alias = 'c';
282
283
		return $qb;
284
	}
285
286
	/**
287
	 * @param array $data
288
	 *
289
	 * @return Member
290
	 */
291
	protected function parseMembersSelectSql(array $data) {
292
		$member = new Member($this->l10n);
293
		$member->setUserId($data['user_id']);
294
		$member->setCircleId($data['circle_id']);
295
		$member->setLevel($data['level']);
296
		$member->setStatus($data['status']);
297
		$member->setJoined($data['joined']);
298
299
		return $member;
300
	}
301
302
303
	/**
304
	 * @param array $data
305
	 *
306
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
307
	 */
308
	protected function parseCirclesSelectSql($data) {
309
		if ($data === false || $data === null) {
310
			return null;
311
		}
312
313
		$circle = new Circle($this->l10n);
314
		$circle->setId($data['id']);
315
		$circle->setUniqueId($data['unique_id']);
316
		$circle->setName($data['name']);
317
		$circle->setDescription($data['description']);
318
		$circle->setSettings($data['settings']);
319
		$circle->setType($data['type']);
320
		$circle->setCreation($data['creation']);
321
322
		if (key_exists('user_level', $data)) {
323
			$user = new Member($this->l10n);
324
			$user->setLevel($data['user_level']);
325
			$circle->setUser($user);
326
		}
327
328
		return $circle;
329
	}
330
331
332
	/**
333
	 * @param array $data
334
	 *
335
	 * @return SharingFrame
0 ignored issues
show
Documentation introduced by
Should the return type not be SharingFrame|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
336
	 */
337
	protected function parseSharesSelectSql($data) {
338
		if ($data === false || $data === null) {
339
			return null;
340
		}
341
342
		$frame = new SharingFrame($data['source'], $data['type']);
343
		$frame->setCircleId($data['circle_id']);
344
		$frame->setAuthor($data['author']);
345
		$frame->setCloudId($data['cloud_id']);
346
		$frame->setPayload(json_decode($data['payload'], true));
347
		$frame->setCreation($data['creation']);
348
		$frame->setHeaders(json_decode($data['headers'], true));
349
		$frame->setUniqueId($data['unique_id']);
350
351
		return $frame;
352
	}
353
354
355
	/**
356
	 * @param array $data
357
	 *
358
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
359
	 */
360 View Code Duplication
	public function parseLinksSelectSql($data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
361
		if ($data === false || $data === null) {
362
			return null;
363
		}
364
365
		$link = new FederatedLink();
366
		$link->setId($data['id'])
367
			 ->setUniqueId($data['unique_id'])
368
			 ->setStatus($data['status'])
369
			 ->setAddress($data['address'])
370
			 ->setToken($data['token'])
371
			 ->setCircleId($data['circle_id']);
372
373
		return $link;
374
	}
375
376
377
}