Completed
Pull Request — master (#362)
by Maxence
02:14
created

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