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

GSEvent::setType()   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';
53
	const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
54
	const MEMBER_DELETE = 'GlobalScale\MemberDelete';
55
56
57
	use TArrayTools;
58
59
60
	/** @var string */
61
	private $type = '';
62
63
	/** @var string */
64
	private $source = '';
65
66
	/** @var Circle */
67
	private $circle;
68
69
	/** @var string */
70
	private $circleId = '';
71
72
	/** @var Member */
73
	private $member;
74
75
	/** @var string */
76
	private $memberId = '';
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
		return $this;
132
	}
133
134
135
	/**
136
	 * @return bool
137
	 */
138
	public function isLocal(): bool {
139
		return $this->local;
140
	}
141
142
	/**
143
	 * @param bool $local
144
	 *
145
	 * @return GSEvent
146
	 */
147
	public function setLocal(bool $local): self {
148
		$this->local = $local;
149
150
		return $this;
151
	}
152
153
154
	/**
155
	 * @return Circle
156
	 */
157
	public function getCircle(): Circle {
158
		return $this->circle;
159
	}
160
161
	/**
162
	 * @param Circle $circle
163
	 *
164
	 * @return GSEvent
165
	 */
166
	public function setCircle(Circle $circle): self {
167
		$this->circle = $circle;
168
169
		return $this;
170
	}
171
172
	/**
173
	 * @return bool
174
	 */
175
	public function hasCircle(): bool {
176
		return ($this->circle !== null);
177
	}
178
179
180
	/**
181
	 * @return Member
182
	 */
183
	public function getMember(): Member {
184
		return $this->member;
185
	}
186
187
	/**
188
	 * @param Member $member
189
	 *
190
	 * @return GSEvent
191
	 */
192
	public function setMember(Member $member): self {
193
		$this->member = $member;
194
195
		return $this;
196
	}
197
198
	/**
199
	 * @return bool
200
	 */
201
	public function hasMember(): bool {
202
		return ($this->member !== null);
203
	}
204
205
206
	/**
207
	 * @return string
208
	 */
209
	public function getCircleId(): string {
210
		return $this->circleId;
211
	}
212
213
	/**
214
	 * @param string $circleId
215
	 *
216
	 * @return GSEvent
217
	 */
218
	public function setCircleId(string $circleId): self {
219
		$this->circleId = $circleId;
220
221
		return $this;
222
	}
223
224
225
	/**
226
	 * @return string
227
	 */
228
	public function getMemberId(): string {
229
		return $this->memberId;
230
	}
231
232
	/**
233
	 * @param string $memberId
234
	 *
235
	 * @return GSEvent
236
	 */
237
	public function setMemberId(string $memberId): self {
238
		$this->memberId = $memberId;
239
240
		return $this;
241
	}
242
243
244
	/**
245
	 * @return string
246
	 */
247
	public function getKey(): string {
248
		return $this->key;
249
	}
250
251
	/**
252
	 * @param string $key
253
	 *
254
	 * @return GSEvent
255
	 */
256
	public function setKey(string $key): self {
257
		$this->key = $key;
258
259
		return $this;
260
	}
261
262
263
	/**
264
	 * @return bool
265
	 */
266
	public function isValid(): bool {
267
		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...
268
			return false;
269
		}
270
271
		return true;
272
	}
273
274
275
	/**
276
	 * @param string $json
277
	 *
278
	 * @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...
279
	 * @throws JsonException
280
	 * @throws ModelException
281
	 */
282
	public function importFromJson(string $json): self {
283
		$data = json_decode($json, true);
284
285
		if (!is_array($data)) {
286
			throw new JsonException('invalid JSON');
287
		}
288
289
		return $this->import($data);
290
	}
291
292
293
	/**
294
	 * @param array $data
295
	 *
296
	 * @return GSEvent
297
	 * @throws ModelException
298
	 */
299
	public function import(array $data): self {
300
		$this->setType($this->get('type', $data));
301
		$this->setKey($this->get('key', $data));
302
		$this->setSource($this->get('source', $data));
303
304
		if (array_key_exists('circle', $data)) {
305
			$this->setCircle(Circle::fromArray($data['circle']));
306
		}
307
		if (array_key_exists('member', $data)) {
308
			$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...
309
		}
310
311
//		$this->setMember($this->get('member', $data));
312
313
		if (!$this->isValid()) {
314
			throw new ModelException('invalid GSEvent');
315
		}
316
317
		return $this;
318
	}
319
320
321
	/**
322
	 * @return array
323
	 */
324
	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...
325
		$arr = [
326
			'type'   => $this->getType(),
327
			'key'    => $this->getKey(),
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