Completed
Pull Request — master (#362)
by Maxence
01:49
created

GSSharesRequest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 181
Duplicated Lines 8.29 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 3
cbo 5
dl 15
loc 181
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 1
A getForUser() 0 15 2
A removeGSSharesFromMember() 0 8 1
A joinMembership() 0 14 1
A leftJoinMountPoint() 0 12 1
A getShareMountPointByPath() 0 17 2
A getShareMountPointById() 15 15 2
A generateShareMountPoint() 0 11 2
A updateShareMountPoint() 0 14 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
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Circles\Db;
29
30
31
use daita\MySmallPhpTools\Traits\TStringTools;
32
use OCA\Circles\Model\GlobalScale\GSShare;
33
use OCA\Circles\Model\GlobalScale\GSShareMountpoint;
34
use OCA\Circles\Model\Member;
35
use OCP\DB\QueryBuilder\IQueryBuilder;
36
use OCP\Share\Exceptions\ShareNotFound;
37
38
39
/**
40
 * Class GSSharesRequest
41
 *
42
 * @package OCA\Circles\Db
43
 */
44
class GSSharesRequest extends GSSharesRequestBuilder {
45
46
47
	use TStringTools;
48
49
50
	/**
51
	 * @param GSShare $gsShare
52
	 */
53
	public function create(GSShare $gsShare): void {
54
		$hash = $this->token();
55
		$qb = $this->getGSSharesInsertSql();
56
		$qb->setValue('circle_id', $qb->createNamedParameter($gsShare->getCircleId()))
57
		   ->setValue('owner', $qb->createNamedParameter($gsShare->getOwner()))
58
		   ->setValue('instance', $qb->createNamedParameter($gsShare->getInstance()))
59
		   ->setValue('token', $qb->createNamedParameter($gsShare->getToken()))
60
		   ->setValue('parent', $qb->createNamedParameter($gsShare->getParent()))
61
		   ->setValue('mountpoint', $qb->createNamedParameter($gsShare->getMountPoint()))
62
		   ->setValue('mountpoint_hash', $qb->createNamedParameter($hash));
63
		$qb->execute();
64
	}
65
66
67
	/**
68
	 * @param string $userId
69
	 *
70
	 * @return GSShare[]
71
	 */
72
	public function getForUser(string $userId): array {
73
		$qb = $this->getGSSharesSelectSql();
74
75
		$this->joinMembership($qb, $userId);
76
		$this->leftJoinMountPoint($qb, $userId);
77
78
		$shares = [];
79
		$cursor = $qb->execute();
80
		while ($data = $cursor->fetch()) {
81
			$shares[] = $this->parseGSSharesSelectSql($data);
82
		}
83
		$cursor->closeCursor();
84
85
		return $shares;
86
	}
87
88
89
	/**
90
	 * @param Member $member
91
	 */
92
	public function removeGSSharesFromMember(Member $member) {
93
		$qb = $this->getGSSharesDeleteSql();
94
		$this->limitToCircleId($qb, $member->getCircleId());
95
		$this->limitToInstance($qb, $member->getInstance());
96
		$this->limitToOwner($qb, $member->getUserId());
97
98
		$qb->execute();
99
	}
100
101
102
	/**
103
	 * @param IQueryBuilder $qb
104
	 * @param string $userId
105
	 */
106
	private function joinMembership(IQueryBuilder $qb, string $userId) {
107
		$qb->from(CoreRequestBuilder::TABLE_MEMBERS, 'm');
108
109
		$expr = $qb->expr();
110
		$andX = $expr->andX();
111
112
		$andX->add($expr->eq('m.user_id', $qb->createNamedParameter($userId)));
113
		$andX->add($expr->eq('m.instance', $qb->createNamedParameter('')));
114
		$andX->add($expr->gt('m.level', $qb->createNamedParameter(0)));
115
		$andX->add($expr->eq('m.user_type', $qb->createNamedParameter(Member::TYPE_USER)));
116
		$andX->add($expr->eq('m.circle_id', 'gsh.circle_id'));
117
118
		$qb->andWhere($andX);
119
	}
120
121
122
	private function leftJoinMountPoint(IQueryBuilder $qb, string $userId) {
123
		$expr = $qb->expr();
124
		$pf = '' . $this->default_select_alias . '.';
125
126
		$on = $expr->andX();
127
		$on->add($expr->eq('mp.user_id', $qb->createNamedParameter($userId)));
128
		$on->add($expr->eq('mp.share_id', $pf . 'id'));
129
130
		/** @noinspection PhpMethodParametersCountMismatchInspection */
131
		$qb->selectAlias('mp.mountPoint', 'gsshares_mountpoint')
132
		   ->leftJoin($this->default_select_alias, CoreRequestBuilder::TABLE_GSSHARES_MOUNTPOINT, 'mp', $on);
133
	}
134
135
136
	/**
137
	 * @param string $userId
138
	 * @param string $target
139
	 *
140
	 * @return GSShareMountpoint
141
	 * @throws ShareNotFound
142
	 */
143
	public function getShareMountPointByPath(string $userId, string $target): GSShareMountpoint {
144
		$qb = $this->getGSSharesMountpointSelectSql();
145
146
		$targetHash = md5($target);
147
		$this->limitToUserId($qb, $userId);
148
		$this->limitToMountpointHash($qb, $targetHash);
149
150
		$shares = [];
0 ignored issues
show
Unused Code introduced by
$shares is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
		$cursor = $qb->execute();
152
		$data = $cursor->fetch();
153
154
		if ($data === false) {
155
			throw new ShareNotFound();
156
		}
157
158
		return $this->parseGSSharesMountpointSelectSql($data);
159
	}
160
161
162
	/**
163
	 * @param int $gsShareId
164
	 * @param string $userId
165
	 *
166
	 * @return GSShareMountpoint
167
	 * @throws ShareNotFound
168
	 */
169 View Code Duplication
	public function getShareMountPointById(int $gsShareId, string $userId): GSShareMountpoint {
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...
170
		$qb = $this->getGSSharesMountpointSelectSql();
171
172
		$this->limitToShareId($qb, $gsShareId);
173
		$this->limitToUserId($qb, $userId);
174
175
		$shares = [];
0 ignored issues
show
Unused Code introduced by
$shares is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
		$cursor = $qb->execute();
177
		$data = $cursor->fetch();
178
		if ($data === false) {
179
			throw new ShareNotFound();
180
		}
181
182
		return $this->parseGSSharesMountpointSelectSql($data);
183
	}
184
185
186
	/**
187
	 * @param GSShareMountpoint $mountpoint
188
	 */
189
	public function generateShareMountPoint(GSShareMountpoint $mountpoint) {
190
		$qb = $this->getGSSharesMountpointInsertSql();
191
192
		$hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint());
193
194
		$qb->setValue('user_id', $qb->createNamedParameter($mountpoint->getUserId()))
195
		   ->setValue('share_id', $qb->createNamedParameter($mountpoint->getShareId()))
196
		   ->setValue('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint()))
197
		   ->setValue('mountpoint_hash', $qb->createNamedParameter($hash));
198
		$qb->execute();
199
	}
200
201
202
	/**
203
	 * @param GSShareMountpoint $mountpoint
204
	 *
205
	 * @return bool
206
	 */
207
	public function updateShareMountPoint(GSShareMountpoint $mountpoint) {
208
		$qb = $this->getGSSharesMountpointUpdateSql();
209
210
		$hash = ($mountpoint->getMountPoint() === '-') ? '' : md5($mountpoint->getMountPoint());
211
212
		$qb->set('mountpoint', $qb->createNamedParameter($mountpoint->getMountPoint()))
213
		   ->set('mountpoint_hash', $qb->createNamedParameter($hash));
214
215
		$this->limitToShareId($qb, $mountpoint->getShareId());
216
		$this->limitToUserId($qb, $mountpoint->getUserId());
217
		$nb = $qb->execute();
218
219
		return ($nb === 1);
220
	}
221
222
223
224
}
225
226