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

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