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