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

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