Completed
Push — master ( f9a32b...1fc0d2 )
by Maxence
04:23
created

FederatedLinksRequest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 206
Duplicated Lines 29.13 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 3
dl 60
loc 206
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 2
B update() 0 25 3
A delete() 0 19 2
A getLinkFromEntry() 0 15 3
A getLinksFromCircle() 14 14 2
A getLinkFromCircle() 15 15 2
A getLinkFromToken() 15 15 2
A getLinkFromId() 16 16 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 OCA\Circles\Exceptions\FederatedLinkDoesNotExistException;
32
use OCA\Circles\Model\FederatedLink;
33
34
class FederatedLinksRequest extends FederatedLinksRequestBuilder {
35
36
37
	/**
38
	 * @param FederatedLink $link
39
	 *
40
	 * @return bool
41
	 * @throws \Exception
42
	 */
43
	public function create(FederatedLink $link) {
44
		try {
45
			$qb = $this->getLinksInsertSql();
46
			$qb->setValue('status', $qb->createNamedParameter($link->getStatus()))
47
			   ->setValue('circle_id', $qb->createNamedParameter($link->getCircleId()))
48
			   ->setValue('unique_id', $qb->createNamedParameter($link->getUniqueId(true)))
49
			   ->setValue('address', $qb->createNamedParameter($link->getAddress()))
50
			   ->setValue('token', $qb->createNamedParameter($link->getToken(true)));
51
52
			$qb->execute();
53
54
			return true;
55
		} catch (\Exception $e) {
56
			throw $e;
57
		}
58
	}
59
60
61
	/**
62
	 * @param FederatedLink $link
63
	 */
64
	public function update(FederatedLink $link) {
65
66
		if ($link->getStatus() === FederatedLink::STATUS_LINK_REMOVE) {
67
			$this->delete($link);
68
69
			return;
70
		}
71
72
		$qb = $this->getLinksUpdateSql();
73
		$expr = $qb->expr();
74
		$qb->set('status', $qb->createNamedParameter($link->getStatus()));
75
		if ($link->getUniqueId() !== '') {
76
			$qb->set('unique_id', $qb->createNamedParameter($link->getUniqueId(true)));
77
		}
78
79
		/** @noinspection PhpMethodParametersCountMismatchInspection */
80
		$qb->andWhere(
81
			$expr->andX(
82
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId())),
83
				$expr->eq('token', $qb->createNamedParameter($link->getToken(true)))
84
			)
85
		);
86
87
		$qb->execute();
88
	}
89
90
91
	/**
92
	 * @param FederatedLink $link
93
	 */
94
	public function delete(FederatedLink $link) {
95
96
		if ($link === null) {
97
			return;
98
		}
99
100
		$qb = $this->getLinksDeleteSql();
101
		$expr = $qb->expr();
102
103
		/** @noinspection PhpMethodParametersCountMismatchInspection */
104
		$qb->andWhere(
105
			$expr->andX(
106
				$expr->eq('token', $qb->createNamedParameter($link->getToken(true))),
107
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId()))
108
			)
109
		);
110
111
		$qb->execute();
112
	}
113
114
115
	/**
116
	 * @param array $data
117
	 *
118
	 * @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...
119
	 */
120
	public function getLinkFromEntry($data) {
121
		if ($data === false || $data === null) {
122
			return null;
123
		}
124
125
		$link = new FederatedLink();
126
		$link->setId($data['id'])
127
			 ->setUniqueId($data['unique_id'])
128
			 ->setStatus($data['status'])
129
			 ->setAddress($data['address'])
130
			 ->setToken($data['token'])
131
			 ->setCircleId($data['circle_id']);
132
133
		return $link;
134
	}
135
136
137
	/**
138
	 * returns all FederatedLink from a circle
139
	 *
140
	 * @param string $circleUniqueId
141
	 * @param int $status
142
	 *
143
	 * @return FederatedLink[]
144
	 */
145 View Code Duplication
	public function getLinksFromCircle($circleUniqueId, $status = 0) {
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...
146
		$qb = $this->getLinksSelectSql();
147
		$this->limitToCircleId($qb, $circleUniqueId);
148
		$this->limitToStatus($qb, $status);
149
150
		$links = [];
151
		$cursor = $qb->execute();
152
		while ($data = $cursor->fetch()) {
153
			$links[] = $this->parseLinksSelectSql($data);
154
		}
155
		$cursor->closeCursor();
156
157
		return $links;
158
	}
159
160
161
	/**
162
	 * returns a FederatedLink from a circle identified by its full unique Id
163
	 *
164
	 * @param string $circleUniqueId
165
	 * @param string $linkUniqueId
166
	 *
167
	 * @return FederatedLink
168
	 * @throws FederatedLinkDoesNotExistException
169
	 */
170 View Code Duplication
	public function getLinkFromCircle($circleUniqueId, $linkUniqueId) {
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...
171
		$qb = $this->getLinksSelectSql();
172
		$this->limitToCircleId($qb, $circleUniqueId);
173
		$this->limitToUniqueId($qb, $linkUniqueId);
174
175
		$cursor = $qb->execute();
176
		$data = $cursor->fetch();
177
		$cursor->closeCursor();
178
179
		if ($data === false) {
180
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
181
		}
182
183
		return $this->parseLinksSelectSql($data);
184
	}
185
186
187
	/**
188
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
189
	 *
190
	 * @param string $token
191
	 * @param string $uniqueId
192
	 *
193
	 * @return FederatedLink
194
	 * @throws FederatedLinkDoesNotExistException
195
	 */
196 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...
197
		$qb = $this->getLinksSelectSql();
198
		$this->limitToUniqueId($qb, (string)$uniqueId);
199
		$this->limitToToken($qb, (string)$token);
200
201
		$cursor = $qb->execute();
202
		$data = $cursor->fetch();
203
		$cursor->closeCursor();
204
205
		if ($data === false) {
206
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
207
		}
208
209
		return $this->parseLinksSelectSql($data);
210
	}
211
212
213
	/**
214
	 * return the FederatedLink identified by a its Id
215
	 *
216
	 * @param string $linkUniqueId
217
	 *
218
	 * @return FederatedLink
219
	 * @throws FederatedLinkDoesNotExistException
220
	 */
221 View Code Duplication
	public function getLinkFromId($linkUniqueId) {
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...
222
		$qb = $this->getLinksSelectSql();
223
		$this->limitToShortenUniqueId($qb, $linkUniqueId, FederatedLink::SHORT_UNIQUE_ID_LENGTH);
224
225
		$cursor = $qb->execute();
226
		$data = $cursor->fetch();
227
		$cursor->closeCursor();
228
229
		if ($data === false) {
230
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
231
		}
232
233
		$entry = $this->parseLinksSelectSql($data);
234
235
		return $entry;
236
	}
237
238
239
}