Completed
Pull Request — master (#362)
by Maxence
02:57
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 CIRCLE_CREATE = 'GlobalScale\CircleCreate';
51
	const CIRCLE_UPDATE = 'GlobalScale\CircleUpdate';
52
	const CIRCLE_DELETE = 'GlobalScale\CircleDelete';
53
	const MEMBER_CREATE = 'GlobalScale\MemberCreate'; // used ?
54
	const MEMBER_JOIN = 'GlobalScale\MemberJoin';
55
	const MEMBER_INVITE = 'GlobalScale\MemberInvite';
56
	const MEMBER_LEAVE = 'GlobalScale\MemberLeave';
57
	const MEMBER_LEVEL = 'GlobalScale\MemberLevel';
58
	const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
59
	const MEMBER_DELETE = 'GlobalScale\MemberDelete';
60
61
62
	use TArrayTools;
63
64
65
	/** @var string */
66
	private $type = '';
67
68
	/** @var string */
69
	private $source = '';
70
71
	/** @var Circle */
72
	private $circle;
73
74
	/** @var Member */
75
	private $member;
76
77
	/** @var Member */
78
	private $viewer;
79
80
	private $data;
81
82
	/** @var string */
83
	private $key = '';
84
85
	/** @var bool */
86
	private $local = false;
87
88
89
	/**
90
	 * GSEvent constructor.
91
	 *
92
	 * @param string $type
93
	 * @param bool $local
94
	 */
95
	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...
96
		$this->type = $type;
97
		$this->local = $local;
98
		$this->data = new SimpleDataStore();
99
	}
100
101
102
	/**
103
	 * @return string
104
	 */
105
	public function getType(): string {
106
		return $this->type;
107
	}
108
109
	/**
110
	 * @param mixed $type
111
	 *
112
	 * @return GSEvent
113
	 */
114
	public function setType($type): self {
115
		$this->type = $type;
116
117
		return $this;
118
	}
119
120
121
	/**
122
	 * @return string
123
	 */
124
	public function getSource(): string {
125
		return $this->source;
126
	}
127
128
	/**
129
	 * @param string $source
130
	 *
131
	 * @return GSEvent
132
	 */
133
	public function setSource(string $source): self {
134
		$this->source = $source;
135
136
		if ($this->hasMember() && $this->member->getInstance() === '') {
137
			$this->member->setInstance($source);
138
		}
139
140
		if ($this->hasViewer() && $this->viewer->getInstance() === '') {
141
			$this->viewer->setInstance($source);
142
		}
143
144
		return $this;
145
	}
146
147
148
	/**
149
	 * @return bool
150
	 */
151
	public function isLocal(): bool {
152
		return $this->local;
153
	}
154
155
	/**
156
	 * @param bool $local
157
	 *
158
	 * @return GSEvent
159
	 */
160
	public function setLocal(bool $local): self {
161
		$this->local = $local;
162
163
		return $this;
164
	}
165
166
167
	/**
168
	 * @return Circle
169
	 */
170
	public function getCircle(): Circle {
171
		return $this->circle;
172
	}
173
174
	/**
175
	 * @param Circle $circle
176
	 *
177
	 * @return GSEvent
178
	 */
179
	public function setCircle(Circle $circle): self {
180
		$this->circle = $circle;
181
182
		return $this;
183
	}
184
185
	/**
186
	 * @return bool
187
	 */
188
	public function hasCircle(): bool {
189
		return ($this->circle !== null);
190
	}
191
192
193
	/**
194
	 * @return Member
195
	 */
196
	public function getMember(): Member {
197
		return $this->member;
198
	}
199
200
	/**
201
	 * @param Member $member
202
	 *
203
	 * @return GSEvent
204
	 */
205
	public function setMember(Member $member): self {
206
		$this->member = $member;
207
208
		return $this;
209
	}
210
211
	/**
212
	 * @return bool
213
	 */
214
	public function hasMember(): bool {
215
		return ($this->member !== null);
216
	}
217
218
219
	/**
220
	 * @return Member
221
	 */
222
	public function getViewer(): Member {
223
		return $this->viewer;
224
	}
225
226
	/**
227
	 * @param Member $viewer
228
	 *
229
	 * @return GSEvent
230
	 */
231
	public function setViewer(Member $viewer): self {
232
		$this->viewer = $viewer;
233
234
		return $this;
235
	}
236
237
	/**
238
	 * @return bool
239
	 */
240
	public function hasViewer(): bool {
241
		return ($this->viewer !== null);
242
	}
243
244
245
	/**
246
	 * @param SimpleDataStore $data
247
	 *
248
	 * @return GSEvent
249
	 */
250
	public function setData(SimpleDataStore $data): self {
251
		$this->data = $data;
252
253
		return $this;
254
	}
255
256
	/**
257
	 * @return SimpleDataStore
258
	 */
259
	public function getData(): SimpleDataStore {
260
		return $this->data;
261
	}
262
263
264
	/**
265
	 * @return string
266
	 */
267
	public function getKey(): string {
268
		return $this->key;
269
	}
270
271
	/**
272
	 * @param string $key
273
	 *
274
	 * @return GSEvent
275
	 */
276
	public function setKey(string $key): self {
277
		$this->key = $key;
278
279
		return $this;
280
	}
281
282
283
	/**
284
	 * @return bool
285
	 */
286
	public function isValid(): bool {
287
		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...
288
			return false;
289
		}
290
291
		return true;
292
	}
293
294
295
	/**
296
	 * @param string $json
297
	 *
298
	 * @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...
299
	 * @throws JsonException
300
	 * @throws ModelException
301
	 */
302
	public function importFromJson(string $json): self {
303
		$data = json_decode($json, true);
304
305
		if (!is_array($data)) {
306
			throw new JsonException('invalid JSON');
307
		}
308
309
		return $this->import($data);
310
	}
311
312
313
	/**
314
	 * @param array $data
315
	 *
316
	 * @return GSEvent
317
	 * @throws ModelException
318
	 */
319
	public function import(array $data): self {
320
		$this->setType($this->get('type', $data));
321
		$this->setKey($this->get('key', $data));
322
		$this->setSource($this->get('source', $data));
323
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
324
325
		if (array_key_exists('circle', $data)) {
326
			$this->setCircle(Circle::fromArray($data['circle']));
327
		}
328
		if (array_key_exists('member', $data)) {
329
			$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...
330
		}
331
		if (array_key_exists('viewer', $data)) {
332
			$this->setViewer(Member::fromArray($data['viewer']));
0 ignored issues
show
Bug introduced by
It seems like \OCA\Circles\Model\Membe...mArray($data['viewer']) can be null; however, setViewer() 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...
333
		}
334
335
//		$this->setMember($this->get('member', $data));
336
337
		if (!$this->isValid()) {
338
			throw new ModelException('invalid GSEvent');
339
		}
340
341
		return $this;
342
	}
343
344
345
	/**
346
	 * @return array
347
	 */
348
	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...
349
		$arr = [
350
			'type'   => $this->getType(),
351
			'key'    => $this->getKey(),
352
			'data'   => $this->getData(),
353
			'source' => $this->getSource()
354
		];
355
356
		if ($this->hasCircle()) {
357
			$arr['circle'] = $this->getCircle();
358
		}
359
		if ($this->hasMember()) {
360
			$arr['member'] = $this->getMember();
361
		}
362
		if ($this->hasViewer()) {
363
			$arr['viewer'] = $this->getViewer();
364
		}
365
366
		$this->cleanArray($arr);
367
368
		return $arr;
369
	}
370
371
372
}
373
374