Completed
Push — master ( 0a2d75...d5a533 )
by Maxence
03:06
created

FederatedLink::getUniqueId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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
	/** @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
	/** @var bool */
74
	private $fullJson = false;
75
76
	public function __construct() {
77
	}
78
79
80
	/**
81
	 * @param int $id
82
	 *
83
	 * @return FederatedLink
84
	 */
85
	public function setId($id) {
86
		$this->id = (int)$id;
87
88
		return $this;
89
	}
90
91
	/**
92
	 * @return int
93
	 */
94
	public function getId() {
95
		return $this->id;
96
	}
97
98
99
	/**
100
	 * @param $token
101
	 *
102
	 * @return $this
103
	 */
104
	public function setToken($token) {
105
		$this->token = (string)$token;
106
107
		return $this;
108
	}
109
110
	/**
111
	 * @param bool $full
112
	 *
113
	 * @return string
114
	 */
115
	public function getToken($full = false) {
116
		if ($full) {
117
			return $this->token;
118
		}
119
120
		return substr($this->token, 0, 12);
121
	}
122
123
124
	/**
125
	 * @return string
126
	 */
127
	public function generateToken() {
128
		$token = bin2hex(openssl_random_pseudo_bytes(24));
129
		$this->setToken($token);
130
131
		return $token;
132
	}
133
134
135
	/**
136
	 * @param string $address
137
	 *
138
	 * @return FederatedLink
139
	 */
140
	public function setAddress($address) {
141
		$this->address = (string)$address;
142
143
		return $this;
144
	}
145
146
	/**
147
	 * @return string
148
	 */
149
	public function getAddress() {
150
		return $this->address;
151
	}
152
153
154
	/**
155
	 * @param string $address
156
	 *
157
	 * @return FederatedLink
158
	 */
159
	public function setLocalAddress($address) {
160
		$this->localAddress = (string)$address;
161
162
		return $this;
163
	}
164
165
	/**
166
	 * @return string
167
	 */
168
	public function getLocalAddress() {
169
		return $this->localAddress;
170
	}
171
172
173
	/**
174
	 * @param int $circleId
175
	 *
176
	 * @return FederatedLink
177
	 */
178
	public function setCircleId($circleId) {
179
		$this->circleId = (int)$circleId;
180
181
		return $this;
182
	}
183
184
	/**
185
	 * @return int
186
	 */
187
	public function getCircleId() {
188
		return $this->circleId;
189
	}
190
191
192
	/**
193
	 * @param string $uniqueId
194
	 *
195
	 * @return FederatedLink
196
	 */
197
	public function setUniqueId($uniqueId) {
198
		$this->uniqueId = (string)$uniqueId;
199
200
		return $this;
201
	}
202
203
	/**
204
	 * @param bool $full
205
	 *
206
	 * @return string
207
	 */
208
	public function getUniqueId($full = false) {
209
		if ($full) {
210
			return $this->uniqueId;
211
		}
212
213
		return substr($this->uniqueId, 0, 12);
214
	}
215
216
217
	/**
218
	 * @param string $circleName
219
	 *
220
	 * @return FederatedLink
221
	 */
222
	public function setRemoteCircleName($circleName) {
223
		$this->remoteCircleName = (string)$circleName;
224
225
		return $this;
226
	}
227
228
	/**
229
	 * @return string
230
	 */
231
	public function getRemoteCircleName() {
232
		return $this->remoteCircleName;
233
	}
234
235
236
	/**
237
	 * @param string $circleName
238
	 *
239
	 * @return FederatedLink
240
	 */
241
	public function setCircleName($circleName) {
242
		$this->localCircleName = (string)$circleName;
243
244
		return $this;
245
	}
246
247
	/**
248
	 * @return string
249
	 */
250
	public function getCircleName() {
251
		return $this->localCircleName;
252
	}
253
254
255
	/**
256
	 * @param int $status
257
	 *
258
	 * @return FederatedLink
259
	 */
260
	public function setStatus($status) {
261
		$this->status = (int)$status;
262
263
		return $this;
264
	}
265
266
	/**
267
	 * @return int
268
	 */
269
	public function getStatus() {
270
		return $this->status;
271
	}
272
273
274
	/**
275
	 * @param int $creation
276
	 *
277
	 * @return FederatedLink
278
	 */
279
	public function setCreation($creation) {
280
		if ($creation === null) {
281
			return $this;
282
		}
283
284
		$this->creation = $creation;
285
286
		return $this;
287
	}
288
289
	/**
290
	 * @return int
291
	 */
292
	public function getCreation() {
293
		return $this->creation;
294
	}
295
296
297
	public function hasToBeValidStatusUpdate($status) {
298
		if ($this->getStatus() === self::STATUS_LINK_DOWN && $status === self::STATUS_LINK_REMOVE) {
299
			return true;
300
		}
301
302 View Code Duplication
		if ($this->getStatus() === self::STATUS_REQUEST_DECLINED
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...
303
			|| $this->getStatus() === self::STATUS_LINK_SETUP
304
		) {
305
			if ($status === self::STATUS_LINK_REMOVE) {
306
				return true;
307
			}
308
		}
309
310 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...
311
			if ($status === self::STATUS_LINK_REMOVE) {
312
				return true;
313
			}
314
			if ($status === self::STATUS_LINK_UP) {
315
				return true;
316
			}
317
		}
318
319 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...
320
			|| $this->getStatus() === self::STATUS_LINK_UP
321
		) {
322
			if ($status === self::STATUS_LINK_REMOVE) {
323
				return true;
324
			}
325
		}
326
327
		throw new FederatedCircleStatusUpdateException('The status could not be updated');
328
	}
329
330
331
	public function jsonSerialize() {
332
		return array(
333
			'id'        => $this->getId(),
334
			'token'     => $this->getToken($this->fullJson),
335
			'address'   => $this->getAddress(),
336
			'status'    => $this->getStatus(),
337
			'circle_id' => $this->getCircleId(),
338
			'unique_id' => $this->getUniqueId($this->fullJson),
339
			'creation'  => $this->getCreation()
340
		);
341
	}
342
343
344
	public function getJson($full = false) {
345
		$this->fullJson = $full;
346
		$json = json_encode($this);
347
		$this->fullJson = false;
348
349
		return $json;
350
	}
351
352
353
	public static function fromArray($arr) {
354
		if ($arr === null) {
355
			return null;
356
		}
357
358
		$link = new FederatedLink();
359
360
		$link->setId($arr['id']);
361
		$link->setToken($arr['token']);
362
		$link->setAddress($arr['address']);
363
		$link->setStatus($arr['status']);
364
		$link->setCircleId($arr['circle_id']);
365
		$link->setUniqueId($arr['unique_id']);
366
		$link->setCreation($arr['creation']);
367
368
		return $link;
369
	}
370
371
372
	public static function fromJSON($json) {
373
		return self::fromArray(json_decode($json, true));
374
	}
375
376
}