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

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