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