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

FederatedEvent::getIncomingOrigin()   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\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Model\SimpleDataStore;
37
use daita\MySmallPhpTools\Traits\TArrayTools;
38
use JsonSerializable;
39
use OCA\Circles\Model\Circle;
40
use OCA\Circles\Model\Member;
41
42
43
/**
44
 * Class FederatedEvent
45
 *
46
 * @package OCA\Circles\Model\Federated
47
 */
48
class FederatedEvent implements JsonSerializable {
49
50
51
	const SEVERITY_LOW = 1;
52
	const SEVERITY_HIGH = 3;
53
54
	const BYPASS_LOCALCIRCLECHECK = 1;
55
	const BYPASS_LOCALMEMBERCHECK = 2;
56
	const BYPASS_INITIATORCHECK = 4;
57
	const BYPASS_INITIATORMEMBERSHIP = 8;
58
59
	use TArrayTools;
60
61
62
	/** @var string */
63
	private $class;
64
65
	/** @var string */
66
	private $source = '';
67
68
	/** @var Circle */
69
	private $circle;
70
71
	/** @var string */
72
	private $itemId = '';
73
74
	/** @var Member */
75
	private $member;
76
77
	/** @var SimpleDataStore */
78
	private $data;
79
80
	/** @var int */
81
	private $severity = self::SEVERITY_LOW;
82
83
	/** @var SimpleDataStore */
84
	private $readingOutcome;
85
86
	/** @var SimpleDataStore */
87
	private $dataOutcome;
88
89
	/** @var SimpleDataStore */
90
	private $result;
91
92
	/** @var bool */
93
	private $async = false;
94
95
	/** @var bool */
96
	private $limitedToInstanceWithMember = false;
97
98
	/** @var bool */
99
	private $dataRequestOnly = false;
100
101
	/** @var string */
102
	private $incomingOrigin = '';
103
104
105
	/** @var string */
106
	private $wrapperToken = '';
107
108
	/** @var bool */
109
	private $verifiedViewer = false;
110
111
	/** @var bool */
112
	private $verifiedCircle = false;
113
114
	/** @var int */
115
	private $bypass = 0;
116
117
118
	/**
119
	 * FederatedEvent constructor.
120
	 *
121
	 * @param string $class
122
	 */
123
	function __construct(string $class = '') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
124
		$this->class = $class;
125
		$this->data = new SimpleDataStore();
126
		$this->result = new SimpleDataStore();
127
		$this->readingOutcome = new SimpleDataStore();
128
		$this->dataOutcome = new SimpleDataStore();
129
	}
130
131
132
	/**
133
	 * @return string
134
	 */
135
	public function getClass(): string {
136
		return $this->class;
137
	}
138
139
	/**
140
	 * @param mixed $class
141
	 *
142
	 * @return self
143
	 */
144
	public function setClass($class): self {
145
		$this->class = $class;
146
147
		return $this;
148
	}
149
150
151
	/**
152
	 * @return string
153
	 */
154
	public function getSource(): string {
155
		return $this->source;
156
	}
157
158
	/**
159
	 * @param string $source
160
	 *
161
	 * @return self
162
	 */
163
	public function setSource(string $source): self {
164
		$this->source = $source;
165
166
		if ($this->hasMember() && $this->member->getInstance() === '') {
167
			$this->member->setInstance($source);
168
		}
169
170
//		if ($this->hasCircle()
171
//			&& $this->getCircle()
172
//					->hasViewer()
173
//			&& $this->getCircle()
174
//					->getViewer()
175
//					->getInstance() === '') {
176
//			$this->getCircle()
177
//				 ->getViewer()
178
//				 ->setInstance($source);
179
//		}
180
181
		return $this;
182
	}
183
184
185
	/**
186
	 * @return bool
187
	 */
188
	public function isAsync(): bool {
189
		return $this->async;
190
	}
191
192
	/**
193
	 * @param bool $async
194
	 *
195
	 * @return self
196
	 */
197
	public function setAsync(bool $async): self {
198
		$this->async = $async;
199
200
		return $this;
201
	}
202
203
204
	/**
205
	 * @return bool
206
	 */
207
	public function isLimitedToInstanceWithMember(): bool {
208
		return $this->limitedToInstanceWithMember;
209
	}
210
211
	/**
212
	 * @param bool $limitedToInstanceWithMember
213
	 *
214
	 * @return self
215
	 */
216
	public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self {
217
		$this->limitedToInstanceWithMember = $limitedToInstanceWithMember;
218
219
		return $this;
220
	}
