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

RemoteInstance::getMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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_EXTERNAL = 'External';  // info about Federated Circles are not broadcasted by default.
54
	const TYPE_TRUSTED = 'Trusted';    // evething about Federated Circles are broadcasted.
55
	const TYPE_GLOBAL_SCALE = 'GlobalScale';  // every Circle is broadcasted,
56
57
	const TEST = 'test';
58
	const INCOMING = 'incoming';
59
	const EVENT = 'event';
60
	const CIRCLES = 'circles';
61
	const CIRCLE = 'circle';
62
	const MEMBERS = 'members';
63
	const MEMBER = 'member';
64
65
66
	/** @var int */
67
	private $dbId = 0;
68
69
	/** @var string */
70
	private $type = self::TYPE_UNKNOWN;
71
72
	/** @var string */
73
	private $test = '';
74
75
	/** @var string */
76
	private $incoming = '';
77
78
	/** @var string */
79
	private $event = '';
80
81
	/** @var string */
82
	private $circles = '';
83
84
	/** @var string */
85
	private $circle = '';
86
87
	/** @var string */
88
	private $members = '';
89
90
	/** @var string */
91
	private $member = '';
92
93
	/** @var string */
94
	private $uid = '';
95
96
	/** @var string */
97
	private $authSigned = '';
98
99
	/** @var bool */
100
	private $identityAuthed = false;
101
102
103
	/**
104
	 * @param int $dbId
105
	 *
106
	 * @return self
107
	 */
108
	public function setDbId(int $dbId): self {
109
		$this->dbId = $dbId;
110
111
		return $this;
112
	}
113
114
	/**
115
	 * @return int
116
	 */
117
	public function getDbId(): int {
118
		return $this->dbId;
119
	}
120
121
122
	/**
123
	 * @param string $type
124
	 *
125
	 * @return $this
126
	 */
127
	public function setType(string $type): self {
128
		$this->type = $type;
129
130
		return $this;
131
	}
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getType(): string {
137
		return $this->type;
138
	}
139
140
141
	/**
142
	 * @return string
143
	 */
144
	public function getIncoming(): string {
145
		return $this->incoming;
146
	}
147
148
	/**
149
	 * @param string $incoming
150
	 *
151
	 * @return self
152
	 */
153
	public function setIncoming(string $incoming): self {
154
		$this->incoming = $incoming;
155
156
		return $this;
157
	}
158
159
160
	/**
161
	 * @return string
162
	 */
163
	public function getEvent(): string {
164
		return $this->event;
165
	}
166
167
	/**
168
	 * @param string $event
169
	 *
170
	 * @return self
171
	 */
172
	public function setEvent(string $event): self {
173
		$this->event = $event;
174
175
		return $this;
176
	}
177
178
179
	/**
180
	 * @param string $test
181
	 *
182
	 * @return RemoteInstance
183
	 */
184
	public function setTest(string $test): self {
185
		$this->test = $test;
186
187
		return $this;
188
	}
189
190
	/**
191
	 * @return string
192
	 */
193
	public function getTest(): string {
194
		return $this->test;
195
	}
196
197
198
	/**
199
	 * @return string
200
	 */
201
	public function getCircles(): string {
202
		return $this->circles;
203
	}
204
205
	/**
206
	 * @param string $circles
207
	 *
208
	 * @return self
209
	 */
210
	public function setCircles(string $circles): self {
211
		$this->circles = $circles;
212
213
		return $this;
214
	}
215
216
217
	/**
218
	 * @return string
219
	 */
220
	public function getCircle(): string {
221
		return $this->circle;
222
	}
223
224
	/**
225
	 * @param string $circle
226
	 *
227
	 * @return self
228
	 */
229
	public function setCircle(string $circle): self {
230
		$this->circle = $circle;
231
232
		return $this;
233
	}
234
235
236
	/**
237
	 * @return string
238
	 */
239
	public function getMembers(): string {
240
		return $this->members;
241
	}
242
243
	/**
244
	 * @param string $members
245
	 *
246
	 * @return self
247
	 */
248
	public function setMembers(string $members): self {
249
		$this->members = $members;
250
251
		return $this;
252
	}
253
254
255
	/**
256
	 * @return string
257
	 */
