Completed
Push — master ( 6ce943...2d2289 )
by Maxence
02:19
created

CirclesRequestBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A joinCircles() 0 6 1
A limitToCircle() 0 6 2
A getSharesInsertSql() 0 7 1
A getMembersSelectSql() 0 12 1
A parseMembersSelectSql() 0 9 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 OCA\Circles\Model\Member;
33
use OCP\DB\QueryBuilder\IQueryBuilder;
34
use OCP\IDBConnection;
35
use OCP\Share;
36
use OCP\Share\IShare;
37
38
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...
39
40
	const TABLE_CIRCLES = 'circles_circles';
41
	const TABLE_MEMBERS = 'circles_members';
42
43
	/** @var IDBConnection */
44
	protected $dbConnection;
45
46
	private $default_select_alias;
47
48
49
	/**
50
	 * Join the Circles table
51
	 *
52
	 * @param $qb
53
	 */
54
	protected function joinCircles(& $qb, $field) {
55
		$expr = $qb->expr();
56
57
		$qb->from(self::TABLE_CIRCLES, 'c')
58
		   ->andWhere($expr->eq('c.id', $field));
59
	}
60
61
62
	/**
63
	 * Limit the request to the Share by its Id.
64
	 *
65
	 * @param IQueryBuilder $qb
66
	 * @param $circleId
67
	 */
68
	protected function limitToCircle(& $qb, $circleId) {
69
		$expr = $qb->expr();
70
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
71
72
		$qb->andWhere($expr->eq($pf . 'circle_id', $qb->createNamedParameter($circleId)));
73
	}
74
75
76
	/**
77
	 * Base of the Sql Insert request
78
	 *
79
	 * @return IQueryBuilder
80
	 */
81
	protected function getSharesInsertSql() {
82
		$qb = $this->dbConnection->getQueryBuilder();
83
		$qb->insert('circles_shares')
84
		   ->setValue('creation', $qb->createFunction('NOW()'));
85
86
		return $qb;
87
	}
88
89
90
	/**
91
	 * @param int $level
92
	 *
93
	 * @return IQueryBuilder
94
	 */
95
	protected function getMembersSelectSql(int $level = Member::LEVEL_MEMBER) {
96
		$qb = $this->dbConnection->getQueryBuilder();
97
		$expr = $qb->expr();
98
99
		$qb->select('user_id', 'circle_id', 'level', 'status', 'joined')
100
		   ->from('circles_members', 'm')
101
		   ->where($expr->gte('m.level', $qb->createNamedParameter($level)));
102
103
		$this->default_select_alias = 'm';
104
105
		return $qb;
106
	}
107
108
	protected function parseMembersSelectSql(array $data) {
109
		return [
110
			'uid'      => $data['user_id'],
111
			'circleId' => $data['circle_id'],
112
			'level'    => $data['level'],
113
			'status'   => $data['status'],
114
			'joined'   => $data['joined']
115
		];
116
	}
117
118
}