Completed
Push — master ( 3aeedd...be0cda )
by Maxence
03:35 queued 11s
created

FederatedEvent::bypass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
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_CIRCLE = 1;
55
	const BYPASS_LOCALCIRCLECHECK = 2;
56
	const BYPASS_LOCALMEMBERCHECK = 4;
57
	const BYPASS_INITIATORCHECK = 8;
58
	const BYPASS_INITIATORMEMBERSHIP = 16;
59
60
	use TArrayTools;
61
62
63
	/** @var string */
64
	private $class;
65
66
	/** @var string */
67
	private $origin = '';
68
69
	/** @var array */
70
	private $interfaces = [];
71
72
	/** @var Circle */
73
	private $circle;
74
75
	/** @var string */
76
	private $itemId = '';
77
78
	/** @var string */
79
	private $itemSource = '';
80
81
	/** @var Member */
82
	private $member;
83
84
	/** @var Member[] */
85
	private $members = [];
86
87
	/** @var SimpleDataStore */
88
	private $params;
89
90
	/** @var SimpleDataStore */
91
	private $data;
92
93
	/** @var int */
94
	private $severity = self::SEVERITY_LOW;
95
96
	/** @var array */
97
	private $outcome = [];
98
99
	/** @var SimpleDataStore */
100
	private $result;
101
102
	/** @var bool */
103
	private $async = false;
104
105
	/** @var bool */
106
	private $limitedToInstanceWithMember = false;
107
108
	/** @var bool */
109
	private $dataRequestOnly = false;
110
111
	/** @var string */
112
	private $sender = '';
113
114
115
	/** @var string */
116
	private $wrapperToken = '';
117
118
	/** @var int */
119
	private $bypass = 0;
120
121
122
	/**
123
	 * FederatedEvent constructor.
124
	 *
125
	 * @param string $class
126
	 */
127
	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...
128
		$this->class = $class;
129
		$this->params = new SimpleDataStore();
130
		$this->data = new SimpleDataStore();
131
		$this->result = new SimpleDataStore();
132
	}
133
134
135
	/**
136
	 * @return string
137
	 */
138
	public function getClass(): string {
139
		return $this->class;
140
	}
141
142
	/**
143
	 * @param mixed $class
144
	 *
145
	 * @return self
146
	 */
147
	public function setClass($class): self {
148
		$this->class = $class;
149
150
		return $this;
151
	}
152
153
154
	/**
155
	 * Origin of the event.
156
	 *
157
	 * @return string
158
	 */
159
	public function getOrigin(): string {
160
		return $this->origin;
161
	}
162
163
	/**
164
	 * @param string $origin
165
	 *
166
	 * @return self
167
	 */
