Completed
Pull Request — master (#362)
by Maxence
01:50
created

GSEvent::getData()   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_INVITE = 'GlobalScale\MemberInvite';
62
	const MEMBER_LEAVE = 'GlobalScale\MemberLeave';
63
	const MEMBER_LEVEL = 'GlobalScale\MemberLevel';
64
	const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
65
	const MEMBER_REMOVE = 'GlobalScale\MemberRemove';
66
	const USER_DELETED = 'GlobalScale\UserDeleted';
67
68
	const FILE_SHARE = 'GlobalScale\FileShare';
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
105
	/**
106
	 * GSEvent constructor.
107
	 *
108
	 * @param string $type
109
	 * @param bool $local
110
	 * @param bool $force
111
	 */
112
	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...
113
		$this->type = $type;
114
		$this->local = $local;
115
		$this->force = $force;
116
		$this->data = new SimpleDataStore();
117
		$this->result = new SimpleDataStore();
118
	}
119
120
121
	/**
122
	 * @return string
123
	 */
124
	public function getType(): string {
125
		return $this->type;
126
	}
127
128
	/**
129
	 * @param mixed $type
130
	 *
131
	 * @return GSEvent
132
	 */
133
	public function setType($type): self {
134
		$this->type = $type;
135
136
		return $this;
137
	}
138
139
140
	/**
141
	 * @return string
142
	 */
143
	public function getSource(): string {
144
		return $this->source;
145
	}
146
147
	/**
148
	 * @param string $source
149
	 *
150
	 * @return GSEvent
151
	 */
152
	public function setSource(string $source): self {
153
		$this->source = $source;
154
155
		if ($this->hasMember() && $this->member->getInstance() === '') {
156
			$this->member->setInstance($source);
157
		}
158
159
		if ($this->hasCircle()
160
			&& $this->getCircle()
161
					->hasViewer()
162
			&& $this->getCircle()
163
					->getViewer()
164
					->getInstance() === '') {
165
			$this->getCircle()
166
				 ->getViewer()
167
				 ->setInstance($source);
168
		}
169
170
		return $this;
171
	}
172
173
174
	/**
175
	 * @return bool
176
	 */
177
	public function isLocal(): bool {
178
		return $this->local;
179
	}
180
181
	/**
182
	 * @param bool $local
183
	 *
184
	 * @return GSEvent
185
	 */
186
	public function setLocal(bool $local): self {
187
		$this->local = $local;
188
189
		return $this;
190
	}
191
192
193
	/**
194
	 * @return bool
195
	 */
196
	public function isForced(): bool {
197
		return $this->force;
198
	}
199
200
	/**
201
	 * @param bool $force
202
	 *
203
	 * @return GSEvent
204
	 */
205
	public function setForced(bool $force): self {
206
		$this->force = $force;
207
208
		return $this;
209
	}
210
211
212
	/**
213
	 * @return Circle
214
	 */
215
	public function getCircle(): Circle {
216
		return $this->circle;
217
	}
218
219
	/**
220
	 * @param Circle $circle
221
	 *
222
	 * @return GSEvent
223
	 */
224
	public function setCircle(Circle $circle): self {
225
		$this->circle = $circle;
226
227
		return $this;
228
	}
229
230
	/**
231
	 * @return bool
232
	 */
233
	public function hasCircle(): bool {
234
		return ($this->circle !== null);
235
	}
236
237
238
	/**
239
	 * @return Member
240
	 */
241
	public function getMember(): Member {
242
		return $this->member;
243
	}
244
245
	/**
246
	 * @param Member $member
247
	 *
248
	 * @return GSEvent
249
	 */
250
	public function setMember(Member $member): self {
251
		$this->member = $member;
252
253
		return $this;
254
	}
255
256
	/**
257
	 * @return bool
258
	 */
259
	public function hasMember(): bool {
260
		return ($this->member !== null);
261
	}
262
263
264
	/**
265
	 * @param SimpleDataStore $data
266
	 *
267
	 * @return GSEvent
268
	 */
269
	public function setData(SimpleDataStore $data): self {
270
		$this->data = $data;
271
272
		return $this;
273
	}
274
275
	/**
276
	 * @return SimpleDataStore
277
	 */
278
	public function getData(): SimpleDataStore {
279
		return $this->data;
280
	}
281
282
283
	/**
284
	 * @return int
285
	 */
286
	public function getSeverity(): int {
287
		return $this->severity;
288
	}
289
290
	/**
291
	 * @param int $severity
292
	 *
293
	 * @return GSEvent
294
	 */
295
	public function setSeverity(int $severity): self {
296
		$this->severity = $severity;
297
298
		return $this;
299
	}
300
301
302
	/**
303
	 * @return SimpleDataStore
304
	 */
305
	public function getResult(): SimpleDataStore {
306
		return $this->result;
307
	}
308
309
	/**
310
	 * @param SimpleDataStore $result
311
	 *
312
	 * @return GSEvent
313
	 */
314
	public function setResult(SimpleDataStore $result): self {
315
		$this->result = $result;
316
317
		return $this;
318
	}
319
320
321
	/**
322
	 * @return string
323
	 */
324
	public function getKey(): string {
325
		return $this->key;
326
	}
327
328
	/**
329
	 * @param string $key
330
	 *
331
	 * @return GSEvent
332
	 */
333
	public function setKey(string $key): self {
334
		$this->key = $key;
335
336
		return $this;
337
	}
338
339
340
	/**
341
	 * @return bool
342
	 */
343
	public function isValid(): bool {
344
		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...
345
			return false;
346
		}
347
348
		return true;
349
	}
350
351
352
	/**
353
	 * @param string $json
354
	 *
355
	 * @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...
356
	 * @throws JsonException
357
	 * @throws ModelException
358
	 */
359
	public function importFromJson(string $json): self {
360
		$data = json_decode($json, true);
361
362
		if (!is_array($data)) {
363
			throw new JsonException('invalid JSON');
364
		}
365
366
		return $this->import($data);
367
	}
368
369
370
	/**
371
	 * @param array $data
372
	 *
373
	 * @return GSEvent
374
	 * @throws ModelException
375
	 */
376
	public function import(array $data): self {
377
		$this->setType($this->get('type', $data));
378
		$this->setSeverity($this->getInt('severity', $data));
379
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
380
		$this->setResult(new SimpleDataStore($this->getArray('result', $data)));
381
		$this->setSource($this->get('source', $data));
382
		$this->setKey($this->get('key', $data));
383
		$this->setForced($this->getBool('force', $data));
384
385
		if (array_key_exists('circle', $data)) {
386
			$this->setCircle(Circle::fromArray($data['circle']));
387
		}
388
389
		if (array_key_exists('member', $data)) {
390
			$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...
391
		}
392
393
		if (!$this->isValid()) {
394
			throw new ModelException('invalid GSEvent');
395
		}
396
397
		return $this;
398
	}
399
400
401
	/**
402
	 * @return array
403
	 */
404
	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...
405
		$arr = [
406
			'type'     => $this->getType(),
407
			'severity' => $this->getSeverity(),
408
			'data'     => $this->getData(),
409
			'result'   => $this->getResult(),
410
			'key'      => $this->getKey(),
411
			'source'   => $this->getSource(),
412
			'force'    => $this->isForced()
413
		];
414
415
		if ($this->hasCircle()) {
416
			$arr['circle'] = $this->getCircle();
417
		}
418
		if ($this->hasMember()) {
419
			$arr['member'] = $this->getMember();
420
		}
421
422
		$this->cleanArray($arr);
423
424
		return $arr;
425
	}
426
427
428
}
429
430