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

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