258
	public function getMember(): string {
259
		return $this->member;
260
	}
261
262
	/**
263
	 * @param string $member
264
	 *
265
	 * @return self
266
	 */
267
	public function setMember(string $member): self {
268
		$this->member = $member;
269
270
		return $this;
271
	}
272
273
274
	/**
275
	 * @return $this
276
	 */
277
	public function setUidFromKey(): self {
278
		$this->setUid(hash('sha512', $this->getPublicKey()));
279
280
		return $this;
281
	}
282
283
	/**
284
	 * @param string $uid
285
	 *
286
	 * @return RemoteInstance
287
	 */
288
	public function setUid(string $uid): self {
289
		$this->uid = $uid;
290
291
		return $this;
292
	}
293
294
	/**
295
	 * @param bool $shorten
296
	 *
297
	 * @return string
298
	 */
299
	public function getUid(bool $shorten = false): string {
300
		if ($shorten) {
301
			return substr($this->uid, 0, 18);
302
		}
303
304
		return $this->uid;
305
	}
306
307
308
	/**
309
	 * @param string $authSigned
310
	 *
311
	 * @return RemoteInstance
312
	 */
313
	public function setAuthSigned(string $authSigned): self {
314
		$this->authSigned = $authSigned;
315
316
		return $this;
317
	}
318
319
	/**
320
	 * @return string
321
	 */
322
	public function getAuthSigned(): string {
323
		return $this->authSigned;
324
	}
325
326
327
	/**
328
	 * @param bool $identityAuthed
329
	 *
330
	 * @return RemoteInstance
331
	 */
332
	public function setIdentityAuthed(bool $identityAuthed): self {
333
		$this->identityAuthed = $identityAuthed;
334
335
		return $this;
336
	}
337
338
	/**
339
	 * @return bool
340
	 */
341
	public function isIdentityAuthed(): bool {
342
		return $this->identityAuthed;
343
	}
344
345
	/**
346
	 * @throws RemoteUidException
347
	 */
348
	public function mustBeIdentityAuthed(): void {
349
		if (!$this->isIdentityAuthed()) {
350
			throw new RemoteUidException('identity not authed');
351
		}
352
	}
353
354
355
	/**
356
	 * @param array $data
357
	 *
358
	 * @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...
359
	 */
360
	public function import(array $data): NC21Signatory {
361
		parent::import($data);
362
363
		$this->setTest($this->get('test', $data));
364
		$this->setEvent($this->get('event', $data));
365
		$this->setIncoming($this->get('incoming', $data));
366
		$this->setCircles($this->get('circles', $data));
367
		$this->setCircle($this->get('circle', $data));
368
		$this->setMembers($this->get('members', $data));
369
		$this->setMember($this->get('member', $data));
370
		$this->setAuthSigned($this->get('auth-signed', $data));
371
		$this->setUid($this->get('uid', $data));
372
373
		return $this;
374
	}
375
376
377
	/**
378
	 * @return array
379
	 */
380
	public function jsonSerialize(): array {
381
		$data = [
382
			'uid'      => $this->getUid(true),
383
			'event'    => $this->getEvent(),
384
			'incoming' => $this->getIncoming(),
385
			'test'     => $this->getTest(),
386
			'circles'  => $this->getCircles(),
387
			'circle'   => $this->getCircle(),
388
			'members'  => $this->getMembers(),
389
			'member'   => $this->getMember(),
390
		];
391
392
		if ($this->getAuthSigned() !== '') {
393
			$data['auth-signed'] = $this->getAlgorithm() . ':' . $this->getAuthSigned();
394
		}
395
396
		return array_filter(array_merge($data, parent::jsonSerialize()));
397
	}
398
399
400
	/**
401
	 * @param array $data
402
	 *
403
	 * @return self
404
	 */
405
	public function importFromDatabase(array $data): INC21QueryRow {
406
		$this->setDbId($this->getInt('id', $data));
407
		$this->import($this->getArray('item', $data));
408
		$this->setOrigData($this->getArray('item', $data));
409
		$this->setType($this->get('type', $data));
410
		$this->setInstance($this->get('instance', $data));
411
		$this->setId($this->get('href', $data));
412
413
		return $this;
414
	}
415
416
}
417
418