Completed
Push — master ( bb7cd9...98e766 )
by Maxence
02:35 queued 12s
created

FederatedEvent::import()   A

Complexity

Conditions 4
Paths 16

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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