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

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