Completed
Pull Request — master (#362)
by Maxence
02:10 queued 11s
created

GSEvent::setMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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