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

CirclesRequest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 242
Duplicated Lines 26.45 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 5
dl 64
loc 242
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMembers() 0 21 3
B getCircleFromId() 0 26 4
A saveFrame() 14 14 1
A updateFrame() 13 13 1
A updateCircle() 0 8 1
A getCircleFromUniqueId() 0 17 2
A getFrame() 0 11 1
A getLinkFromToken() 19 19 3
A getLinkFromId() 18 18 3
A getLinksFromCircle() 0 16 3

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 OC\L10N\L10N;
32
use OCA\Circles\Exceptions\CircleDoesNotExistException;
33
use OCA\Circles\Exceptions\FederatedLinkDoesNotExistException;
34
use OCA\Circles\Model\Circle;
35
use OCA\Circles\Model\FederatedLink;
36
use OCA\Circles\Model\Member;
37
use OCA\Circles\Model\SharingFrame;
38
use OCA\Circles\Service\MiscService;
39
use OCP\IDBConnection;
40
41
class CirclesRequest extends CirclesRequestBuilder {
42
43
44
	/**
45
	 * @param int $circleId
46
	 * @param string $userId
47
	 *
48
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
49
	 * @throws CircleDoesNotExistException
50
	 */
51
	public function getCircleFromId($circleId, $userId = '') {
52
		$qb = $this->getCirclesSelectSql();
53
54
		$this->limitToId($qb, $circleId);
55
		if ($userId !== '') {
56
			$this->leftJoinUserIdAsMember($qb, $userId);
57
		}
58
59
60
//		$this->leftjoinOwner($qb);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
//		$this->buildWithMemberLevel($qb, 'u.level', $level);
62
//		$this->buildWithCircleId($qb, 'c.id', $circleId);
63
//		$this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId);
64
65
		$cursor = $qb->execute();
66
		$data = $cursor->fetch();
67
		if ($data === false || $data === null) {
68
			throw new CircleDoesNotExistException(
69
				$this->l10n->t('Circle not found')
70
			);
71
		}
72
73
		$entry = $this->parseCirclesSelectSql($data);
74
75
		return $entry;
76
	}
77
78
79
	/**
80
	 * saveFrame()
81
	 *
82
	 * Insert a new entry in the database to save the SharingFrame.
83
	 *
84
	 * @param SharingFrame $frame
85
	 */
86 View Code Duplication
	public function saveFrame(SharingFrame $frame) {
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...
87
88
		$qb = $this->getSharesInsertSql();
89
		$qb->setValue('circle_id', $qb->createNamedParameter($frame->getCircleId()))
90
		   ->setValue('source', $qb->createNamedParameter($frame->getSource()))
91
		   ->setValue('type', $qb->createNamedParameter($frame->getType()))
92
		   ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true)))
93
		   ->setValue('author', $qb->createNamedParameter($frame->getAuthor()))
94
		   ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
95
		   ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
96
		   ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true)));
97
98
		$qb->execute();
99
	}
100
101
102 View Code Duplication
	public function updateFrame(SharingFrame $frame) {
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...
103
		$qb = $this->getSharesUpdateSql($frame->getUniqueId());
104
		$qb->set('circle_id', $qb->createNamedParameter($frame->getCircleId()))
105
		   ->set('source', $qb->createNamedParameter($frame->getSource()))
106
		   ->set('type', $qb->createNamedParameter($frame->getType()))
107
		   ->set('headers', $qb->createNamedParameter($frame->getHeaders(true)))
108
		   ->set('author', $qb->createNamedParameter($frame->getAuthor()))
109
		   ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
110
		   ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
111
		   ->set('payload', $qb->createNamedParameter($frame->getPayload(true)));
112
113
		$qb->execute();
114
	}
115
116
117
	public function updateCircle(Circle $circle) {
118
		$qb = $this->getCirclesUpdateSql($circle->getId());
119
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
120
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
121
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
122
123
		$qb->execute();
124
	}
125
126
127
	/**
128
	 * @param string $uniqueId
129
	 *
130
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
131
	 * @throws CircleDoesNotExistException
132
	 */
