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

RemoteEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Remote;
33
34
35
use daita\MySmallPhpTools\Model\SimpleDataStore;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use JsonSerializable;
38
use OCA\Circles\Exceptions\ViewerNotConfirmedException;
39
use OCA\Circles\Model\Circle;
40
use OCA\Circles\Model\Member;
41
42
43
/**
44
 * Class RemoteEvent
45
 *
46
 * @package OCA\Circles\Model\Remote
47
 */
48
class RemoteEvent implements JsonSerializable {
49
50
51
	const SEVERITY_LOW = 1;
52
	const SEVERITY_HIGH = 3;
53
54
	const BYPASS_LOCALCIRCLECHECK = 1;
55
	const BYPASS_VIEWERCHECK = 2;
56
57
58
	use TArrayTools;
59
60
61
	/** @var string */
62
	private $class;
63
64
	/** @var string */
65
	private $source = '';
66
67
	/** @var Circle */
68
	private $circle;
69
70
	/** @var Member */
71
	private $member;
72
73
	/** @var SimpleDataStore */
74
	private $data;
75
76
	/** @var int */
77
	private $severity = self::SEVERITY_LOW;
78
79
	/** @var SimpleDataStore */
80
	private $result;
81
82
	/** @var bool */
83
	private $async = false;
84
85
	/** @var string */
86
	private $incomingOrigin = '';
87
88
89
	/** @var string */
90
	private $wrapperToken = '';
91
92
	/** @var bool */
93
	private $verifiedViewer = false;
94
95
	/** @var bool */
96
	private $verifiedCircle = false;
97
98
	/** @var int */
99
	private $bypass = 0;
100
101
102
	/**
103
	 * RemoteEvent constructor.
104
	 *
105
	 * @param string $class
106
	 */
107
	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...
108
		$this->class = $class;
109
		$this->data = new SimpleDataStore();
110
		$this->result = new SimpleDataStore();
111
	}
112
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getClass(): string {
118
		return $this->class;
119
	}
120
121
	/**
122
	 * @param mixed $class
123
	 *
124
	 * @return self
125
	 */
126
	public function setClass($class): self {
127
		$this->class = $class;
128
129
		return $this;
130
	}
131
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getSource(): string {
137
		return $this->source;
138
	}
139
140
	/**
141
	 * @param string $source
142
	 *
143
	 * @return self
144
	 */
145
	public function setSource(string $source): self {
146
		$this->source = $source;
147
148
		if ($this->hasMember() && $this->member->getInstance() === '') {
149
			$this->member->setInstance($source);
150
		}
151
152
//		if ($this->hasCircle()
153
//			&& $this->getCircle()
154
//					->hasViewer()
155
//			&& $this->getCircle()
156
//					->getViewer()
157
//					->getInstance() === '') {
158
//			$this->getCircle()
159
//				 ->getViewer()
160
//				 ->setInstance($source);
161
//		}
162
163
		return $this;
164
	}
165
166
167
	/**
168
	 * @return bool
169
	 */
170
	public function isAsync(): bool {
171
		return $this->async;
172
	}
173
174
	/**
175
	 * @param bool $async
176
	 *
177
	 * @return self
178
	 */
179
	public function setAsync(bool $async): self {
180
		$this->async = $async;
181
182
		return $this;
183
	}
184
185
	/**
186
	 * @param string $incomingOrigin
187
	 *
188
	 * @return self
189
	 */
190
	public function setIncomingOrigin(string $incomingOrigin): self {
191
		$this->incomingOrigin = $incomingOrigin;
192
193
		return $this;
194
	}
195
196
	/**
197
	 * @return string
198
	 */
199
	public function getIncomingOrigin(): string {
200
		return $this->incomingOrigin;
201
	}
202
203
204
	/**
205
	 * @param bool $verifiedViewer
206
	 *
207
	 * @return RemoteEvent
208
	 */
209
	public function setVerifiedViewer(bool $verifiedViewer): self {
210
		$this->verifiedViewer = $verifiedViewer;
211
212
		return $this;
213
	}
214
215
	/**
216
	 * @return bool
217
	 */
218
	public function isVerifiedViewer(): bool {
219
		return $this->verifiedViewer;
220
	}
221
222
	/**
223
	 * @throws ViewerNotConfirmedException
224
	 */
225
	public function confirmVerifiedViewer(): void {
226
		if ($this->isVerifiedViewer()) {
227
			return;
228
		}
229
230
		throw new ViewerNotConfirmedException();
231
	}
232
233
234
	/**
235
	 * @param bool $verifiedCircle
236
	 *
237
	 * @return RemoteEvent
238
	 */
239
	public function setVerifiedCircle(bool $verifiedCircle): self {
240
		$this->verifiedCircle = $verifiedCircle;
241
242
		return $this;
243
	}
