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

GSEvent::hasCircle()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Model\GlobalScale;
31
32
33
use daita\MySmallPhpTools\Model\SimpleDataStore;
34
use daita\MySmallPhpTools\Traits\TArrayTools;
35
use JsonSerializable;
36
use OCA\Circles\Exceptions\JsonException;
37
use OCA\Circles\Exceptions\ModelException;
38
use OCA\Circles\Model\Circle;
39
use OCA\Circles\Model\DeprecatedCircle;
40
use OCA\Circles\Model\DeprecatedMember;
41
42
43
/**
44
 * Class GSEvent
45
 *
46
 * @package OCA\Circles\Model\GlobalScale
47
 */
48
class GSEvent implements JsonSerializable {
49
50
51
	const SEVERITY_LOW = 1;
52
	const SEVERITY_HIGH = 3;
53
54
	const TEST = '\OCA\Circles\GlobalScale\Test';
55
	const GLOBAL_SYNC = '\OCA\Circles\GlobalScale\GlobalSync';
56
	const CIRCLE_STATUS = '\OCA\Circles\GlobalScale\CircleStatus';
57
58
	const CIRCLE_CREATE = '\OCA\Circles\GlobalScale\CircleCreate';
59
	const CIRCLE_UPDATE = '\OCA\Circles\GlobalScale\CircleUpdate';
60
	const CIRCLE_DESTROY = '\OCA\Circles\GlobalScale\CircleDestroy';
61
	const MEMBER_ADD = '\OCA\Circles\GlobalScale\MemberAdd';
62
	const MEMBER_JOIN = '\OCA\Circles\GlobalScale\MemberJoin';
63
	const MEMBER_LEAVE = '\OCA\Circles\GlobalScale\MemberLeave';
64
	const MEMBER_LEVEL = '\OCA\Circles\GlobalScale\MemberLevel';
65
	const MEMBER_UPDATE = '\OCA\Circles\GlobalScale\MemberUpdate';
66
	const MEMBER_REMOVE = '\OCA\Circles\GlobalScale\MemberRemove';
67
	const USER_DELETED = '\OCA\Circles\GlobalScale\UserDeleted';
68
69
	const FILE_SHARE = '\OCA\Circles\GlobalScale\FileShare';
70
	const FILE_UNSHARE = '\OCA\Circles\GlobalScale\FileUnshare';
71
72
73
	use TArrayTools;
74
75
76
	/** @var string */
77
	private $type = '';
78
79
	/** @var string */
80
	private $source = '';
81
82
	/** @var DeprecatedCircle */
83
	private $deprecatedCircle;
84
85
	/** @var Circle */
86
	private $circle;
87
88
	/** @var DeprecatedMember */
89
	private $member;
90
91
	/** @var SimpleDataStore */
92
	private $data;
93
94
	/** @var int */
95
	private $severity = self::SEVERITY_LOW;
96
97
	/** @var SimpleDataStore */
98
	private $result;
99
100
	/** @var string */
101
	private $key = '';
102
103
	/** @var bool */
104
	private $local = false;
105
106
	/** @var bool */
107
	private $force = false;
108
109
	/** @var bool */
110
	private $async = false;
111
112
	/** @var bool */
113
	private $checked = false;
114
115
116
	/**
117
	 * GSEvent constructor.
118
	 *
119
	 * @param string $type
120
	 * @param bool $local
121
	 * @param bool $force
122
	 */
123
	function __construct(string $type = '', bool $local = false, bool $force = 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...
124
		$this->type = $type;
125
		$this->local = $local;
126
		$this->force = $force;
127
		$this->data = new SimpleDataStore();
128
		$this->result = new SimpleDataStore();
129
	}
130
131
132
	/**
133
	 * @return string
134
	 */
135
	public function getType(): string {
136
		return $this->type;
137
	}
138
139
	/**
140
	 * @param mixed $type
141
	 *
142
	 * @return GSEvent
143
	 */
144
	public function setType($type): self {
145
		$this->type = $type;
146
147
		return $this;
148
	}
149
150
151
	/**
152
	 * @return string
153
	 */
154
	public function getSource(): string {
155
		return $this->source;
156
	}
157
158
	/**
159
	 * @param string $source
160
	 *
161
	 * @return GSEvent
162
	 */
163
	public function setSource(string $source): self {
164
		$this->source = $source;
165
166
		if ($this->hasMember() && $this->member->getInstance() === '') {
167
			$this->member->setInstance($source);
168
		}
169
170
		if ($this->hasCircle()
171
			&& $this->getDeprecatedCircle()
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Global...::getDeprecatedCircle() has been deprecated.

This method has been deprecated.

Loading history...
172
					->hasViewer()
173
			&& $this->getDeprecatedCircle()
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Global...::getDeprecatedCircle() has been deprecated.

This method has been deprecated.

Loading history...
174
					->getViewer()
175
					->getInstance() === '') {
176
			$this->getDeprecatedCircle()
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Global...::getDeprecatedCircle() has been deprecated.

This method has been deprecated.

Loading history...
177
				 ->getViewer()
178
				 ->setInstance($source);
179
		}
180
181
		return $this;
182
	}
183
184
185
	/**
186
	 * @return bool
187
	 */
188
	public function isLocal(): bool {
189
		return $this->local;
190
	}
191
192
	/**
193
	 * @param bool $local
194
	 *
195
	 * @return GSEvent
196
	 */
197
	public function setLocal(bool $local): self {
198
		$this->local = $local;
199
200
		return $this;
201
	}
202
203
204
	/**
205
	 * @return bool
206
	 */
207
	public function isForced(): bool {
208
		return $this->force;
209
	}
210
211
	/**
212
	 * @param bool $force
213
	 *
214
	 * @return GSEvent
215
	 */
216
	public function setForced(bool $force): self {
217
		$this->force = $force;
218
219
		return $this;
220
	}
221
222
223
	/**
224
	 * @return bool
225
	 */
226
	public function isAsync(): bool {
227
		return $this->async;
228
	}
229
230
	/**
231
	 * @param bool $async
232
	 *
233
	 * @return GSEvent
234
	 */
235
	public function setAsync(bool $async): self {
236
		$this->async = $async;
237
238
		return $this;
239
	}
240
241
242
	/**
243
	 * @return DeprecatedCircle
244
	 * @deprecated
245
	 */
246
	public function getDeprecatedCircle(): DeprecatedCircle {
247
		return $this->deprecatedCircle;
248
	}
249
250
	/**
251
	 * @param DeprecatedCircle $deprecatedCircle
252
	 *
253
	 * @return GSEvent
254
	 * @deprecated
255
	 */
256
	public function setDeprecatedCircle(DeprecatedCircle $deprecatedCircle): self {
257
		$this->deprecatedCircle = $deprecatedCircle;
258
259
		return $this;
260
	}
261
262
	/**
263
	 * @return bool
264
	 */
265
	public function hasCircle(): bool {
266
		return ($this->deprecatedCircle !== null || $this->circle !== null);
267
	}
268
269
	/**
270
	 * @param Circle $circle
271
	 *
272
	 * @return GSEvent
273
	 */
274
	public function setCircle(Circle $circle): self {
275
		$this->circle = $circle;
276
277
		return $this;
278
	}
279
280
	/**
281
	 * @return Circle
282
	 */
283
	public function getCircle(): Circle {
284
		return $this->circle;
285
	}
286
287
288
	/**
289
	 * @return DeprecatedMember
290
	 */
291
	public function getMember(): DeprecatedMember {
292
		return $this->member;
293
	}
294
295
	/**
296
	 * @param DeprecatedMember $member
297
	 *
298
	 * @return GSEvent
299
	 */
300
	public function setMember(DeprecatedMember $member): self {
301
		$this->member = $member;
302
303
		return $this;
304
	}
305
306
	/**
307
	 * @return bool
308
	 */
309
	public function hasMember(): bool {
310
		return ($this->member !== null);
311
	}
312
313
314
	/**
315
	 * @param SimpleDataStore $data
316
	 *
317
	 * @return GSEvent
318
	 */
319
	public function setData(SimpleDataStore $data): self {
320
		$this->data = $data;
321
322
		return $this;
323
	}
324
325
	/**
326
	 * @return SimpleDataStore
327
	 */
328
	public function getData(): SimpleDataStore {
329
		return $this->data;
330
	}
331
332
333
	/**
334
	 * @return int
335
	 */
336
	public function getSeverity(): int {
337
		return $this->severity;
338
	}
339
340
	/**
341
	 * @param int $severity
342
	 *
343
	 * @return GSEvent
344
	 */
345
	public function setSeverity(int $severity): self {
346
		$this->severity = $severity;
347
348
		return $this;
349
	}
350
351
352
	/**
353
	 * @return SimpleDataStore
354
	 */
355
	public function getResult(): SimpleDataStore {
356
		return $this->result;
357
	}
358
359
	/**
360
	 * @param SimpleDataStore $result
361
	 *
362
	 * @return GSEvent
363
	 */
364
	public function setResult(SimpleDataStore $result): self {
365
		$this->result = $result;
366
367
		return $this;
368
	}
369
370
371
	/**
372
	 * @return string
373
	 */
374
	public function getKey(): string {
375
		return $this->key;
376
	}
377
378
	/**
379
	 * @param string $key
380
	 *
381
	 * @return GSEvent
382
	 */
383
	public function setKey(string $key): self {
384
		$this->key = $key;
385
386
		return $this;
387
	}
388
389
390
	/**
391
	 * @return bool
392
	 */
393
	public function isValid(): bool {
394
		if ($this->getType() === '') {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->getType() === '');.
Loading history...
395
			return false;
396
		}
397
398
		return true;
399
	}
