Completed
Pull Request — master (#551)
by Maxence
02:47
created

RemoteInstance::setEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model\Federated;
33
34
35
use daita\MySmallPhpTools\Db\Nextcloud\nc21\INC21QueryRow;
36
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Signatory;
37
use daita\MySmallPhpTools\Traits\TArrayTools;
38
use JsonSerializable;
39
use OCA\Circles\Exceptions\RemoteUidException;
40
41
42
/**
43
 * Class AppService
44
 *
45
 * @package OCA\Circles\Model
46
 */
47
class RemoteInstance extends NC21Signatory implements INC21QueryRow, JsonSerializable {
48
49
50
	use TArrayTools;
51
52
	const TYPE_UNKNOWN = 'Unknown';    // not trusted
53
	const TYPE_PASSIVE = 'Passive';    // Minimum information about Federated Circles are broadcasted if a member belongs to the circle.
54
	const TYPE_EXTERNAL = 'External';  // info about Federated Circles and their members are broadcasted  if a member belongs to the circle.
55
	const TYPE_TRUSTED = 'Trusted';    // everything about Federated Circles are broadcasted.
56
	const TYPE_GLOBAL_SCALE = 'GlobalScale';  // every Circle is broadcasted,
57
58
	const TEST = 'test';
59
	const INCOMING = 'incoming';
60
	const EVENT = 'event';
61
	const CIRCLES = 'circles';
62
	const CIRCLE = 'circle';
63
	const MEMBERS = 'members';
64
	const MEMBER = 'member';
65
66
67
	/** @var int */
68
	private $dbId = 0;
69
70
	/** @var string */
71
	private $type = self::TYPE_UNKNOWN;
72
73
	/** @var string */
74
	private $test = '';
75
76
	/** @var string */
77
	private $incoming = '';
78
79
	/** @var string */
80
	private $event = '';
81
82
	/** @var string */
83
	private $circles = '';
84
85
	/** @var string */
86
	private $circle = '';
87
88
	/** @var string */
89
	private $members = '';
90
91
	/** @var string */
92
	private $member = '';
93
94
	/** @var string */
95
	private $uid = '';
96
97
	/** @var string */
98
	private $authSigned = '';
99
100
	/** @var bool */
101
	private $identityAuthed = false;
102
103
104
	/**
105
	 * @param int $dbId
106
	 *
107
	 * @return self
108
	 */
109
	public function setDbId(int $dbId): self {
110
		$this->dbId = $dbId;
111
112
		return $this;
113
	}
114
115
	/**
116
	 * @return int
117
	 */
118
	public function getDbId(): int {
119
		return $this->dbId;
120
	}
121
122
123
	/**
124
	 * @param string $type
125
	 *
126
	 * @return $this
127
	 */
128
	public function setType(string $type): self {
129
		$this->type = $type;
130
131
		return $this;
132
	}
133
134
	/**
135
	 * @return string
136
	 */
137
	public function getType(): string {
138
		return $this->type;
139
	}
140
141
142
	/**
143
	 * @return string
144
	 */
145
	public function getIncoming(): string {
146
		return $this->incoming;
147
	}
148
149
	/**
150
	 * @param string $incoming
151
	 *
152
	 * @return self
153
	 */
154
	public function setIncoming(string $incoming): self {
155
		$this->incoming = $incoming;
156
157
		return $this;
158
	}
159
160
161
	/**
162
	 * @return string
163
	 */
164
	public function getEvent(): string {
165
		return $this->event;
166
	}
167
168
	/**
169
	 * @param string $event
170
	 *
171
	 * @return self
172
	 */
173
	public function setEvent(string $event): self {
174
		$this->event = $event;
175
176
		return $this;
177
	}
178
179
180
	/**
181
	 * @param string $test
182
	 *
183
	 * @return RemoteInstance
184
	 */
185
	public function setTest(string $test): self {
186
		$this->test = $test;
187
188
		return $this;
189
	}
190
191
	/**
192
	 * @return string
193
	 */
194
	public function getTest(): string {
195
		return $this->test;
196
	}
197
198
199
	/**
200
	 * @return string
201
	 */
202
	public function getCircles(): string {
203
		return $this->circles;
204
	}
205
206
	/**
207
	 * @param string $circles
208
	 *
209
	 * @return self
210
	 */
211
	public function setCircles(string $circles): self {
212
		$this->circles = $circles;
213
214
		return $this;
215
	}
216
217
218
	/**
219
	 * @return string
220
	 */
221
	public function getCircle(): string {
222
		return $this->circle;
223
	}
224
225
	/**
226
	 * @param string $circle
227
	 *
228
	 * @return self
229
	 */
230
	public function setCircle(string $circle): self {
231
		$this->circle = $circle;
232
233
		return $this;
234
	}
235
236
237
	/**
238
	 * @return string
239
	 */
240
	public function getMembers(): string {
241
		return $this->members;
242
	}
243
244
	/**
245
	 * @param string $members
246
	 *
247
	 * @return self
248
	 */
249
	public function setMembers(string $members): self {
250
		$this->members = $members;
251
252
		return $this;
253
	}
254
255
256
	/**
257
	 * @return string
258
	 */
259
	public function getMember(): string {
260
		return $this->member;
261
	}
262
263
	/**
264
	 * @param string $member
265
	 *
266
	 * @return self
267
	 */
268
	public function setMember(string $member): self {
269
		$this->member = $member;
270
271
		return $this;
272
	}
273
274
275
	/**
276
	 * @return $this
277
	 */
278
	public function setUidFromKey(): self {
279
		$this->setUid(hash('sha512', $this->getPublicKey()));
280
281
		return $this;
282
	}
283
284
	/**
285
	 * @param string $uid
286
	 *
287
	 * @return RemoteInstance
288
	 */
289
	public function setUid(string $uid): self {
290
		$this->uid = $uid;
291
292
		return $this;
293
	}
294
295
	/**
296
	 * @param bool $shorten
297
	 *
298
	 * @return string
299
	 */
300
	public function getUid(bool $shorten = false): string {
301
		if ($shorten) {
302
			return substr($this->uid, 0, 18);
303
		}
304
305
		return $this->uid;
306
	}
307
308
309
	/**
310
	 * @param string $authSigned
311
	 *
312
	 * @return RemoteInstance
313
	 */
314
	public function setAuthSigned(string $authSigned): self {
315
		$this->authSigned = $authSigned;
316
317
		return $this;
318
	}
319
320
	/**
321
	 * @return string
322
	 */
323
	public function getAuthSigned(): string {
324
		return $this->authSigned;
325
	}
326
327
328
	/**
329
	 * @param bool $identityAuthed
330
	 *
331
	 * @return RemoteInstance
332
	 */
333
	public function setIdentityAuthed(bool $identityAuthed): self {
334
		$this->identityAuthed = $identityAuthed;
335
336
		return $this;
337
	}
338
339
	/**
340
	 * @return bool
341
	 */
342
	public function isIdentityAuthed(): bool {
343
		return $this->identityAuthed;
344
	}
345
346
	/**
347
	 * @throws RemoteUidException
348
	 */
349
	public function mustBeIdentityAuthed(): void {
350
		if (!$this->isIdentityAuthed()) {
351
			throw new RemoteUidException('identity not authed');
352
		}
353
	}
354
355
356
	/**
357
	 * @param array $data
358
	 *
359
	 * @return NC21Signatory
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use RemoteInstance.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
360
	 */
361
	public function import(array $data): NC21Signatory {
362
		parent::import($data);
363
364
		$this->setTest($this->get('test', $data))
365
			 ->setEvent($this->get('event', $data))
366
			 ->setIncoming($this->get('incoming', $data))
367
			 ->setCircles($this->get('circles', $data))
368
			 ->setCircle($this->get('circle', $data))
369
			 ->setMembers($this->get('members', $data))
370
			 ->setMember($this->get('member', $data))
371
			 ->setUid($this->get('uid', $data));
372
373
		$algo = '';
374
		$authSigned = trim($this->get('auth-signed', $data), ':');
375
		if (strpos($authSigned, ':') > 0) {
376
			list($algo, $authSigned) = explode(':', $authSigned);
377
		}
378
379
		$this->setAuthSigned($authSigned)
380
			 ->setAlgorithm($algo);
381
382
		return $this;
383
	}
384
385
386
	/**
387
	 * @return array
388
	 */
389
	public function jsonSerialize(): array {
390
		$data = [
391
			'uid'      => $this->getUid(true),
392
			'event'    => $this->getEvent(),
393
			'incoming' => $this->getIncoming(),
394
			'test'     => $this->getTest(),
395
			'circles'  => $this->getCircles(),
396
			'circle'   => $this->getCircle(),
397
			'members'  => $this->getMembers(),
398
			'member'   => $this->getMember(),
399
		];
400
401
		if ($this->getAuthSigned() !== '') {
402
			$data['auth-signed'] = $this->getAlgorithm() . ':' . $this->getAuthSigned();
403
		}
404
405
		return array_filter(array_merge($data, parent::jsonSerialize()));
406
	}
407
408
409
	/**
410
	 * @param array $data
411
	 *
412
	 * @return self
413
	 */
414
	public function importFromDatabase(array $data): INC21QueryRow {
415
		$this->setDbId($this->getInt('id', $data));
416
		$this->import($this->getArray('item', $data));
417
		$this->setOrigData($this->getArray('item', $data));
418
		$this->setType($this->get('type', $data));
419
		$this->setInstance($this->get('instance', $data));
420
		$this->setId($this->get('href', $data));
421
422
		return $this;
423
	}
424
425
}
426
427