Completed
Push — master ( 80bdb5...459619 )
by Maxence
03:02
created

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