244
245
	/**
246
	 * @return bool
247
	 */
248
	public function isVerifiedCircle(): bool {
249
		return $this->verifiedCircle;
250
	}
251
252
253
	/**
254
	 * @param string $wrapperToken
255
	 *
256
	 * @return RemoteEvent
257
	 */
258
	public function setWrapperToken(string $wrapperToken): self {
259
		$this->wrapperToken = $wrapperToken;
260
261
		return $this;
262
	}
263
264
	/**
265
	 * @return string
266
	 */
267
	public function getWrapperToken(): string {
268
		return $this->wrapperToken;
269
	}
270
271
272
	/**
273
	 * @return bool
274
	 */
275
	public function hasCircle(): bool {
276
		return ($this->circle !== null);
277
	}
278
279
	/**
280
	 * @param Circle $circle
281
	 *
282
	 * @return self
283
	 */
284
	public function setCircle(Circle $circle): self {
285
		$this->circle = $circle;
286
287
		return $this;
288
	}
289
290
	/**
291
	 * @return Circle
292
	 */
293
	public function getCircle(): Circle {
294
		return $this->circle;
295
	}
296
297
298
	/**
299
	 * @return Member
300
	 */
301
	public function getMember(): Member {
302
		return $this->member;
303
	}
304
305
	/**
306
	 * @param Member $member
307
	 *
308
	 * @return self
309
	 */
310
	public function setMember(Member $member): self {
311
		$this->member = $member;
312
313
		return $this;
314
	}
315
316
	/**
317
	 * @return bool
318
	 */
319
	public function hasMember(): bool {
320
		return ($this->member !== null);
321
	}
322
323
324
	/**
325
	 * @param SimpleDataStore $data
326
	 *
327
	 * @return self
328
	 */
329
	public function setData(SimpleDataStore $data): self {
330
		$this->data = $data;
331
332
		return $this;
333
	}
334
335
	/**
336
	 * @return SimpleDataStore
337
	 */
338
	public function getData(): SimpleDataStore {
339
		return $this->data;
340
	}
341
342
343
	/**
344
	 * @return int
345
	 */
346
	public function getSeverity(): int {
347
		return $this->severity;
348
	}
349
350
	/**
351
	 * @param int $severity
352
	 *
353
	 * @return self
354
	 */
355
	public function setSeverity(int $severity): self {
356
		$this->severity = $severity;
357
358
		return $this;
359
	}
360
361
362
	/**
363
	 * @return SimpleDataStore
364
	 */
365
	public function getResult(): SimpleDataStore {
366
		return $this->result;
367
	}
368
369
	/**
370
	 * @param SimpleDataStore $result
371
	 *
372
	 * @return self
373
	 */
374
	public function setResult(SimpleDataStore $result): self {
375
		$this->result = $result;
376
377
		return $this;
378
	}
379
380
381
	/**
382
	 * @param array $data
383
	 *
384
	 * @return self
385
	 */
386
	public function import(array $data): self {
387
		$this->setClass($this->get('class', $data));
388
		$this->setSeverity($this->getInt('severity', $data));
389
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
390
		$this->setResult(new SimpleDataStore($this->getArray('result', $data)));
391
		$this->setSource($this->get('source', $data));
392
		$this->setAsync($this->getBool('async', $data));
393
394
		if (array_key_exists('circle', $data)) {
395
			$circle = new Circle();
396
			$circle->import($this->getArray('circle', $data));
397
			$this->setCircle($circle);
398
		}
399
400
		if (array_key_exists('member', $data)) {
401
			$member = new Member();
402
			$member->import($this->getArray('member', $data));
403
			$this->setMember($member);
404
		}
405
406
		return $this;
407
	}
408
409
410
	/**
411
	 * @return array
412
	 */
413
	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...
414
		$arr = [
415
			'class'    => $this->getClass(),
416
			'severity' => $this->getSeverity(),
417
			'data'     => $this->getData(),
418
			'result'   => $this->getResult(),
419
			'source'   => $this->getSource(),
420
			'async'    => $this->isAsync()
421
		];
422
423
		if ($this->hasCircle()) {
424
			$arr['circle'] = $this->getCircle();
425
		}
426
		if ($this->hasMember()) {
427
			$arr['member'] = $this->getMember();
428
		}
429
430
		return $arr;
431
	}
432
433
434
	/**
435
	 * @param int $flag
436
	 *
437
	 * @return RemoteEvent
438
	 */
439
	public function bypass(int $flag): self {
440
		if (!$this->canBypass($flag)) {
441
			$this->bypass += $flag;
442
		}
443
444
		return $this;
445
	}
446
447
	/**
448
	 * @param int $flag
449
	 *
450
	 * @return bool
451
	 */
452
	public function canBypass(int $flag): bool {
453
		return (($this->bypass & $flag) !== 0);
454
	}
455
456
}
457
458