Completed
Push — master ( 94366e...dc9b8a )
by Maxence
04:37 queued 01:50
created

MembersRequestBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 16
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A limitToMemberLevel() 0 7 2
A getGroupsSelectSql() 0 10 1
A getGroupsInsertSql() 7 7 1
A getGroupsUpdateSql() 0 15 1
A getGroupsDeleteSql() 9 9 1

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 OCP\DB\QueryBuilder\IQueryBuilder;
32
use Doctrine\DBAL\Query\QueryBuilder;
33
34
class MembersRequestBuilder extends CoreRequestBuilder {
35
36
	const TABLE_GROUPS = 'circles_groups';
37
	const TABLE_MEMBERS = 'circles_members';
38
39
40
	/**
41
	 * Limit the request to a minimum member level.
42
	 *
43
	 * @param IQueryBuilder $qb
44
	 * @param integer $level
45
	 */
46
	protected function limitToMemberLevel(IQueryBuilder & $qb, $level) {
47
		$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : '';
48
		$qb->andWhere(
49
			$qb->expr()
50
			   ->gte($pf . 'level', $qb->createNamedParameter($level))
51
		);
52
	}
53
54
55
	/**
56
	 * @return IQueryBuilder
57
	 */
58
	protected function getGroupsSelectSql() {
59
		$qb = $this->dbConnection->getQueryBuilder();
60
61
		/** @noinspection PhpMethodParametersCountMismatchInspection */
62
		$qb->select('g.circle_id', 'g.group_id', 'g.level', 'g.note', 'g.joined')
63
		   ->from(self::TABLE_GROUPS, 'g');
64
		$this->default_select_alias = 'g';
65
66
		return $qb;
67
	}
68
69
70
	/**
71
	 * Base of the Sql Insert request for Shares
72
	 *
73
	 * @return IQueryBuilder
74
	 */
75 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...
76
		$qb = $this->dbConnection->getQueryBuilder();
77
		$qb->insert(self::TABLE_GROUPS)
78
		   ->setValue('joined', $qb->createFunction('NOW()'));
79
80
		return $qb;
81
	}
82
83
84
	/**
85
	 * Base of the Sql Insert request for Shares
86
	 *
87
	 * @param int $circleId
88
	 * @param string $groupId
89
	 *
90
	 * @return IQueryBuilder
91
	 */
92
	protected function getGroupsUpdateSql($circleId, $groupId) {
93
		$qb = $this->dbConnection->getQueryBuilder();
94
		$expr = $qb->expr();
95
96
		/** @noinspection PhpMethodParametersCountMismatchInspection */
97
		$qb->update(self::TABLE_GROUPS)
98
		   ->where(
99
			   $expr->andX(
100
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
101
				   $expr->eq('group_id', $qb->createNamedParameter($groupId))
102
			   )
103
		   );
104
105
		return $qb;
106
	}
107
108
109
	/**
110
	 * Base of the Sql Insert request for Shares
111
	 *
112
	 * @param string $groupId
113
	 *
114
	 * @return IQueryBuilder
115
	 */
116 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...
117
		$qb = $this->dbConnection->getQueryBuilder();
118
		$expr = $qb->expr();
119
120
		$qb->delete(self::TABLE_GROUPS)
121
		   ->where($expr->eq('group_id', $qb->createNamedParameter($groupId)));
122
123
		return $qb;
124
	}
125
126
127
}