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

MembersRequestBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 194
Duplicated Lines 40.72 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 2
cbo 2
dl 79
loc 194
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMembersInsertSql() 7 7 1
A getMembersSelectSql() 0 11 1
A getGroupsSelectSql() 0 10 1
A getGroupsInsertSql() 7 7 1
A getGroupsUpdateSql() 15 15 1
A getMembersUpdateSql() 15 15 1
A getGroupsDeleteSql() 9 9 1
A getMembersDeleteSql() 0 17 3
A parseMembersSelectSql() 11 11 1
A parseGroupsSelectSql() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 OCA\Circles\Model\Member;
32
use OCP\DB\QueryBuilder\IQueryBuilder;
33
34
class MembersRequestBuilder extends CoreRequestBuilder {
35
36
37
	/**
38
	 * Base of the Sql Insert request for Shares
39
	 *
40
	 * @return IQueryBuilder
41
	 */
42 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...
43
		$qb = $this->dbConnection->getQueryBuilder();
44
		$qb->insert(self::TABLE_MEMBERS)
45
		   ->setValue('joined', $qb->createFunction('NOW()'));
46
47
		return $qb;
48
	}
49
50
51
	/**
52
	 * @return IQueryBuilder
53
	 */
54
	protected function getMembersSelectSql() {
55
		$qb = $this->dbConnection->getQueryBuilder();
56
57
		/** @noinspection PhpMethodParametersCountMismatchInspection */
58
		$qb->select('m.user_id', 'm.circle_id', 'm.level', 'm.status', 'm.note', 'm.joined')
59
		   ->from(self::TABLE_MEMBERS, 'm');
60
61
		$this->default_select_alias = 'm';
62
63
		return $qb;
64
	}
65
66
67
	/**
68
	 * @return IQueryBuilder
69
	 */
70
	protected function getGroupsSelectSql() {
71
		$qb = $this->dbConnection->getQueryBuilder();
72
73
		/** @noinspection PhpMethodParametersCountMismatchInspection */
74
		$qb->select('g.circle_id', 'g.group_id', 'g.level', 'g.note', 'g.joined')
75
		   ->from(self::TABLE_GROUPS, 'g');
76
		$this->default_select_alias = 'g';
77
78
		return $qb;
79
	}
80
81
82
	/**
83
	 * Base of the Sql Insert request for Shares
84
	 *
85
	 * @return IQueryBuilder
86
	 */
87 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...
88
		$qb = $this->dbConnection->getQueryBuilder();
89
		$qb->insert(self::TABLE_GROUPS)
90
		   ->setValue('joined', $qb->createFunction('NOW()'));
91
92
		return $qb;
93
	}
94
95
96
	/**
97
	 * Base of the Sql Update request for Groups
98
	 *
99
	 * @param int $circleId
100
	 * @param string $groupId
101
	 *
102
	 * @return IQueryBuilder
103
	 */
104 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...
105
		$qb = $this->dbConnection->getQueryBuilder();
106
		$expr = $qb->expr();
107
108
		/** @noinspection PhpMethodParametersCountMismatchInspection */
109
		$qb->update(self::TABLE_GROUPS)
110
		   ->where(
111
			   $expr->andX(
112
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
113
				   $expr->eq('group_id', $qb->createNamedParameter($groupId))
114
			   )
115
		   );
116
117
		return $qb;
118
	}
119
120
	/**
121
	 * Base of the Sql Updte request for Members
122
	 *
123
	 * @param int $circleId
124
	 * @param string $userId
125
	 *
126
	 * @return IQueryBuilder
127
	 */
128 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...
129
		$qb = $this->dbConnection->getQueryBuilder();
130
		$expr = $qb->expr();
131
132
		/** @noinspection PhpMethodParametersCountMismatchInspection */
133
		$qb->update(self::TABLE_MEMBERS)
134
		   ->where(
135
			   $expr->andX(
136
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
137
				   $expr->eq('user_id', $qb->createNamedParameter($userId))
138
			   )
139
		   );
140
141
		return $qb;
142
	}
143
144
145
	/**
146
	 * Base of the Sql Delete request for Groups
147
	 *
148
	 * @param string $groupId
149
	 *
150
	 * @return IQueryBuilder
151
	 */
152 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...
153
		$qb = $this->dbConnection->getQueryBuilder();
154
		$expr = $qb->expr();
155
156
		$qb->delete(CoreRequestBuilder::TABLE_GROUPS)
157
		   ->where($expr->eq('group_id', $qb->createNamedParameter($groupId)));
158
159
		return $qb;
160
	}
161
162
	/**
163
	 * Base of the Sql Delete request for Members
164
	 *
165
	 * @param int $circleId
166
	 * @param string $userId
167
	 *
168
	 * @return IQueryBuilder
169
	 */
170
	protected function getMembersDeleteSql($circleId, $userId) {
171
		$qb = $this->dbConnection->getQueryBuilder();
172
		$expr = $qb->expr();
173
174
		$and = $expr->andX();
175
		if ($circleId > 0) {
176
			$and->add($expr->eq('circle_id', $qb->createNamedParameter($circleId)));
177
		}
178
		if ($userId !== '') {
179
			$and->add($expr->eq('user_id', $qb->createNamedParameter($userId)));
180
		}
181
182
		$qb->delete(CoreRequestBuilder::TABLE_MEMBERS)
183
		   ->where($and);
184
185
		return $qb;
186
	}
187
188
189
	/**
190
	 * @param array $data
191
	 *
192
	 * @return Member
193
	 */
194 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...
195
		$member = new Member($this->l10n);
196
		$member->setUserId($data['user_id']);
197
		$member->setCircleId($data['circle_id']);
198
		$member->setNote($data['note']);
199
		$member->setLevel($data['level']);
200
		$member->setStatus($data['status']);
201
		$member->setJoined($data['joined']);
202
203
		return $member;
204
	}
205
206
	/**
207
	 * @param array $data
208
	 *
209
	 * @return Member
210
	 */
211 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...
212
		$member = new Member($this->l10n);
213
		$member->setCircleId($data['circle_id']);
214
		$member->setNote($data['note']);
215
		$member->setLevel($data['level']);
216
		$member->setGroupId($data['group_id']);
217
218
		if (key_exists('user_id', $data)) {
219
			$member->setUserId($data['user_id']);
220
		}
221
222
		$member->setJoined($data['joined']);
223
224
		return $member;
225
	}
226
227
}