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

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