Completed
Push — federated-circles ( 92a91f...545292 )
by Maxence
02:39
created

CirclesRequestBuilder::getCirclesSelectSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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\Member;
35
use OCP\DB\QueryBuilder\IQueryBuilder;
36
use OCP\IDBConnection;
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
	/** @var L10N */
47
	protected $l10n;
48
49
	private $default_select_alias;
50
51
52
	/**
53
	 * Join the Circles table
54
	 *
55
	 * @param IQueryBuilder $qb
56
	 */
57
	protected function joinCircles(& $qb, $field) {
58
		$expr = $qb->expr();
59
60
		$qb->from(self::TABLE_CIRCLES, 'c')
61
		   ->andWhere($expr->eq('c.id', $field));
62
	}
63
64
65
	/**
66
	 * Limit the request to the Share by its Id.
67
	 *
68
	 * @param IQueryBuilder $qb
69
	 * @param int $circleId
70
	 */
71
	protected function limitToCircle(IQueryBuilder &$qb, $circleId) {
72
		$expr = $qb->expr();
73
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
74
75
		$qb->andWhere($expr->eq($pf . 'circle_id', $qb->createNamedParameter($circleId)));
76
	}
77
78
79
80
	/**
81
	 * Limit the request to the Share by its Id.
82
	 *
83
	 * @param IQueryBuilder $qb
84
	 * @param int $id
85
	 */
86
	protected function limitToId(IQueryBuilder &$qb, $id) {
87
		$expr = $qb->expr();
88
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
89
90
		$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($id)));
91
	}
92
93
94
	/**
95
	 * Limit the request to the Share by its Id.
96
	 *
97
	 * @param IQueryBuilder $qb
98
	 * @param string $userId
99
	 */
100
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
0 ignored issues
show
Unused Code introduced by
The parameter $qb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
//		$expr = $qb->expr();
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
//		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
103
//
104
//		$qb->andWhere($expr->eq($pf . 'userid', $qb->createNamedParameter($userId)));
105
	}
106
107
108
109
110
111
	protected function limitToMemberLevel(IQueryBuilder &$qb, $level) {
112
		$qb->where(
113
			$qb->expr()
114
			   ->gte('m.level', $qb->createNamedParameter($level))
115
		);
116
	}
117
118
119
	/**
120
	 * @param IQueryBuilder $qb
121
	 *
122
	 * @deprecated
123
	 * never used in fact.
124
	 */
125
	protected function leftJoinOwner(IQueryBuilder &$qb) {
126
127
		if ($qb->getType() !== QueryBuilder::SELECT) {
128
			return;
129
		}
130
131
		$expr = $qb->expr();
132
		$pf = $this->default_select_alias . '.';
133
134
		$qb->leftJoin(
135
			$this->default_select_alias, MembersMapper::TABLENAME, 'o',
136
			$expr->andX(
137
				$expr->eq($pf . 'id', 'o.circle_id'),
138
				$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER))
139
			)
140
		);
141
	}
142
143
144
	/**
145
	 * Base of the Sql Insert request
146
	 *
147
	 * @return IQueryBuilder
148
	 */
149
	protected function getSharesInsertSql() {
150
		$qb = $this->dbConnection->getQueryBuilder();
151
		$qb->insert('circles_shares')
152
		   ->setValue('creation', $qb->createFunction('NOW()'));
153
154
		return $qb;
155
	}
156
157
158
	/**
159
	 * @return IQueryBuilder
160
	 */
161
	protected function getMembersSelectSql() {
162
		$qb = $this->dbConnection->getQueryBuilder();
163
164
		$qb->select('user_id', 'circle_id', 'level', 'status', 'joined')
165
		   ->from('circles_members', 'm');
166
167
		$this->default_select_alias = 'm';
168
169
		return $qb;
170
	}
171
172
173
	/**
174
	 * @return IQueryBuilder
175
	 */
176
	protected function getCirclesSelectSql() {
177
		$qb = $this->dbConnection->getQueryBuilder();
178
179
		$qb->select('c.id', 'c.unique_id', 'c.name', 'c.description', 'c.type', 'c.creation')
180
		   ->from('circles_circles', 'c');
181
		$this->default_select_alias = 'c';
182
183
		return $qb;
184
	}
185
186
	/**
187
	 * @param array $data
188
	 *
189
	 * @return Member
190
	 */
191
	protected function parseMembersSelectSql(array $data) {
192
		$member = new Member($this->l10n);
193
		$member->setUserId($data['user_id']);
194
		$member->setCircleId($data['circle_id']);
195
		$member->setLevel($data['level']);
196
		$member->setStatus($data['status']);
197
		$member->setJoined($data['joined']);
198
199
		return $member;
200
	}
201
202
203
	/**
204
	 * @param array $data
205
	 *
206
	 * @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...
207
	 */
208
	protected function parseCirclesSelectSql(array $data) {
209
		if ($data === null) {
210
			return null;
211
		}
212
213
		$circle = new Circle($this->l10n);
214
		$circle->setId($data['id']);
215
		$circle->setUniqueId($data['unique_id']);
216
		$circle->setName($data['name']);
217
		$circle->setDescription($data['description']);
218
		$circle->setType($data['type']);
219
		$circle->setCreation($data['creation']);
220
221
		return $circle;
222
	}
223
}