400
401
402
	/**
403
	 * @param string $json
404
	 *
405
	 * @return GSEvent
0 ignored issues
show
Documentation introduced by
Should the return type not be \self?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
406
	 * @throws JsonException
407
	 * @throws ModelException
408
	 */
409
	public function importFromJson(string $json): self {
410
		$data = json_decode($json, true);
411
		if (!is_array($data)) {
412
			throw new JsonException('invalid JSON');
413
		}
414
415
		return $this->import($data);
416
	}
417
418
419
	/**
420
	 * @param array $data
421
	 *
422
	 * @return GSEvent
423
	 * @throws ModelException
424
	 */
425
	public function import(array $data): self {
426
		$this->setType($this->get('type', $data));
427
		$this->setSeverity($this->getInt('severity', $data));
428
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
429
		$this->setResult(new SimpleDataStore($this->getArray('result', $data)));
430
		$this->setSource($this->get('source', $data));
431
		$this->setKey($this->get('key', $data));
432
		$this->setForced($this->getBool('force', $data));
433
		$this->setAsync($this->getBool('async', $data));
434
435
		if (array_key_exists('circle', $data)) {
436
			$this->setDeprecatedCircle(DeprecatedCircle::fromArray($data['circle']));
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Global...::setDeprecatedCircle() has been deprecated.

This method has been deprecated.

Loading history...
437
		}
438
439
		if (array_key_exists('member', $data)) {
440
			$this->setMember(DeprecatedMember::fromArray($data['member']));
0 ignored issues
show
Bug introduced by
It seems like \OCA\Circles\Model\Depre...mArray($data['member']) can be null; however, setMember() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
441
		}
442
443
		if (!$this->isValid()) {
444
			throw new ModelException('invalid GSEvent');
445
		}
446
447
		return $this;
448
	}
449
450
451
	/**
452
	 * @return array
453
	 */
454
	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...
455
		$arr = [
456
			'type'     => $this->getType(),
457
			'severity' => $this->getSeverity(),
458
			'data'     => $this->getData(),
459
			'result'   => $this->getResult(),
460
			'key'      => $this->getKey(),
461
			'source'   => $this->getSource(),
462
			'force'    => $this->isForced(),
463
			'async'    => $this->isAsync()
464
		];
465
466
		if ($this->hasCircle()) {
467
			$arr['circle'] = $this->getDeprecatedCircle();
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Global...::getDeprecatedCircle() has been deprecated.

This method has been deprecated.

Loading history...
468
		}
469
		if ($this->hasMember()) {
470
			$arr['member'] = $this->getMember();
471
		}
472
473
		$this->cleanArray($arr);
474
475
		return $arr;
476
	}
477
478
479
}
480
481