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

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