Completed
Push — master ( 70edb2...6362ed )
by Maxence
02:18
created

MembersRequestBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
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 OC\L10N\L10N;
32
use OCA\Circles\Model\Member;
33
use OCA\Circles\Service\ConfigService;
34
use OCA\Circles\Service\MiscService;
35
use OCP\DB\QueryBuilder\IQueryBuilder;
36
use OCP\IDBConnection;
37
use OCP\IGroupManager;
38
39
class MembersRequestBuilder extends CoreRequestBuilder {
40
41
42
	/** @var IGroupManager */
43
	protected $groupManager;
44
45
	/**
46
	 * CirclesRequestBuilder constructor.
47
	 *
48
	 * {@inheritdoc}
49
	 * @param IGroupManager $groupManager
50
	 */
51
	public function __construct(
52
		L10N $l10n, IDBConnection $connection, IGroupManager $groupManager,
53
		ConfigService $configService, MiscService $miscService
54
	) {
55
		parent::__construct($l10n, $connection, $configService, $miscService);
56
		$this->groupManager = $groupManager;
57
	}
58
59
60
	/**
61
	 * Base of the Sql Insert request for Shares
62
	 *
63
	 * @return IQueryBuilder
64
	 */
65 View Code Duplication
	protected function getMembersInsertSql() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
		$qb = $this->dbConnection->getQueryBuilder();
67
		$qb->insert(self::TABLE_MEMBERS)
68
		   ->setValue('joined', $qb->createFunction('NOW()'));
69
70
		return $qb;
71
	}
72
73
74
	/**
75
	 * @return IQueryBuilder
76
	 */
77
	protected function getMembersSelectSql() {
78
		$qb = $this->dbConnection->getQueryBuilder();
79
80
		/** @noinspection PhpMethodParametersCountMismatchInspection */
81
		$qb->select('m.user_id', 'm.circle_id', 'm.level', 'm.status', 'm.note', 'm.joined')
82
		   ->from(self::TABLE_MEMBERS, 'm');
83
84
		$this->default_select_alias = 'm';
85
86
		return $qb;
87
	}
88
89
90
	/**
91
	 * @return IQueryBuilder
92
	 */
93
	protected function getGroupsSelectSql() {
94
		$qb = $this->dbConnection->getQueryBuilder();
95
96
		/** @noinspection PhpMethodParametersCountMismatchInspection */
97
		$qb->select('g.circle_id', 'g.group_id', 'g.level', 'g.note', 'g.joined')
98
		   ->from(self::TABLE_GROUPS, 'g');
99
		$this->default_select_alias = 'g';
100
101
		return $qb;
102
	}
103
104
105
	/**
106
	 * Base of the Sql Insert request for Shares
107
	 *
108
	 * @return IQueryBuilder
109
	 */
110 View Code Duplication
	protected function getGroupsInsertSql() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
		$qb = $this->dbConnection->getQueryBuilder();
112
		$qb->insert(self::TABLE_GROUPS)
113
		   ->setValue('joined', $qb->createFunction('NOW()'));
114
115
		return $qb;
116
	}
117
118
119
	/**
120
	 * Base of the Sql Update request for Groups
121
	 *
122
	 * @param int $circleId
123
	 * @param string $groupId
124
	 *
125
	 * @return IQueryBuilder
126
	 */
127 View Code Duplication
	protected function getGroupsUpdateSql($circleId, $groupId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
		$qb = $this->dbConnection->getQueryBuilder();
129
		$expr = $qb->expr();
130
131
		/** @noinspection PhpMethodParametersCountMismatchInspection */
132
		$qb->update(self::TABLE_GROUPS)
133
		   ->where(
134
			   $expr->andX(
135
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
136
				   $expr->eq('group_id', $qb->createNamedParameter($groupId))
137
			   )
138
		   );
139
140
		return $qb;
141
	}
142
143
	/**
144
	 * Base of the Sql Updte request for Members
145
	 *
146
	 * @param int $circleId
147
	 * @param string $userId
148
	 *
149
	 * @return IQueryBuilder
150
	 */
151 View Code Duplication
	protected function getMembersUpdateSql($circleId, $userId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
		$qb = $this->dbConnection->getQueryBuilder();
153
		$expr = $qb->expr();
154
155
		/** @noinspection PhpMethodParametersCountMismatchInspection */
156
		$qb->update(self::TABLE_MEMBERS)
157
		   ->where(
158
			   $expr->andX(
159
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
160
				   $expr->eq('user_id', $qb->createNamedParameter($userId))
161
			   )
162
		   );
163
164
		return $qb;
165
	}
166
167
168
	/**
169
	 * Base of the Sql Delete request for Groups
170
	 *
171
	 * @param string $groupId
172
	 *
173
	 * @return IQueryBuilder
174
	 */
175 View Code Duplication
	protected function getGroupsDeleteSql($groupId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
		$qb = $this->dbConnection->getQueryBuilder();
177
		$expr = $qb->expr();
178
179
		$qb->delete(CoreRequestBuilder::TABLE_GROUPS)
180
		   ->where($expr->eq('group_id', $qb->createNamedParameter($groupId)));
181
182
		return $qb;
183
	}
184
185
	/**
186
	 * Base of the Sql Delete request for Members
187
	 *
188
	 * @param string $uniqueCircleId
189
	 * @param string $userId
190
	 *
191
	 * @return IQueryBuilder
192
	 */
193
	protected function getMembersDeleteSql($uniqueCircleId, $userId) {
194
		$qb = $this->dbConnection->getQueryBuilder();
195
		$expr = $qb->expr();
196
197
		$and = $expr->andX();
198
		if ($uniqueCircleId > 0) {
199
			$and->add($expr->eq('circle_id', $qb->createNamedParameter($uniqueCircleId)));
200
		}
201
		if ($userId !== '') {
202
			$and->add($expr->eq('user_id', $qb->createNamedParameter($userId)));
203
		}
204
205
		$qb->delete(CoreRequestBuilder::TABLE_MEMBERS)
206
		   ->where($and);
207
208
		return $qb;
209
	}
210
211
212
	/**
213
	 * @param array $data
214
	 *
215
	 * @return Member
216
	 */
217 View Code Duplication
	protected function parseMembersSelectSql(array $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
		$member = new Member($this->l10n);
219
		$member->setUserId($data['user_id']);
220
		$member->setCircleId($data['circle_id']);
221
		$member->setNote($data['note']);
222
		$member->setLevel($data['level']);
223
		$member->setStatus($data['status']);
224
		$member->setJoined($data['joined']);
225
226
		return $member;
227
	}
228
229
	/**
230
	 * @param array $data
231
	 *
232
	 * @return Member
233
	 */
234 View Code Duplication
	protected function parseGroupsSelectSql(array $data) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
		$member = new Member($this->l10n);
236
		$member->setCircleId($data['circle_id']);
237
		$member->setNote($data['note']);
238
		$member->setLevel($data['level']);
239
		$member->setGroupId($data['group_id']);
240
241
		if (key_exists('user_id', $data)) {
242
			$member->setUserId($data['user_id']);
243
		}
244
245
		$member->setJoined($data['joined']);
246
247
		return $member;
248
	}
249
250
}