Completed
Push — master ( dc9b8a...1415c3 )
by Maxence
02:30
created

CoreRequestBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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
24
25
	/** @var IDBConnection */
26
	protected $dbConnection;
27
28
	/** @var L10N */
29
	protected $l10n;
30
31
	/** @var MiscService */
32
	protected $miscService;
33
34
	/** @var string */
35
	protected $default_select_alias;
36
37
38
	/**
39
	 * CirclesRequest constructor.
40
	 *
41
	 * @param L10N $l10n
42
	 * @param IDBConnection $connection
43
	 * @param MiscService $miscService
44
	 */
45
	public function __construct(L10N $l10n, IDBConnection $connection, MiscService $miscService) {
46
		$this->l10n = $l10n;
47
		$this->dbConnection = $connection;
48
		$this->miscService = $miscService;
49
	}
50
51
52
	/**
53
	 * Limit the request by its Id.
54
	 *
55
	 * @param IQueryBuilder $qb
56
	 * @param int $id
57
	 */
58
	protected function limitToId(IQueryBuilder &$qb, $id) {
59
		$this->limitToDBField($qb, 'id', $id);
60
	}
61
62
63
	/**
64
	 * Limit the request by its UniqueId.
65
	 *
66
	 * @param IQueryBuilder $qb
67
	 * @param int $uniqueId
68
	 */
69
	protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) {
70
		$this->limitToDBField($qb, 'unique_id', $uniqueId);
71
	}
72
73
74
	/**
75
	 * Limit the request by its Token.
76
	 *
77
	 * @param IQueryBuilder $qb
78
	 * @param string $token
79
	 */
80
	protected function limitToToken(IQueryBuilder &$qb, $token) {
81
		$this->limitToDBField($qb, 'token', $token);
82
	}
83
84
85
	/**
86
	 * Limit the request to the Circle by its Id.
87
	 *
88
	 * @param IQueryBuilder $qb
89
	 * @param int $circleId
90
	 */
91
	protected function limitToCircleId(IQueryBuilder &$qb, $circleId) {
92
		$this->limitToDBField($qb, 'circle_id', $circleId);
93
	}
94
95
96
	/**
97
	 * Limit the request to the Group by its Id.
98
	 *
99
	 * @param IQueryBuilder $qb
100
	 * @param int $groupId
101
	 */
102
	protected function limitToGroupId(IQueryBuilder &$qb, $groupId) {
103
		$this->limitToDBField($qb, 'group_id', $groupId);
104
	}
105
106
107
	/**
108
	 * Limit the request to a minimum member level.
109
	 *
110
	 * @param IQueryBuilder $qb
111
	 * @param integer $level
112
	 */
113
	protected function limitToLevel(IQueryBuilder &$qb, $level) {
114
		$this->limitToDBField($qb, 'level', $level);
115
	}
116
117
118
	/**
119
	 * @param IQueryBuilder $qb
120
	 * @param string $field
121
	 * @param string|integer $value
122
	 */
123
	private function limitToDBField(IQueryBuilder & $qb, $field, $value) {
124
		$expr = $qb->expr();
125
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
126
		$qb->andWhere($expr->eq($pf . $field, $qb->createNamedParameter($value)));
127
	}
128
129
}