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

GSEvent::setCircle()   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\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 GLOBAL_SYNC = 'GlobalScale\GlobalSync';
51
	const CIRCLE_STATUS = 'GlobalScale\CircleStatus';
52
53
	const CIRCLE_CREATE = 'GlobalScale\CircleCreate';
54
	const CIRCLE_UPDATE = 'GlobalScale\CircleUpdate';
55
	const CIRCLE_DELETE = 'GlobalScale\CircleDelete';
56
	const MEMBER_CREATE = 'GlobalScale\MemberCreate'; // used ?
57
	const MEMBER_ADD = 'GlobalScale\MemberAdd';
58
	const MEMBER_JOIN = 'GlobalScale\MemberJoin';
59
	const MEMBER_INVITE = 'GlobalScale\MemberInvite';
60
	const MEMBER_LEAVE = 'GlobalScale\MemberLeave';
61
	const MEMBER_LEVEL = 'GlobalScale\MemberLevel';
62
	const MEMBER_UPDATE = 'GlobalScale\MemberUpdate';
63
	const MEMBER_REMOVE = 'GlobalScale\MemberRemove';
64
65
66
	use TArrayTools;
67
68
69
	/** @var string */
70
	private $type = '';
71
72
	/** @var string */
73
	private $source = '';
74
75
	/** @var Circle */
76
	private $circle;
77
78
	/** @var Member */
79
	private $member;
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->hasCircle()
142
			&& $this->getCircle()
143
					->hasViewer()
144
			&& $this->getCircle()
145
					->getViewer()
146
					->getInstance() === '') {
147
			$this->getCircle()
148
				 ->getViewer()
149
				 ->setInstance($source);
150
		}
151
152
		return $this;
153
	}
154
155
156
	/**
157
	 * @return bool
158
	 */
159
	public function isLocal(): bool {
160
		return $this->local;
161
	}
162
163
	/**
164
	 * @param bool $local
165
	 *
166
	 * @return GSEvent
167
	 */
168
	public function setLocal(bool $local): self {
169
		$this->local = $local;
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * @return Circle
177
	 */
178
	public function getCircle(): Circle {
179
		return $this->circle;
180
	}
181
182
	/**
183
	 * @param Circle $circle
184
	 *
185
	 * @return GSEvent
186
	 */
187
	public function setCircle(Circle $circle): self {
188
		$this->circle = $circle;
189
190
		return $this;
191
	}
192
193
	/**
194
	 * @return bool
195
	 */
196
	public function hasCircle(): bool {
197
		return ($this->circle !== null);
198
	}
199
200
201
	/**
202
	 * @return Member
203
	 */
204
	public function getMember(): Member {
205
		return $this->member;
206
	}
207
208
	/**
209
	 * @param Member $member
210
	 *
211
	 * @return GSEvent
212
	 */
213
	public function setMember(Member $member): self {
214
		$this->member = $member;
215
216
		return $this;
217
	}
218
219
	/**
220
	 * @return bool
221
	 */
222
	public function hasMember(): bool {
223
		return ($this->member !== null);
224
	}
225
226
227
	/**
228
	 * @param SimpleDataStore $data
229
	 *
230
	 * @return GSEvent
231
	 */
232
	public function setData(SimpleDataStore $data): self {
233
		$this->data = $data;
234
235
		return $this;
236
	}
237
238
	/**
239
	 * @return SimpleDataStore
240
	 */
241
	public function getData(): SimpleDataStore {
242
		return $this->data;
243
	}
244
245
246
	/**
247
	 * @return string
248
	 */
249
	public function getKey(): string {
250
		return $this->key;
251
	}
252
253
	/**
254
	 * @param string $key
255
	 *
256
	 * @return GSEvent
257
	 */
258
	public function setKey(string $key): self {
259
		$this->key = $key;
260
261
		return $this;
262
	}
263
264
265
	/**
266
	 * @return bool
267
	 */
268
	public function isValid(): bool {
269
		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...
270
			return false;
271
		}
272
273
		return true;
274
	}
275
276
277
	/**
278
	 * @param string $json
279
	 *
280
	 * @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...
281
	 * @throws JsonException
282
	 * @throws ModelException
283
	 */
284
	public function importFromJson(string $json): self {
285
		$data = json_decode($json, true);
286
287
		if (!is_array($data)) {
288
			throw new JsonException('invalid JSON');
289
		}
290
291
		return $this->import($data);
292
	}
293
294
295
	/**
296
	 * @param array $data
297
	 *
298
	 * @return GSEvent
299
	 * @throws ModelException
300
	 */
301
	public function import(array $data): self {
302
		$this->setType($this->get('type', $data));
303
		$this->setKey($this->get('key', $data));
304
		$this->setSource($this->get('source', $data));
305
		$this->setData(new SimpleDataStore($this->getArray('data', $data)));
306
307
		if (array_key_exists('circle', $data)) {
308
			$this->setCircle(Circle::fromArray($data['circle']));
309
		}
310
311
		if (array_key_exists('member', $data)) {
312
			$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...
313
		}
314
315
		if (!$this->isValid()) {
316
			throw new ModelException('invalid GSEvent');
317
		}
318
319
		return $this;
320
	}
321
322
323
	/**
324
	 * @return array
325
	 */
326
	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...
327
		$arr = [
328
			'type'   => $this->getType(),
329
			'key'    => $this->getKey(),
330
			'data'   => $this->getData(),
331
			'source' => $this->getSource()
332
		];
333
334
		if ($this->hasCircle()) {
335
			$arr['circle'] = $this->getCircle();
336
		}
337
		if ($this->hasMember()) {
338
			$arr['member'] = $this->getMember();
339
		}
340
341
		$this->cleanArray($arr);
342
343
		return $arr;
344
	}
345
346
347
}
348
349