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

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