133
	public function getCircleFromUniqueId($uniqueId) {
134
		$qb = $this->getCirclesSelectSql();
135
		$this->limitToUniqueId($qb, (string)$uniqueId);
136
137
		$cursor = $qb->execute();
138
		$data = $cursor->fetch();
139
140
		if ($data === false) {
141
			throw new CircleDoesNotExistException(
142
				$this->l10n->t('Circle not found')
143
			);
144
		}
145
		$entry = $this->parseCirclesSelectSql($data);
146
		$cursor->closeCursor();
147
148
		return $entry;
149
	}
150
151
152
	/**
153
	 * @param int $circleId
154
	 * @param string $uniqueId
155
	 *
156
	 * @return SharingFrame
0 ignored issues
show
Documentation introduced by
Should the return type not be SharingFrame|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
157
	 */
158
	public function getFrame($circleId, $uniqueId) {
159
		$qb = $this->getSharesSelectSql();
160
		$this->limitToUniqueId($qb, (string)$uniqueId);
161
		$this->limitToCircleId($qb, (int)$circleId);
162
163
		$cursor = $qb->execute();
164
		$data = $cursor->fetch();
165
		$entry = $this->parseSharesSelectSql($data);
166
167
		return $entry;
168
	}
169
170
171
	/**
172
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
173
	 *
174
	 * @param string $token
175
	 * @param string $uniqueId
176
	 *
177
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
178
	 * @throws FederatedLinkDoesNotExistException
179
	 */
180 View Code Duplication
	public function getLinkFromToken($token, $uniqueId) {
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...
181
		$qb = $this->getLinksSelectSql();
182
		$this->limitToUniqueId($qb, (string)$uniqueId);
183
		$this->limitToToken($qb, (string)$token);
184
185
		$cursor = $qb->execute();
186
		$data = $cursor->fetch();
187
188
		if ($data === false || $data === null) {
189
			throw new FederatedLinkDoesNotExistException(
190
				$this->l10n->t('Federated Link not found')
191
			);
192
		}
193
194
		$entry = $this->parseLinksSelectSql($data);
195
		$cursor->closeCursor();
196
197
		return $entry;
198
	}
199
200
201
	/**
202
	 * return the FederatedLink identified by a its Id
203
	 *
204
	 * @param int $linkId
205
	 *
206
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
207
	 * @throws FederatedLinkDoesNotExistException
208
	 */
209 View Code Duplication
	public function getLinkFromId($linkId) {
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...
210
		$qb = $this->getLinksSelectSql();
211
		$this->limitToId($qb, (string)$linkId);
212
213
		$cursor = $qb->execute();
214
		$data = $cursor->fetch();
215
		$cursor->closeCursor();
216
217
		if ($data === false || $data === null) {
218
			throw new FederatedLinkDoesNotExistException(
219
				$this->l10n->t('Federated Link not found')
220
			);
221
		}
222
223
		$entry = $this->parseLinksSelectSql($data);
224
225
		return $entry;
226
	}
227
228
229
	/**
230
	 * returns all FederatedLink from a circle
231
	 *
232
	 * @param int $circleId
233
	 *
234
	 * @return FederatedLink[]
235
	 */
236
	public function getLinksFromCircle($circleId) {
237
		$qb = $this->getLinksSelectSql();
238
		$this->limitToCircleId($qb, $circleId);
239
240
		$links = [];
241
		$cursor = $qb->execute();
242
		while ($data = $cursor->fetch()) {
243
			$link = $this->parseLinksSelectSql($data);
244
			if ($link !== null) {
245
				$links[] = $link;
246
			}
247
		}
248
		$cursor->closeCursor();
249
250
		return $links;
251
	}
252
253
	/**
254
	 * @param integer $circleId
255
	 * @param int $level
256
	 *
257
	 * @return Member[]
258
	 */
259
	public function getMembers($circleId, $level = Member::LEVEL_MEMBER) {
260
		$qb = $this->getMembersSelectSql();
261
		$this->limitToMemberLevel($qb, $level);
262
263
		$this->joinCircles($qb, 'm.circle_id');
264
		$this->limitToCircleId($qb, $circleId);
265
266
		$qb->selectAlias('c.name', 'circle_name');
267
268
		$users = [];
269
		$cursor = $qb->execute();
270
		while ($data = $cursor->fetch()) {
271
			$member = $this->parseMembersSelectSql($data);
272
			if ($member !== null) {
273
				$users[] = $member;
274
			}
275
		}
276
		$cursor->closeCursor();
277
278
		return $users;
279
	}
280
281
282
}