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

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