Completed
Push — master ( 3071ad...3723f6 )
by Maxence
03:35 queued 01:12
created

CoreRequestBuilder::limitToLevel()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 5
eloc 12
nc 4
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: maxence
5
 * Date: 7/4/17
6
 * Time: 5:01 PM
7
 */
8
9
namespace OCA\Circles\Db;
10
11
12
use OCP\DB\QueryBuilder\IQueryBuilder;
13
use Doctrine\DBAL\Query\QueryBuilder;
14
use OC\L10N\L10N;
15
use OCA\Circles\Service\MiscService;
16
use OCP\IDBConnection;
17
18
class CoreRequestBuilder {
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...
19
20
	const TABLE_CIRCLES = 'circles_circles';
21
	const TABLE_MEMBERS = 'circles_members';
22
	const TABLE_GROUPS = 'circles_groups';
23
	const TABLE_SHARES = 'circles_shares';
24
	const TABLE_LINKS = 'circles_links';
25
26
	const NC_TABLE_GROUP_USER = 'group_user';
27
28
	/** @var IDBConnection */
29
	protected $dbConnection;
30
31
	/** @var L10N */
32
	protected $l10n;
33
34
	/** @var MiscService */
35
	protected $miscService;
36
37
	/** @var string */
38
	protected $default_select_alias;
39
40
41
	/**
42
	 * RequestBuilder constructor.
43
	 *
44
	 * @param L10N $l10n
45
	 * @param IDBConnection $connection
46
	 * @param MiscService $miscService
47
	 */
48
	public function __construct(L10N $l10n, IDBConnection $connection, MiscService $miscService) {
49
		$this->l10n = $l10n;
50
		$this->dbConnection = $connection;
51
		$this->miscService = $miscService;
52
	}
53
54
55
	/**
56
	 * Limit the request by its Id.
57
	 *
58
	 * @param IQueryBuilder $qb
59
	 * @param int $id
60
	 */
61
	protected function limitToId(IQueryBuilder &$qb, $id) {
62
		$this->limitToDBField($qb, 'id', $id);
63
	}
64
65
66
	/**
67
	 * Limit the request by its UniqueId.
68
	 *
69
	 * @param IQueryBuilder $qb
70
	 * @param int $uniqueId
71
	 */
72
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
73
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
74
	}
75
76
77
	/**
78
	 * Limit the request by its Token.
79
	 *
80
	 * @param IQueryBuilder $qb
81
	 * @param string $token
82
	 */
83
	protected function limitToToken(IQueryBuilder &$qb, $token) {
84
		$this->limitToDBField($qb, 'token', $token);
85
	}
86
87
88
	/**
89
	 * Limit the request to the User by its Id.
90
	 *
91
	 * @param IQueryBuilder $qb
92
	 * @param $userId
93
	 *
94
	 * @internal param int $circleId
95
	 */
96
	protected function limitToUserId(IQueryBuilder &$qb, $userId) {
97
		$this->limitToDBField($qb, 'user_id', $userId);
98
	}
99
100
101
	/**
102
	 * Limit the request to the Circle by its Id.
103
	 *
104
	 * @param IQueryBuilder $qb
105
	 * @param int $circleId
106
	 */
107
	protected function limitToCircleId(IQueryBuilder &$qb, $circleId) {
108
		$this->limitToDBField($qb, 'circle_id', $circleId);
109
	}
110
111
112
	/**
113
	 * Limit the request to the Group by its Id.
114
	 *
115
	 * @param IQueryBuilder $qb
116
	 * @param int $groupId
117
	 */
118
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
119
		$this->limitToDBField($qb, 'group_id', $groupId);
120
	}
121
122
123
	/**
124
	 * Limit the search by its Name
125
	 *
126
	 * @param IQueryBuilder $qb
127
	 */
128
	protected function limitToName(IQueryBuilder &$qb, $name) {
129
		$this->limitToDBField($qb, 'name', $name);
130
	}
131
132
133
	/**
134
	 * Limit the request to a minimum member level.
135
	 *
136
	 * @param IQueryBuilder $qb
137
	 * @param int $level
138
	 * @param string|array $pf
139
	 */
140
	protected function limitToLevel(IQueryBuilder &$qb, $level, $pf = '') {
141
		$expr = $qb->expr();
142
		$orX = $expr->orX();
143
144
		if ($pf === '') {
145
			$p = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
146
			$orX->add($expr->gte($p . 'level', $qb->createNamedParameter($level)));
147
148
		} else {
149
150
			if (!is_array($pf)) {
151
				$pf = [$pf];
152
			}
153
154
			foreach ($pf as $p) {
155
				$orX->add($expr->gte($p . '.level', $qb->createNamedParameter($level)));
156
			}
157
		}
158
159
		$qb->andWhere($orX);
160
	}
161
162
163
	/**
164
	 * @param IQueryBuilder $qb
165
	 * @param string $field
166
	 * @param string|integer $value
167
	 */
168
	private function limitToDBField(IQueryBuilder & $qb, $field, $value) {
169
		$expr = $qb->expr();
170
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
171
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
172
	}
173
174
175
	/**
176
	 * link to the groupId/UserId of the NC DB.
177
	 * If userId is empty, we add the uid of the NCGroup Table in the select list with 'user_id'
178
	 * alias
179
	 *
180
	 * @param IQueryBuilder $qb
181
	 * @param string $userId
182
	 */
183
	protected function limitToNCGroupUser(IQueryBuilder $qb, $userId = '') {
184
		$expr = $qb->expr();
185
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
186
187
		$and = $expr->andX($expr->eq($pf . 'group_id', 'ncgu.gid'));
188
		if ($userId !== '') {
189
			$and->add($expr->eq('ncgu.uid', $qb->createNamedParameter($userId)));
190
		} else {
191
			$qb->selectAlias('ncgu.uid', 'user_id');
192
		}
193
194
		$qb->from(self::NC_TABLE_GROUP_USER, 'ncgu');
195
		$qb->andWhere($and);
196
	}
197
198
199
	/**
200
	 * Right Join the Circles table
201
	 *
202
	 * @param IQueryBuilder $qb
203
	 *
204
	 * @deprecated not used (14/07/17)
205
	 */
206
	protected function rightJoinCircles(& $qb) {
207
		$expr = $qb->expr();
208
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
209
210
		$qb->from(self::TABLE_CIRCLES, 'c')
211
		   ->andWhere($expr->eq('c.id', $pf . 'circle_id'));
212
	}
213
214
215
	/**
216
	 * link to the groupId/UserId of the NC DB.
217
	 *
218
	 * @param IQueryBuilder $qb
219
	 * @param string $fieldGroup
220
	 * @param string $userId
221
	 */
222
	protected function leftJoinNCGroupAndUser(IQueryBuilder $qb, $fieldGroup, $userId) {
223
		$expr = $qb->expr();
224
225
		$qb->leftJoin(
226
			$this->default_select_alias, self::NC_TABLE_GROUP_USER, 'ncgu',
227
			$expr->andX(
228
				$expr->eq('ncgu.gid', $fieldGroup),
229
				$expr->eq('ncgu.uid', $qb->createNamedParameter($userId))
230
			)
231
		);
232
	}
233
}