Completed
Pull Request — master (#546)
by Maxence
01:58
created

DeprecatedMembersRequestBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 152
Duplicated Lines 14.47 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 22
loc 152
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getMembersInsertSql() 7 7 1
A getMembersUpdateSql() 0 17 1
A getMembersDeleteSql() 0 6 1
A parseMembersSelectSql() 0 23 2
A parseGroupsSelectSql() 0 18 2
A getMembersSelectSql() 15 15 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
29
namespace OCA\Circles\Db;
30
31
32
use OCA\Circles\Model\DeprecatedMember;
33
use OCA\Circles\Service\ConfigService;
34
use OCA\Circles\Service\MiscService;
35
use OCA\Circles\Service\TimezoneService;
36
use OCP\DB\QueryBuilder\IQueryBuilder;
37
use OCP\IDBConnection;
38
use OCP\IGroupManager;
39
use OCP\IL10N;
40
41
class DeprecatedMembersRequestBuilder extends DeprecatedRequestBuilder {
42
43
44
	/** @var IGroupManager */
45
	protected $groupManager;
46
47
	/**
48
	 * CirclesRequestBuilder constructor.
49
	 *
50
	 * {@inheritdoc}
51
	 * @param IGroupManager $groupManager
52
	 */
53
	public function __construct(
54
		IL10N $l10n, IDBConnection $connection, IGroupManager $groupManager,
55
		ConfigService $configService, TimezoneService $timezoneService, MiscService $miscService
56
	) {
57
		parent::__construct($l10n, $connection, $configService, $timezoneService, $miscService);
58
		$this->groupManager = $groupManager;
59
	}
60
61
62
	/**
63
	 * Base of the Sql Insert request for Shares
64
	 *
65
	 * @return IQueryBuilder
66
	 */
67 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...
68
		$qb = $this->dbConnection->getQueryBuilder();
69
		$qb->insert(self::TABLE_MEMBERS)
70
		   ->setValue('joined', $qb->createNamedParameter($this->timezoneService->getUTCDate()));
71
72
		return $qb;
73
	}
74
75
76
	/**
77
	 * @return IQueryBuilder
78
	 */
79 View Code Duplication
	protected function getMembersSelectSql() {
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...
80
		$qb = $this->dbConnection->getQueryBuilder();
81
82
		/** @noinspection PhpMethodParametersCountMismatchInspection */
83
		$qb->select(
84
			'm.user_id', 'm.instance', 'm.user_type', 'm.circle_id', 'm.level', 'm.status', 'm.note',
85
			'm.contact_id', 'm.member_id', 'm.cached_name', 'm.cached_update', 'm.contact_meta', 'm.joined'
86
		)
87
		   ->from(self::TABLE_MEMBERS, 'm')
88
		   ->orderBy('m.joined');
89
90
		$this->default_select_alias = 'm';
91
92
		return $qb;
93
	}
94
95
96
	/**
97
	 * Base of the Sql Updte request for Members
98
	 *
99
	 * @param string /$circleId
100
	 * @param string $userId
101
	 * @param string $instance
102
	 * @param int $type
103
	 *
104
	 * @return IQueryBuilder
105
	 */
106
	protected function getMembersUpdateSql(string $circleId, string $userId, string $instance, int $type) {
107
		$qb = $this->dbConnection->getQueryBuilder();
108
		$expr = $qb->expr();
109
110
		/** @noinspection PhpMethodParametersCountMismatchInspection */
111
		$qb->update(self::TABLE_MEMBERS)
112
		   ->where(
113
			   $expr->andX(
114
				   $expr->eq('circle_id', $qb->createNamedParameter($circleId)),
115
				   $expr->eq('user_id', $qb->createNamedParameter($userId)),
116
				   $expr->eq('instance', $qb->createNamedParameter($instance)),
117
				   $expr->eq('user_type', $qb->createNamedParameter($type))
118
			   )
119
		   );
120
121
		return $qb;
122
	}
123
124
125
	/**
126
	 * Base of the Sql Delete request for Members
127
	 *
128
	 * @return IQueryBuilder
129
	 */
130
	protected function getMembersDeleteSql() {
131
		$qb = $this->dbConnection->getQueryBuilder();
132
		$qb->delete(DeprecatedRequestBuilder::TABLE_MEMBERS);
133
134
		return $qb;
135
	}
136
137
138
	/**
139
	 * @param array $data
140
	 *
141
	 * @return DeprecatedMember
142
	 */
143
	protected function parseMembersSelectSql(array $data) {
144
		$member = new DeprecatedMember($data['user_id'], $data['user_type'], $data['circle_id']);
145
		$member->setNote($data['note']);
146
		$member->setContactId($data['contact_id']);
147
		$member->setMemberId($data['member_id']);
148
		$member->setCachedName($data['cached_name']);
149
		$member->setCachedUpdate($this->timezoneService->convertToTimestamp($data['cached_update']));
150
151
		$contactMeta = json_decode($data['contact_meta'], true);
152
		if (is_array($contactMeta)) {
153
			$member->setContactMeta($contactMeta);
154
		}
155
156
		$member->setLevel($data['level']);
157
		$member->setInstance($data['instance']);
158
		$member->setStatus($data['status']);
159
		$member->setJoined($this->timezoneService->convertTimeForCurrentUser($data['joined']));
160
161
		$joined = $this->timezoneService->convertToTimestamp($data['joined']);
162
		$member->setJoinedSince(time() - $joined);
163
164
		return $member;
165
	}
166
167
168
	/**
169
	 * @param array $data
170
	 *
171
	 * @return DeprecatedMember
172
	 */
173
	protected function parseGroupsSelectSql(array $data) {
174
		$member = new DeprecatedMember();
175
		$member->setCircleId($data['circle_id']);
176
		$member->setNote($data['note']);
177
		$member->setLevel($data['level']);
178
179
		if (key_exists('user_id', $data)) {
180
			$member->setType(DeprecatedMember::TYPE_USER);
181
			$member->setUserId($data['user_id']);
182
		} else {
183
			$member->setType(DeprecatedMember::TYPE_GROUP);
184
			$member->setUserId($data['group_id']);
185
		}
186
187
		$member->setJoined($this->timezoneService->convertTimeForCurrentUser($data['joined']));
188
189
		return $member;
190
	}
191
192
}
193