221
222
223
	/**
224
	 * @return bool
225
	 */
226
	public function isDataRequestOnly(): bool {
227
		return $this->dataRequestOnly;
228
	}
229
230
	/**
231
	 * @param bool $dataRequestOnly
232
	 *
233
	 * @return self
234
	 */
235
	public function setDataRequestOnly(bool $dataRequestOnly): self {
236
		$this->dataRequestOnly = $dataRequestOnly;
237
238
		return $this;
239
	}
240
241
242
	/**
243
	 * @param string $incomingOrigin
244
	 *
245
	 * @return self
246
	 */
247
	public function setIncomingOrigin(string $incomingOrigin): self {
248
		$this->incomingOrigin = $incomingOrigin;
249
250
		return $this;
251
	}
252
253
	/**
254
	 * @return string
255
	 */
256
	public function getIncomingOrigin(): string {
257
		return $this->incomingOrigin;
258
	}
259
260
261
	/**
262
	 * @param bool $verifiedViewer
263
	 *
264
	 * @return FederatedEvent
265
	 */
266
	public function setVerifiedViewer(bool $verifiedViewer): self {
267
		$this->verifiedViewer = $verifiedViewer;
268
269
		return $this;
270
	}
271
272
//	/**
273
//	 * @return bool
274
//	 */
275
//	public function isVerifiedViewer(): bool {
276
//		return $this->verifiedViewer;
277
//	}
278
279
//	/**
280
//	 * @throws InitiatorNotConfirmedException
281
//	 */
282
//	public function confirmVerifiedViewer(): void {
283
//		if ($this->isVerifiedViewer()) {
284
//			return;
285
//		}
286
//
287
//		throw new InitiatorNotConfirmedException();
288
//	}
289
290
291
//	/**
292
//	 * @param bool $verifiedCircle
293
//	 *
294
//	 * @return FederatedEvent
295
//	 */
296
//	public function setVerifiedCircle(bool $verifiedCircle): self {
297
//		$this->verifiedCircle = $verifiedCircle;
298
//
299
//		return $this;
300
//	}
301
//
302
//	/**
303
//	 * @return bool
304
//	 */
305
//	public function isVerifiedCircle(): bool {
306
//		return $this->verifiedCircle;
307
//	}
308
309
310
	/**
311
	 * @param string $wrapperToken
312
	 *
313
	 * @return FederatedEvent
314
	 */
315
	public function setWrapperToken(string $wrapperToken): self {
316
		$this->wrapperToken = $wrapperToken;
317
318
		return $this;
319
	}
320
321
	/**
322
	 * @return string
323
	 */
324
	public function getWrapperToken(): string {
325
		return $this->wrapperToken;
326
	}
327
328
329
	/**
330
	 * @return bool
331
	 */
332
	public function hasCircle(): bool {
333
		return ($this->circle !== null);
334
	}
335
336
	/**
337
	 * @param Circle $circle
338
	 *
339
	 * @return self
340
	 */
341
	public function setCircle(Circle $circle): self {
342
		$this->circle = $circle;
343
344
		return $this;
345
	}
346
347
	/**
348
	 * @return Circle
349
	 */
350
	public function getCircle(): Circle {
351
		return $this->circle;
352
	}
353
354
355
356
	/**
357
	 * @param string $itemId
358
	 *
359
	 * @return self
360
	 */
361
	public function setItemId(string $itemId): self {
362
		$this->itemId = $itemId;
363
364
		return $this;
365
	}
366
367
	/**
368
	 * @return string
369
	 */
370
	public function getItemId(): string {
371
		return $this->itemId;
372
	}
373
374
375
	/**
376
	 * @return Member
377
	 */
378
	public function getMember(): Member {
379
		return $this->member;
380
	}
381
382
	/**
383
	 * @param Member|null $member
384
	 *
385
	 * @return self
386
	 */
387
	public function setMember(?Member $member): self {
388
		$this->member = $member;
389
390
		return $this;
391
	}
392
393
	/**
394
	 * @return bool
395
	 */
396
	public function hasMember(): bool {
397
		return ($this->member !== null);
398
	}
399
400
401
	/**
402
	 * @param SimpleDataStore $data
403
	 *
404
	 * @return self
405
	 */
406
	public function setData(SimpleDataStore $data): self {
407
		$this->data = $data;
408
409
		return $this;
410
	}
411
412
	/**
413
	 * @return SimpleDataStore
414
	 */
