Completed
Push — master ( 90f02e...53fcb4 )
by Maxence
02:59
created

FederatedLink::setRemoteCircleName()   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
	/** @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_REQUESTED) {
295
			if ($status === self::STATUS_LINK_REMOVE) {
296
				return true;
297
			}
298
			if ($status === self::STATUS_LINK_UP) {
299
				return true;
300
			}
301
		}
302
303
		throw new FederatedCircleStatusUpdateException('The status could not be updated');
304
	}
305
306
307
	public function isValid() {
308
309
		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...
310
			|| $this->getStatus() === FederatedLink::STATUS_LINK_UP
311
		) {
312
			return true;
313
		}
314
315
		return false;
316
	}
317
318
	public function jsonSerialize() {
319
		return array(
320
			'id'        => $this->getId(),
321
			'token'     => $this->getToken(),
322
			'address'   => $this->getAddress(),
323
			'status'    => $this->getStatus(),
324
			'unique_id' => $this->getUniqueId(),
325
			'creation'  => $this->getCreation()
326
		);
327
	}
328
329
}