Completed
Push — master ( ab41ba...607864 )
by Maxence
02:17
created

FederatedLink::setAddress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
	const SHORT_UNIQUE_ID_LENGTH = 12;
44
45
	/** @var int */
46
	private $id;
47
48
	/** @var string */
49
	private $token;
50
51
	/** @var string */
52
	private $address;
53
54
	/** @var string */
55
	private $localAddress;
56
57
	/** @var int */
58
	private $status;
59
60
	/** @var int */
61
	private $creation;
62
63
	/** @var int */
64
	private $circleUniqueId;
65
66
	/** @var string */
67
	private $uniqueId = '';
68
69
	/** @var string */
70
	private $remoteCircleName;
71
72
	/** @var string */
73
	private $localCircleName;
74
75
	/** @var bool */
76
	private $fullJson = false;
77
78
	public function __construct() {
79
	}
80
81
82
	/**
83
	 * @param int $id
84
	 *
85
	 * @return FederatedLink
86
	 */
87
	public function setId($id) {
88
		$this->id = (int)$id;
89
90
		return $this;
91
	}
92
93
	/**
94
	 * @return int
95
	 */
96
	public function getId() {
97
		return $this->id;
98
	}
99
100
101
	/**
102
	 * @param $token
103
	 *
104
	 * @return $this
105
	 */
106
	public function setToken($token) {
107
		$this->token = (string)$token;
108
109
		return $this;
110
	}
111
112
	/**
113
	 * @param bool $full
114
	 *
115
	 * @return string
116
	 */
