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

GSEvent   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 33
lcom 1
cbo 5
dl 0
loc 292
rs 9.76
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getType() 0 3 1
A setType() 0 5 1
A getSource() 0 3 1
A setSource() 0 13 5
A isLocal() 0 3 1
A setLocal() 0 5 1
A getCircle() 0 3 1
A setCircle() 0 5 1
A hasCircle() 0 3 1
A getMember() 0 3 1
A setMember() 0 5 1
A hasMember() 0 3 1
A getViewer() 0 3 1
A setViewer() 0 5 1
A hasViewer() 0 3 1
A getKey() 0 3 1
A setKey() 0 5 1
A isValid() 0 7 2
A importFromJson() 0 9 2
A import() 0 20 4
A jsonSerialize() 0 18 3
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\Traits\TArrayTools;
34
use JsonSerializable;
35
use OCA\Circles\Exceptions\JsonException;
36
use OCA\Circles\Exceptions\ModelException;
37
use OCA\Circles\Model\Circle;
38
use OCA\Circles\Model\Member;
39
40
41
/**
42
 * Class GSEvent
43
 *
44
 * @package OCA\Circles\Model\GlobalScale
45
 */
46
class GSEvent implements JsonSerializable {
47
48
49
	const CIRCLE_CREATE = 'GlobalScale\CircleCreate';
50
	const CIRCLE_UPDATE = 'GlobalScale\CircleUpdate';
51
	const CIRCLE_DELETE = 'GlobalScale\CircleDelete';
52
	const MEMBER_CREATE = 'GlobalScale\MemberCreate';
53
	const MEMBER_JOIN = 'GlobalScale\MemberJoin';
54
	const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
55
	const MEMBER_DELETE = 'GlobalScale\MemberDelete';
56
57
58
	use TArrayTools;
59
60
61
	/** @var string */
62
	private $type = '';
63
64
	/** @var string */
65
	private $source = '';
66
67
	/** @var Circle */
68
	private $circle;
69
70
	/** @var Member */
71
	private $member;
72
73
	/** @var Member */
74
	private $viewer;
75
76
	/** @var string */
77
	private $key = '';
78
79
	/** @var bool */
80
	private $local = false;
81
82
83
	/**
84
	 * GSEvent constructor.
85
	 *
86
	 * @param string $type
87
	 * @param bool $local
88
	 */
89
	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...
90
		$this->type = $type;
91
		$this->local = $local;
92
	}
93
94
95
	/**
96
	 * @return string
97
	 */
98
	public function getType(): string {
99
		return $this->type;
100
	}
101
102
	/**
103
	 * @param mixed $type
104
	 *
105
	 * @return GSEvent
106
	 */
107
	public function setType($type): self {
108
		$this->type = $type;
109
110
		return $this;
111
	}
112
113
114
	/**
115
	 * @return string
116
	 */
117
	public function getSource(): string {
118
		return $this->source;
119
	}
120
121
	/**
122
	 * @param string $source
123
	 *
124
	 * @return GSEvent
125
	 */
126
	public function setSource(string $source): self {
127
		$this->source = $source;
128
129
		if ($this->hasMember() && $this->member->getInstance() === '') {
130
			$this->member->setInstance($source);
131
		}
132
133
		if ($this->hasViewer() && $this->viewer->getInstance() === '') {
134
			$this->viewer->setInstance($source);
135
		}
136
137
		return $this;
138
	}
139
140
141
	/**
142
	 * @return bool
143
	 */
144
	public function isLocal(): bool {
145
		return $this->local;
146
	}
147
148
	/**
149
	 * @param bool $local
150
	 *
151
	 * @return GSEvent
152
	 */
153
	public function setLocal(bool $local): self {
154
		$this->local = $local;
155
156
		return $this;
157
	}
158
159
160
	/**
161
	 * @return Circle
162
	 */
163
	public function getCircle(): Circle {
164
		return $this->circle;
165
	}
166
167
	/**
168
	 * @param Circle $circle
169
	 *
170
	 * @return GSEvent
171
	 */
172
	public function setCircle(Circle $circle): self {
173
		$this->circle = $circle;
174
175
		return $this;
176
	}
177
178
	/**
179
	 * @return bool
180
	 */
181
	public function hasCircle(): bool {
182
		return ($this->circle !== null);
183
	}
184
185
186
	/**
187
	 * @return Member
188
	 */
189
	public function getMember(): Member {
190
		return $this->member;
191
	}
192
193
	/**
194
	 * @param Member $member
195
	 *
196
	 * @return GSEvent
197
	 */
198
	public function setMember(Member $member): self {
199
		$this->member = $member;
200
201
		return $this;
202
	}
203
204
	/**
205
	 * @return bool
206
	 */
207
	public function hasMember(): bool {
208
		return ($this->member !== null);
209
	}
210
211
212
	/**
213
	 * @return Member
214
	 */
215
	public function getViewer(): Member {
216
		return $this->viewer;
217
	}
218
219
	/**
220
	 * @param Member $viewer
221
	 *
222
	 * @return GSEvent
223
	 */
224
	public function setViewer(Member $viewer): self {
225
		$this->viewer = $viewer;
226
227
		return $this;
228
	}
229
230
	/**
231
	 * @return bool
232
	 */
233
	public function hasViewer(): bool {
234
		return ($this->viewer !== null);
235
	}
236
237
238
	/**
239
	 * @return string
240
	 */
241
	public function getKey(): string {
242
		return $this->key;
243
	}
244
245
	/**
246
	 * @param string $key
247
	 *
248
	 * @return GSEvent
249
	 */
250
	public function setKey(string $key): self {
251
		$this->key = $key;
252
253
		return $this;
254
	}
255
256
257
	/**
258
	 * @return bool
259
	 */
260
	public function isValid(): bool {
261
		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...
262
			return false;
263
		}
264
265
		return true;
266
	}
267
268
269
	/**
270
	 * @param string $json
271
	 *
272
	 * @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...
273
	 * @throws JsonException
274
	 * @throws ModelException
275
	 */
276
	public function importFromJson(string $json): self {
277
		$data = json_decode($json, true);
278
279
		if (!is_array($data)) {
280
			throw new JsonException('invalid JSON');
281
		}
282
283
		return $this->import($data);
284
	}
285
286
287
	/**
288
	 * @param array $data
289
	 *
290
	 * @return GSEvent
291
	 * @throws ModelException
292
	 */
293
	public function import(array $data): self {
294
		$this->setType($this->get('type', $data));
295
		$this->setKey($this->get('key', $data));
296
		$this->setSource($this->get('source', $data));
297
298
		if (array_key_exists('circle', $data)) {
299
			$this->setCircle(Circle::fromArray($data['circle']));
300
		}
301
		if (array_key_exists('member', $data)) {
302
			$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...
303
		}
304
305
//		$this->setMember($this->get('member', $data));
306
307
		if (!$this->isValid()) {
308
			throw new ModelException('invalid GSEvent');
309
		}
310
311
		return $this;
312
	}
313
314
315
	/**
316
	 * @return array
317
	 */
318
	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...
319
		$arr = [
320
			'type'   => $this->getType(),
321
			'key'    => $this->getKey(),
322
			'source' => $this->getSource()
323
		];
324
325
		if ($this->hasCircle()) {
326
			$arr['circle'] = $this->getCircle();
327
		}
328
		if ($this->hasMember()) {
329
			$arr['member'] = $this->getMember();
330
		}
331
332
		$this->cleanArray($arr);
333
334
		return $arr;
335
	}
336
337
}
338
339