168
	public function setOrigin(string $origin): self {
169
		$this->origin = $origin;
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * @return array
177
	 */
178
	public function getInterfaces(): array {
179
		return $this->interfaces;
180
	}
181
182
	/**
183
	 * @param array $interfaces
184
	 *
185
	 * @return FederatedEvent
186
	 */
187
	public function setInterfaces(array $interfaces): self {
188
		$this->interfaces = $interfaces;
189
190
		return $this;
191
	}
192
193
	/**
194
	 * @return $this
195
	 */
196
	public function obfuscateInterfaces(): self {
197
		$this->interfaces = [];
198
199
		return $this;
200
	}
201
202
203
	/**
204
	 * @return bool
205
	 */
206
	public function isAsync(): bool {
207
		return $this->async;
208
	}
209
210
	/**
211
	 * @param bool $async
212
	 *
213
	 * @return self
214
	 */
215
	public function setAsync(bool $async): self {
216
		$this->async = $async;
217
218
		return $this;
219
	}
220
221
222
	/**
223
	 * @return bool
224
	 */
225
	public function isLimitedToInstanceWithMember(): bool {
226
		return $this->limitedToInstanceWithMember;
227
	}
228
229
	/**
230
	 * @param bool $limitedToInstanceWithMember
231
	 *
232
	 * @return self
233
	 */
234
	public function setLimitedToInstanceWithMember(bool $limitedToInstanceWithMember): self {
235
		$this->limitedToInstanceWithMember = $limitedToInstanceWithMember;
236
237
		return $this;
238
	}
239
240
241
	/**
242
	 * @return bool
243
	 */
244
	public function isDataRequestOnly(): bool {
245
		return $this->dataRequestOnly;
246
	}
247
248
	/**
249
	 * @param bool $dataRequestOnly
250
	 *
251
	 * @return self
252
	 */
253
	public function setDataRequestOnly(bool $dataRequestOnly): self {
254
		$this->dataRequestOnly = $dataRequestOnly;
255
256
		return $this;
257
	}
258
259
260
	/**
261
	 *
262
	 * Origin of the request
263
	 *
264
	 * @param string $sender
265
	 *
266
	 * @return self
267
	 */
268
	public function setSender(string $sender): self {
269
		$this->sender = $sender;
270
271
		return $this;
272
	}
273
274
	/**
275
	 * @return string
276
	 */
277
	public function getSender(): string {
278
		return $this->sender;
279
	}
280
281
282
	/**
283
	 * @param string $wrapperToken
284
	 *
285
	 * @return FederatedEvent
286
	 */
287
	public function setWrapperToken(string $wrapperToken): self {
288
		$this->wrapperToken = $wrapperToken;
289
290
		return $this;
291
	}
292
293
	/**
294
	 * @return string
295
	 */
296
	public function getWrapperToken(): string {
297
		return $this->wrapperToken;
298
	}
299
300
301
	/**
302
	 * @return bool
303
	 */
304
	public function hasCircle(): bool {
305
		return ($this->circle !== null);
306
	}
307
308
	/**
309
	 * @param Circle $circle
310
	 *
311
	 * @return self
312
	 */
313
	public function setCircle(Circle $circle): self {
314
		$this->circle = $circle;
315
316
		return $this;
317
	}
318
319
	/**
320
	 * @return Circle
321
	 */
322
	public function getCircle(): Circle {
323
		return $this->circle;
324
	}
325
326
327
	/**
328
	 * @param string $itemId
329
	 *
330
	 * @return self
331
	 */
332
	public function setItemId(string $itemId): self {
333
		$this->itemId = $itemId;
334
335
		return $this;
336
	}
337
338
	/**
339
	 * @return string
340
	 */
341
	public function getItemId(): string {
342
		return $this->itemId;
343
	}
344
345
346
	/**
347
	 * @param string $itemSource
348
	 *
349
	 * @return self
350
	 */
351
	public function setItemSource(string $itemSource): self {
352
		$this->itemSource = $itemSource;
353
354
		return $this;
355
	}
356
357
	/**
358
	 * @return string
359
	 */
360
	public function getItemSource(): string {
361
		return $this->itemSource;
362
	}
363
364
365
	/**
366
	 * @return Member
367
	 */
368
	public function getMember(): Member {
369
		return $this->member;
370
	}
371
372
	/**
373
	 * @param Member|null $member
374
	 *
375
	 * @return self
376
	 */
377
	public function setMember(?Member $member): self {
378
		$this->member = $member;
379
380
		return $this;
381
	}
382
383
	/**
384
	 * @return bool
385
	 */
386
	public function hasMember(): bool {
387
		return ($this->member !== null);
388
	}
389
390
391
	/**
392
	 * @return Member[]
393
	 */
394
	public function getMembers(): array {
395
		return $this->members;
396
	}
397
398
	/**
399
	 * @param Member[] $members
400
	 *
401
	 * @return self
402
	 */
403
	public function setMembers(array $members): self {
404
		$this->members = $members;
405
406
		return $this;
407
	}
408
409
410
	/**
411
	 * @param SimpleDataStore $params
412
	 *
413
	 * @return self
414
	 */
415
	public function setParams(SimpleDataStore $params): self {
416
		$this->params = $params;
417
418
		return $this;
419
	}
420
421
	/**
422
	 * @return SimpleDataStore
423
	 */
424
	public function getParams(): SimpleDataStore {
425
		return $this->params;
426
	}
427
428
429
	/**
430
	 * @param SimpleDataStore $data
431
	 *
432
	 * @return self
433
	 */
434
	public function setData(SimpleDataStore $data): self {
435
		$this->data = $data;
436
437
		return $this;
438
	}
439
440
	/**
441
	 * @return SimpleDataStore
442
	 */
443
	public function getData(): SimpleDataStore {
444
		return $this->data;
445
	}
446
447
	/**
448
	 * @return $this
449
	 */
450
	public function resetData(): self {
451
		$this->data = new SimpleDataStore();
452
453
		return $this;
454
	}
455
456
457
	/**
458
	 * @return int
459
	 */
460
	public function getSeverity(): int {
461
		return $this->severity;
462
	}
463
464
	/**
465
	 * @param int $severity
466
	 *
467
	 * @return self
468
	 */
469
	public function setSeverity(int $severity): self {
470
		$this->severity = $severity;
471
472
		return $this;
473
	}
474
475
476
	/**
477
	 * @return array
478
	 */
479
	public function getOutcome(): array {
480
		return $this->outcome;
481
	}
482
483
	/**
484
	 * @param array $data
485
	 *
486
	 * @return $this
487
	 */
488
	public function setOutcome(array $data): self {
489
		$this->outcome = $data;
490
491
		return $this;
492
	}
493
494
495
	/**
496
	 * @return SimpleDataStore
497
	 */
498
	public function getResult(): SimpleDataStore {
499
		return $this->result;
500
	}
501
502
	/**
503
	 * @param SimpleDataStore $result
504
	 *
505
	 * @return self
506
	 */
507
	public function setResult(SimpleDataStore $result): self {
508
		$this->result = $result;
509
510
		return $this;
511
	}
512
513
	/**
514
	 * @param string $key
515
	 * @param array $result
516
	 *
517
	 * @return $this
518
	 */
519
	public function addResult(string $key, array $result): self {
520
		if (is_null($this->result)) {
521
			$this->result = new SimpleDataStore();
522
		}
523
524
		$this->result->aData($key, new SimpleDataStore($result));
525
526
		return $this;
527
	}
528
529
530
	/**
531
	 * @param int $flag
532
	 *
533
	 * @return FederatedEvent
534
	 */
535
	public function bypass(int $flag): self {
536
		if (!$this->canBypass($flag)) {
537
			$this->bypass += $flag;
538
		}
539
540
		return $this;
541
	}
542
543
	/**
544
	 * @param int $flag
545
	 *
546
	 * @return bool
547
	 */
548
	public function canBypass(int $flag): bool {
549
		return (($this->bypass & $flag) !== 0);
550
	}
551
552
553
	/**
554
	 * @param array $data
555
	 *
556
	 * @return self
557
	 * @throws InvalidItemException
558
	 */
559
	public function import(array $data): self {
560
		$this->setClass($this->get('class', $data));
561
		$this->setSeverity($this->getInt('severity', $data));
562
		$this->setParams(new SimpleDataStore($this->getArray('params', $data)));
563
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
564
		$this->setResult(new SimpleDataStore($this->getArray('result', $data)));
565
		$this->setOrigin($this->get('origin', $data));
566
		$this->setInterfaces($this->getArray('interfaces', $data));
567
		$this->setItemId($this->get('itemId', $data));
568
569
		try {
570
			$circle = new Circle();
571
			$circle->import($this->getArray('circle', $data));
572
			$this->setCircle($circle);
573
		} catch (InvalidItemException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
574
		}
575
576
		if (array_key_exists('member', $data)) {
577
			$member = new Member();
578
			$member->import($this->getArray('member', $data));
579
			$this->setMember($member);
580
		}
581
582
		$members = [];
583
		foreach ($this->getArray('members', $data) as $item) {
584
			$member = new Member();
585
			$members[] = $member->import($item);
586
		}
587
		$this->setMembers($members);
588
589
		return $this;
590
	}
591
592
593
	/**
594
	 * @return array
595
	 */
596
	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...
597
		$arr = [
598
			'class'      => $this->getClass(),
599
			'severity'   => $this->getSeverity(),
600
			'params'     => $this->getParams(),
601
			'data'       => $this->getData(),
602
			'result'     => $this->getResult(),
603
			'origin'     => $this->getOrigin(),
604
			'interfaces' => $this->getInterfaces(),
605
			'sender'     => $this->getSender(),
606
			'itemId'     => $this->getItemId(),
607
			'outcome'    => $this->getOutcome(),
608
			'members'    => $this->getMembers()
609
		];
610
611
		if ($this->hasCircle()) {
612
			$arr['circle'] = $this->getCircle();
613
		}
614
		if ($this->hasMember()) {
615
			$arr['member'] = $this->getMember();
616
		}
617
618
		return $arr;
619
	}
620
621
}
622
623