415
	public function getData(): SimpleDataStore {
416
		return $this->data;
417
	}
418
419
420
	/**
421
	 * @return int
422
	 */
423
	public function getSeverity(): int {
424
		return $this->severity;
425
	}
426
427
	/**
428
	 * @param int $severity
429
	 *
430
	 * @return self
431
	 */
432
	public function setSeverity(int $severity): self {
433
		$this->severity = $severity;
434
435
		return $this;
436
	}
437
438
439
	/**
440
	 * @return SimpleDataStore
441
	 */
442
	public function getOutcome(): SimpleDataStore {
443
		return new SimpleDataStore(
444
			[
445
				'reading' => $this->getReadingOutcome(),
446
				'data'    => $this->getDataOutcome()
447
			]
448
		);
449
	}
450
451
	/**
452
	 * @return SimpleDataStore
453
	 */
454
	public function getReadingOutcome(): SimpleDataStore {
455
		return $this->readingOutcome;
456
	}
457
458
	/**
459
	 * @param string $message
460
	 * @param array $params
461
	 * @param bool $fail
462
	 *
463
	 * @return $this
464
	 */
465
	public function setReadingOutcome(string $message, array $params = [], bool $fail = false): self {
466
		$this->readingOutcome = new SimpleDataStore(
467
			[
468
				'message' => $message,
469
				'params'  => $params,
470
				'fail'    => $fail
471
			]
472
		);
473
474
		return $this;
475
	}
476
477
478
	/**
479
	 * @return SimpleDataStore
480
	 */
481
	public function getDataOutcome(): SimpleDataStore {
482
		return $this->dataOutcome;
483
	}
484
485
	/**
486
	 * @param array $data
487
	 *
488
	 * @return $this
489
	 */
490
	public function setDataOutcome(array $data): self {
491
		$this->dataOutcome = new SimpleDataStore($data);
492
493
		return $this;
494
	}
495
496
497
	/**
498
	 * @return SimpleDataStore
499
	 */
500
	public function getResult(): SimpleDataStore {
501
		return $this->result;
502
	}
503
504
	/**
505
	 * @param SimpleDataStore $result
506
	 *
507
	 * @return self
508
	 */
509
	public function setResult(SimpleDataStore $result): self {
510
		$this->result = $result;
511
512
		return $this;
513
	}
514
515
516
	/**
517
	 * @param array $data
518
	 *
519
	 * @return self
520
	 * @throws InvalidItemException
521
	 */
522
	public function import(array $data): self {
523
		$this->setClass($this->get('class', $data));
524
		$this->setSeverity($this->getInt('severity', $data));
525
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
526
		$this->setResult(new SimpleDataStore($this->getArray('result', $data)));
527
		$this->setSource($this->get('source', $data));
528
529
		if (array_key_exists('circle', $data)) {
530
			$circle = new Circle();
531
			$circle->import($this->getArray('circle', $data));
532
			$this->setCircle($circle);
533
		}
534
535
		if (array_key_exists('member', $data)) {
536
			$member = new Member();
537
			$member->import($this->getArray('member', $data));
538
			$this->setMember($member);
539
		}
540
541
		return $this;
542
	}
543
544
545
	/**
546
	 * @return array
547
	 */
548
	function jsonSerialize(): array {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
549
		$arr = [
550
			'class'    => $this->getClass(),
551
			'severity' => $this->getSeverity(),
552
			'data'     => $this->getData(),
553
			'result'   => $this->getResult(),
554
			'source'   => $this->getSource(),
555
			'outcome'  => [
556
				'message' => $this->getReadingOutcome(),
557
				'data'    => $this->getDataOutcome()
558
			]
559
		];
560
561
		if ($this->hasCircle()) {
562
			$arr['circle'] = $this->getCircle();
563
		}
564
		if ($this->hasMember()) {
565
			$arr['member'] = $this->getMember();
566
		}
567
568
		return $arr;
569
	}
570
571
572
	/**
573
	 * @param int $flag
574
	 *
575
	 * @return FederatedEvent
576
	 */
577
	public function bypass(int $flag): self {
578
		if (!$this->canBypass($flag)) {
579
			$this->bypass += $flag;
580
		}
581
582
		return $this;
583
	}
584
585
	/**
586
	 * @param int $flag
587
	 *
588
	 * @return bool
589
	 */
590
	public function canBypass(int $flag): bool {
591
		return (($this->bypass & $flag) !== 0);
592
	}
593
594
}
595
596