117
	public function getToken($full = false) {
118
		if ($full) {
119
			return $this->token;
120
		}
121
122
		return substr($this->token, 0, FederatedLink::SHORT_UNIQUE_ID_LENGTH);
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 string $circleUniqueId
177
	 *
178
	 * @return FederatedLink
179
	 */
180
	public function setCircleId($circleUniqueId) {
181
		$this->circleUniqueId = $circleUniqueId;
0 ignored issues
show
Documentation Bug introduced by
The property $circleUniqueId was declared of type integer, but $circleUniqueId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
182
183
		return $this;
184
	}
185
186
	/**
187
	 * @param bool $full
188
	 *
189
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

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...
190
	 */
191
	public function getCircleId($full = false) {
192
		if ($full) {
193
			return $this->circleUniqueId;
194
		}
195
196
		return substr($this->circleUniqueId, 0, Circle::SHORT_UNIQUE_ID_LENGTH);
197
198
	}
199
200
201
	/**
202
	 * @param string $uniqueId
203
	 *
204
	 * @return FederatedLink
205
	 */
206
	public function setUniqueId($uniqueId) {
207
		$this->uniqueId = (string)$uniqueId;
208
209
		return $this;
210
	}
211
212
	/**
213
	 * @param bool $full
214
	 *
215
	 * @return string
216
	 */
217
	public function getUniqueId($full = false) {
218
		if ($full) {
219
			return $this->uniqueId;
220
		}
221
222
		return substr($this->uniqueId, 0, FederatedLink::SHORT_UNIQUE_ID_LENGTH);
223
	}
224
225
226
	/**
227
	 * @param string $circleName
228
	 *
229
	 * @return FederatedLink
230
	 */
231
	public function setRemoteCircleName($circleName) {
232
		$this->remoteCircleName = (string)$circleName;
233
234
		return $this;
235
	}
236
237
	/**
238
	 * @return string
239
	 */
240
	public function getRemoteCircleName() {
241
		return $this->remoteCircleName;
242
	}
243
244
245
	/**
246
	 * @param string $circleName
247
	 *
248
	 * @return FederatedLink
249
	 */
250
	public function setCircleName($circleName) {
251
		$this->localCircleName = (string)$circleName;
252
253
		return $this;
254
	}
255
256
	/**
257
	 * @return string
258
	 */
259
	public function getCircleName() {
260
		return $this->localCircleName;
261
	}
262
263
264
	/**
265
	 * @param int $status
266
	 *
267
	 * @return FederatedLink
268
	 */
269
	public function setStatus($status) {
270
		$this->status = (int)$status;
271
272
		return $this;
273
	}
274
275
	/**
276
	 * @return int
277
	 */
278
	public function getStatus() {
279
		return $this->status;
280
	}
281
282
283
	/**
284
	 * @param int $creation
285
	 *
286
	 * @return FederatedLink
287
	 */
288
	public function setCreation($creation) {
289
		if ($creation === null) {
290
			return $this;
291
		}
292
293
		$this->creation = $creation;
294
295
		return $this;
296
	}
297
298
	/**
299
	 * @return int
300
	 */
301
	public function getCreation() {
302
		return $this->creation;
303
	}
304
305
306
	public function hasToBeValidStatusUpdate($status) {
307
		try {
308
			$this->hasToBeValidStatusUpdateWhileLinkDown($status);
309
			$this->hasToBeValidStatusUpdateWhileRequestDeclined($status);
310
			$this->hasToBeValidStatusUpdateWhileLinkRequested($status);
311
			$this->hasToBeValidStatusUpdateWhileRequestSent($status);
312
313
		} catch (FederatedCircleStatusUpdateException $e) {
314
			throw new FederatedCircleStatusUpdateException('The status could not be updated');
315
		}
316
	}
317
318
319
	/**
320
	 * @param $status
321
	 *
322
	 * @throws FederatedCircleStatusUpdateException
323
	 */
324
	private function hasToBeValidStatusUpdateWhileLinkDown($status) {
325
326
		if ($this->getStatus() === self::STATUS_LINK_DOWN) {
327
			return;
328
		}
329
330
		if ($status !== self::STATUS_LINK_REMOVE) {
331
			throw new FederatedCircleStatusUpdateException();
332
		}
333
	}
334
335
336
	/**
337
	 * @param $status
338
	 *
339
	 * @throws FederatedCircleStatusUpdateException
340
	 */
341 View Code Duplication
	private function hasToBeValidStatusUpdateWhileRequestDeclined($status) {
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...
342
		if ($this->getStatus() !== self::STATUS_REQUEST_DECLINED
343
			&& $this->getStatus() !== self::STATUS_LINK_SETUP) {
344
			return;
345
		}
346
347
		if ($status !== self::STATUS_LINK_REMOVE) {
348
			throw new FederatedCircleStatusUpdateException();
349
		}
350
	}
351
352
353
	/**
354
	 * @param $status
355
	 *
356
	 * @throws FederatedCircleStatusUpdateException
357
	 */
358 View Code Duplication
	private function hasToBeValidStatusUpdateWhileLinkRequested($status) {
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...
359
		if ($this->getStatus() !== self::STATUS_LINK_REQUESTED) {
360
			return;
361
		}
362
363
		if ($status !== self::STATUS_LINK_REMOVE && $status !== self::STATUS_LINK_UP) {
364
			throw new FederatedCircleStatusUpdateException();
365
		}
366
	}
367
368
369
	/**
370
	 * @param $status
371
	 *
372
	 * @throws FederatedCircleStatusUpdateException
373
	 */
374 View Code Duplication
	private function hasToBeValidStatusUpdateWhileRequestSent($status) {
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...
375
		if ($this->getStatus() !== self::STATUS_REQUEST_SENT
376
			&& $this->getStatus() !== self::STATUS_LINK_UP
377
		) {
378
			return;
379
		}
380
381
		if ($status !== self::STATUS_LINK_REMOVE) {
382
			throw new FederatedCircleStatusUpdateException();
383
		}
384
	}
385
386
387
	public function jsonSerialize() {
388
		return array(
389
			'id'        => $this->getId(),
390
			'token'     => $this->getToken($this->fullJson),
391
			'address'   => $this->getAddress(),
392
			'status'    => $this->getStatus(),
393
			'circle_id' => $this->getCircleId(),
394
			'unique_id' => $this->getUniqueId($this->fullJson),
395
			'creation'  => $this->getCreation()
396
		);
397
	}
398
399
400
	public function getJson($full = false) {
401
		$this->fullJson = $full;
402
		$json = json_encode($this);
403
		$this->fullJson = false;
404
405
		return $json;
406
	}
407
408
409
	public static function fromArray($arr) {
410
		if ($arr === null) {
411
			return null;
412
		}
413
414
		$link = new FederatedLink();
415
416
		$link->setId($arr['id']);
417
		$link->setToken($arr['token']);
418
		$link->setAddress($arr['address']);
419
		$link->setStatus($arr['status']);
420
		$link->setCircleId($arr['circle_id']);
421
		$link->setUniqueId($arr['unique_id']);
422
		$link->setCreation($arr['creation']);
423
424
		return $link;
425
	}
426
427
428
	public static function fromJSON($json) {
429
		return self::fromArray(json_decode($json, true));
430
	}
431
432
}