Completed
Push — federated-circles ( 545292...450355 )
by Maxence
02:38
created

CirclesRequestBuilder::leftJoinUserIdAsMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
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\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
	 * Limit the request to the Share by its Id.
81
	 *
82
	 * @param IQueryBuilder $qb
83
	 * @param int $id
84
	 */
85
	protected function limitToId(IQueryBuilder &$qb, $id) {
86
		$expr = $qb->expr();
87
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
88
89
		$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($id)));
90
	}
91
92
93
94
95
	protected function limitToMemberLevel(IQueryBuilder &$qb, $level) {
96
		$qb->where(
97
			$qb->expr()
98
			   ->gte('m.level', $qb->createNamedParameter($level))
99
		);
100
	}
101
102
103
104
105
	/**
106
	 * add a request to the members list, using the current user ID.
107
	 * will returns level and stuff.
108
	 *
109
	 * @param IQueryBuilder $qb
110
	 * @param string $userId
111
	 */
112
	protected function leftJoinUserIdAsMember(IQueryBuilder &$qb, $userId) {
113
114
		if ($qb->getType() !== QueryBuilder::SELECT) {
115
			return;
116
		}
117
118
		$expr = $qb->expr();
119
		$pf = $this->default_select_alias . '.';
120
121
		$qb->selectAlias('u.level', 'user_level');
122
		$qb->leftJoin(
123
			$this->default_select_alias, MembersMapper::TABLENAME, 'u',
124
			$expr->andX(
125
				$expr->eq($pf . 'id', 'u.circle_id'),
126
				$expr->eq('u.user_id', $qb->createNamedParameter($userId))
127
			)
128
		);
129
	}
130
131
		/**
132
	 * @param IQueryBuilder $qb
133
	 *
134
	 * @deprecated
135
	 * never used in fact.
136
	 */
137
	protected function leftJoinOwner(IQueryBuilder &$qb) {
138
139
		if ($qb->getType() !== QueryBuilder::SELECT) {
140
			return;
141
		}
142
143
		$expr = $qb->expr();
144
		$pf = $this->default_select_alias . '.';
145
146
		$qb->leftJoin(
147
			$this->default_select_alias, MembersMapper::TABLENAME, 'o',
148
			$expr->andX(
149
				$expr->eq($pf . 'id', 'o.circle_id'),
150
				$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER))
151
			)
152
		);
153
	}
154
155
156
	/**
157
	 * Base of the Sql Insert request
158
	 *
159
	 * @return IQueryBuilder
160
	 */
161
	protected function getSharesInsertSql() {
162
		$qb = $this->dbConnection->getQueryBuilder();
163
		$qb->insert('circles_shares')
164
		   ->setValue('creation', $qb->createFunction('NOW()'));
165
166
		return $qb;
167
	}
168
169
170
	/**
171
	 * @return IQueryBuilder
172
	 */
173
	protected function getMembersSelectSql() {
174
		$qb = $this->dbConnection->getQueryBuilder();
175
176
		$qb->select('user_id', 'circle_id', 'level', 'status', 'joined')
177
		   ->from('circles_members', 'm');
178
179
		$this->default_select_alias = 'm';
180
181
		return $qb;
182
	}
183
184
185
	/**
186
	 * @return IQueryBuilder
187
	 */
188
	protected function getCirclesSelectSql() {
189
		$qb = $this->dbConnection->getQueryBuilder();
190
191
		$qb->select('c.id', 'c.unique_id', 'c.name', 'c.description', 'c.type', 'c.creation')
192
		   ->from('circles_circles', 'c');
193
		$this->default_select_alias = 'c';
194
195
		return $qb;
196
	}
197
198
	/**
199
	 * @param array $data
200
	 *
201
	 * @return Member
202
	 */
203
	protected function parseMembersSelectSql(array $data) {
204
		$member = new Member($this->l10n);
205
		$member->setUserId($data['user_id']);
206
		$member->setCircleId($data['circle_id']);
207
		$member->setLevel($data['level']);
208
		$member->setStatus($data['status']);
209
		$member->setJoined($data['joined']);
210
211
		return $member;
212
	}
213
214
215
	/**
216
	 * @param array $data
217
	 *
218
	 * @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...
219
	 */
220
	protected function parseCirclesSelectSql(array $data) {
221
		if ($data === null) {
222
			return null;
223
		}
224
225
		$circle = new Circle($this->l10n);
226
		$circle->setId($data['id']);
227
		$circle->setUniqueId($data['unique_id']);
228
		$circle->setName($data['name']);
229
		$circle->setDescription($data['description']);
230
		$circle->setType($data['type']);
231
		$circle->setCreation($data['creation']);
232
233
		if (key_exists('user_level', $data)) {
234
			$user = new Member($this->l10n);
235
			$user->setLevel($data['user_level']);
236
			$circle->setUser($user);
237
		}
238
239
		return $circle;
240
	}
241
}