Completed
Pull Request — master (#551)
by Maxence
01:55
created

RemoteEvent::getIncomingOrigin()   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\Remote;
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 RemoteEvent
44
 *
45
 * @package OCA\Circles\Model\Remote
46
 */
47
class RemoteEvent implements JsonSerializable {
48
49
50
	const SEVERITY_LOW = 1;
51
	const SEVERITY_HIGH = 3;
52
53
	const BYPASS_LOCALCIRCLECHECK = 1;
54
55
56
	use TArrayTools;
57
58
59
	/** @var string */
60
	private $class;
61
62
	/** @var string */
63
	private $source = '';
64
65
	/** @var Circle */
66
	private $circle;
67
68
	/** @var Member */
69
	private $member;
70
71
	/** @var SimpleDataStore */
72
	private $data;
73
74
	/** @var int */
75
	private $severity = self::SEVERITY_LOW;
76
77
	/** @var SimpleDataStore */
78
	private $result;
79
80
	/** @var bool */
81
	private $local;
82
83
	/** @var bool */
84
	private $async = false;
85
86
	/** @var string */
87
	private $incomingOrigin = '';
88
89
90
	/** @var string */
91
	private $wrapperToken = '';
92
93
	/** @var bool */
94
	private $verifiedViewer = false;
95
96
	/** @var bool */
97
	private $verifiedCircle = false;
98
99
	/** @var int */
100
	private $bypass = 0;
101
102
103
	/**
104
	 * RemoteEvent constructor.
105
	 *
106
	 * @param string $class
107
	 * @param bool $local
108
	 */
109 View Code Duplication
	function __construct(string $class = '', bool $local = false) {
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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