Completed
Push — master ( 8d67fa...90f02e )
by Maxence
03:18
created

FederatedLink::setCreation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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