Completed
Push — master ( c4f011...0a2d75 )
by Maxence
03:16
created

FederatedLink   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 311
Duplicated Lines 4.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 35
c 0
b 0
f 0
lcom 1
cbo 1
dl 15
loc 311
rs 9

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A setId() 0 5 1
A getId() 0 3 1
A setToken() 0 5 1
A getToken() 0 3 1
A shortenToken() 0 3 1
A shortenUniqueId() 0 3 1
A generateToken() 0 6 1
A setAddress() 0 5 1
A getAddress() 0 3 1
A setLocalAddress() 0 5 1
A getLocalAddress() 0 3 1
A setCircleId() 0 5 1
A getCircleId() 0 3 1
A setUniqueId() 0 5 1
A getUniqueId() 0 3 1
A setRemoteCircleName() 0 5 1
A getRemoteCircleName() 0 3 1
A setCircleName() 0 5 1
A getCircleName() 0 3 1
A setStatus() 0 5 1
A getStatus() 0 3 1
A setCreation() 0 9 2
A getCreation() 0 3 1
C hasToBeValidStatusUpdate() 15 24 9
A jsonSerialize() 0 10 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
 * 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
namespace OCA\Circles\Model;
28
29
use OCA\Circles\Exceptions\FederatedCircleStatusUpdateException;
30
31
class FederatedLink implements \JsonSerializable {
32
33
34
	const STATUS_ERROR = -1;
35
	const STATUS_LINK_REMOVE = 0;
36
	const STATUS_LINK_DOWN = 1;
37
	const STATUS_LINK_SETUP = 2;
38
	const STATUS_REQUEST_DECLINED = 4;
39
	const STATUS_REQUEST_SENT = 5;
40
	const STATUS_LINK_REQUESTED = 6;
41
	const STATUS_LINK_UP = 9;
42
43
	/** @var int */
44
	private $id;
45
46
	/** @var string */
47
	private $token;
48
49
	/** @var string */
50
	private $address;
51
52
	/** @var string */
53
	private $localAddress;
54
55
	/** @var int */
56
	private $status;
57
58
	/** @var int */
59
	private $creation;
60
61
	/** @var int */
62
	private $circleId;
63
64
	/** @var string */
65
	private $uniqueId = '';
66
67
	/** @var string */
68
	private $remoteCircleName;
69
70
	/** @var string */
71
	private $localCircleName;
72
73
74
	public function __construct() {
75
	}
76
77
78
	/**
79
	 * @param int $id
80
	 *
81
	 * @return FederatedLink
82
	 */
83
	public function setId($id) {
84
		$this->id = (int)$id;
85
86
		return $this;
87
	}
88
89
	/**
90
	 * @return int
91
	 */
92
	public function getId() {
93
		return $this->id;
94
	}
95
96
97
	/**
98
	 * @param $token
99
	 *
100
	 * @return $this
101
	 */
102
	public function setToken($token) {
103
		$this->token = (string)$token;
104
105
		return $this;
106
	}
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getToken() {
112
		return $this->token;
113
	}
114
115
116
	public function shortenToken() {
117
		$this->setToken(substr($this->getToken(), 0, 8));
118
	}
119
120
121
	public function shortenUniqueId() {
122
		$this->setUniqueId(substr($this->getUniqueId(), 0, 8));
123
	}
124
125
126
	/**
127
	 * @return string
128
	 */
129
	public function generateToken() {
130
		$token = bin2hex(openssl_random_pseudo_bytes(24));
131
		$this->setToken($token);
132
133
		return $token;
134
	}
135
136
137
	/**
138
	 * @param string $address
139
	 *
140
	 * @return FederatedLink
141
	 */
142
	public function setAddress($address) {
143
		$this->address = (string)$address;
144
145
		return $this;
146
	}
147
148
	/**
149
	 * @return string
150
	 */
151
	public function getAddress() {
152
		return $this->address;
153
	}
154
155
156
	/**
157
	 * @param string $address
158
	 *
159
	 * @return FederatedLink
160
	 */
161
	public function setLocalAddress($address) {
162
		$this->localAddress = (string)$address;
163
164
		return $this;
165
	}
166
167
	/**
168
	 * @return string
169
	 */
170
	public function getLocalAddress() {
171
		return $this->localAddress;
172
	}
173
174
175
	/**
176
	 * @param int $circleId
177
	 *
178
	 * @return FederatedLink
179
	 */
180
	public function setCircleId($circleId) {
181
		$this->circleId = (int)$circleId;
182
183
		return $this;
184
	}
185
186
	/**
187
	 * @return int
188
	 */
189
	public function getCircleId() {
190
		return $this->circleId;
191
	}
192
193
194
	/**
195
	 * @param string $uniqueId
196
	 *
197
	 * @return FederatedLink
198
	 */
199
	public function setUniqueId($uniqueId) {
200
		$this->uniqueId = (string)$uniqueId;
201
202
		return $this;
203
	}
204
205
	/**
206
	 * @return string
207
	 */
208
	public function getUniqueId() {
209
		return $this->uniqueId;
210
	}
211
212
213
	/**
214
	 * @param string $circleName
215
	 *
216
	 * @return FederatedLink
217
	 */
218
	public function setRemoteCircleName($circleName) {
219
		$this->remoteCircleName = (string)$circleName;
220
221
		return $this;
222
	}
223
224
	/**
225
	 * @return string
226
	 */
227
	public function getRemoteCircleName() {
228
		return $this->remoteCircleName;
229
	}
230
231
232
	/**
233
	 * @param string $circleName
234
	 *
235
	 * @return FederatedLink
236
	 */
237
	public function setCircleName($circleName) {
238
		$this->localCircleName = (string)$circleName;
239
240
		return $this;
241
	}
242
243
	/**
244
	 * @return string
245
	 */
246
	public function getCircleName() {
247
		return $this->localCircleName;
248
	}
249
250
251
	/**
252
	 * @param int $status
253
	 *
254
	 * @return FederatedLink
255
	 */
256
	public function setStatus($status) {
257
		$this->status = (int)$status;
258
259
		return $this;
260
	}
261
262
	/**
263
	 * @return int
264
	 */
265
	public function getStatus() {
266
		return $this->status;
267
	}
268
269
270
	/**
271
	 * @param int $creation
272
	 *
273
	 * @return FederatedLink
274
	 */
275
	public function setCreation($creation) {
276
		if ($creation === null) {
277
			return $this;
278
		}
279
280
		$this->creation = $creation;
281
282
		return $this;
283
	}
284
285
	/**
286
	 * @return int
287
	 */
288
	public function getCreation() {
289
		return $this->creation;
290
	}
291
292
293
	public function hasToBeValidStatusUpdate($status) {
294
		if ($this->getStatus() === self::STATUS_LINK_DOWN && $status === self::STATUS_LINK_REMOVE) {
295
			return true;
296
		}
297
		
298 View Code Duplication
		if ($this->getStatus() === self::STATUS_LINK_REQUESTED) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
299
			if ($status === self::STATUS_LINK_REMOVE) {
300
				return true;
301
			}
302
			if ($status === self::STATUS_LINK_UP) {
303
				return true;
304
			}
305
		}
306
307 View Code Duplication
		if ($this->getStatus() === self::STATUS_REQUEST_SENT
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
308
			|| $this->getStatus() === self::STATUS_LINK_UP
309
		) {
310
			if ($status === self::STATUS_LINK_REMOVE) {
311
				return true;
312
			}
313
		}
314
315
		throw new FederatedCircleStatusUpdateException('The status could not be updated');
316
	}
317
318
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% 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...
319
//	public function isValid() {
320
//
321
//		if ($this->getStatus() === FederatedLink::STATUS_REQUEST_SENT
322
//			|| $this->getStatus() === FederatedLink::STATUS_LINK_UP
323
//		) {
324
//			return true;
325
//		}
326
//
327
//		return false;
328
//	}
329
330
	public function jsonSerialize() {
331
		return array(
332
			'id'        => $this->getId(),
333
			'token'     => $this->getToken(),
334
			'address'   => $this->getAddress(),
335
			'status'    => $this->getStatus(),
336
			'unique_id' => $this->getUniqueId(),
337
			'creation'  => $this->getCreation()
338
		);
339
	}
340
341
}