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

RemoteEvent   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 394
Duplicated Lines 1.52 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 37
lcom 1
cbo 4
dl 6
loc 394
rs 9.44
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A getClass() 0 3 1
A setClass() 0 5 1
A getSource() 0 3 1
A setSource() 0 20 3
A isLocal() 0 3 1
A setLocal() 0 5 1
A isAsync() 0 3 1
A setAsync() 0 5 1
A setIncomingOrigin() 0 5 1
A getIncomingOrigin() 0 3 1
A setVerifiedViewer() 0 5 1
A isVerifiedViewer() 0 3 1
A setVerifiedCircle() 0 5 1
A isVerifiedCircle() 0 3 1
A setWrapperToken() 0 5 1
A getWrapperToken() 0 3 1
A hasCircle() 0 3 1
A setCircle() 0 5 1
A getCircle() 0 3 1
A getMember() 0 3 1
A setMember() 0 5 1
A hasMember() 0 3 1
A setData() 0 5 1
A getData() 0 3 1
A getSeverity() 0 3 1
A setSeverity() 0 5 1
A getResult() 0 3 1
A setResult() 0 5 1
A import() 0 22 3
A jsonSerialize() 0 19 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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