Completed
Pull Request — master (#362)
by Maxence
01:55 queued 11s
created

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