Completed
Push — master ( fa4c5d...fa4c5d )
by Maxence
05:16 queued 02:40
created

CirclesRequestBuilder::getCirclesUpdateSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
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
	 * Base of the Sql Update request for Shares
260
	 *
261
	 * @param int $circleId
262
	 *
263
	 * @return IQueryBuilder
264
	 */
265 View Code Duplication
	protected function getCirclesUpdateSql($circleId) {
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...
266
		$qb = $this->dbConnection->getQueryBuilder();
267
		$qb->update('circles_circles')
268
		   ->where(
269
			   $qb->expr()
270
				  ->eq('id', $qb->createNamedParameter($circleId))
271
		   );
272
273
		return $qb;
274
	}
275
276
277
	/**
278
	 * @return IQueryBuilder
279
	 */
280
	protected function getMembersSelectSql() {
281
		$qb = $this->dbConnection->getQueryBuilder();
282
283
		$qb->select('user_id', 'circle_id', 'level', 'status', 'joined')
284
		   ->from('circles_members', 'm');
285
286
		$this->default_select_alias = 'm';
287
288
		return $qb;
289
	}
290
291
292
	/**
293
	 * @return IQueryBuilder
294
	 */
295
	protected function getCirclesSelectSql() {
296
		$qb = $this->dbConnection->getQueryBuilder();
297
298
		$qb->select(
299
			'c.id', 'c.unique_id', 'c.name', 'c.description', 'c.settings', 'c.type', 'c.creation'
300
		)
301
		   ->from('circles_circles', 'c');
302
		$this->default_select_alias = 'c';
303
304
		return $qb;
305
	}
306
307
	/**
308
	 * @param array $data
309
	 *
310
	 * @return Member
311
	 */
312
	protected function parseMembersSelectSql(array $data) {
313
		$member = new Member($this->l10n);
314
		$member->setUserId($data['user_id']);
315
		$member->setCircleId($data['circle_id']);
316
		$member->setLevel($data['level']);
317
		$member->setStatus($data['status']);
318
		$member->setJoined($data['joined']);
319
320
		return $member;
321
	}
322
323
324
	/**
325
	 * @param array $data
326
	 *
327
	 * @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...
328
	 */
329
	protected function parseCirclesSelectSql($data) {
330
		if ($data === false || $data === null) {
331
			return null;
332
		}
333
334
		$circle = new Circle($this->l10n);
335
		$circle->setId($data['id']);
336
		$circle->setUniqueId($data['unique_id']);
337
		$circle->setName($data['name']);
338
		$circle->setDescription($data['description']);
339
		$circle->setSettings($data['settings']);
340
		$circle->setType($data['type']);
341
		$circle->setCreation($data['creation']);
342
343
		if (key_exists('user_level', $data)) {
344
			$user = new Member($this->l10n);
345
			$user->setLevel($data['user_level']);
346
			$circle->setUser($user);
347
		}
348
349
		return $circle;
350
	}
351
352
353
	/**
354
	 * @param array $data
355
	 *
356
	 * @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...
357
	 */
358
	protected function parseSharesSelectSql($data) {
359
		if ($data === false || $data === null) {
360
			return null;
361
		}
362
363
		$frame = new SharingFrame($data['source'], $data['type']);
364
		$frame->setCircleId($data['circle_id']);
365
		$frame->setAuthor($data['author']);
366
		$frame->setCloudId($data['cloud_id']);
367
		$frame->setPayload(json_decode($data['payload'], true));
368
		$frame->setCreation($data['creation']);
369
		$frame->setHeaders(json_decode($data['headers'], true));
370
		$frame->setUniqueId($data['unique_id']);
371
372
		return $frame;
373
	}
374
375
376
	/**
377
	 * @param array $data
378
	 *
379
	 * @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...
380
	 */
381 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...
382
		if ($data === false || $data === null) {
383
			return null;
384
		}
385
386
		$link = new FederatedLink();
387
		$link->setId($data['id'])
388
			 ->setUniqueId($data['unique_id'])
389
			 ->setStatus($data['status'])
390
			 ->setAddress($data['address'])
391
			 ->setToken($data['token'])
392
			 ->setCircleId($data['circle_id']);
393
394
		return $link;
395
	}
396
397
398
}