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

GSEvent::setKey()   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 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 TYPE_CIRCLE_CREATION = 'GlobalScale\CircleCreation';
50
51
52
	use TArrayTools;
53
54
55
	/** @var string */
56
	private $type = '';
57
58
	/** @var Circle */
59
	private $circle;
60
61
	/** @var string */
62
	private $circleId = '';
63
64
	/** @var Member */
65
	private $member = '';
66
67
	/** @var string */
68
	private $memberId = '';
69
70
	/** @var string */
71
	private $key = '';
72
73
	/** @var bool */
74
	private $local = false;
75
76
77
	/**
78
	 * GSEvent constructor.
79
	 *
80
	 * @param string $type
81
	 * @param bool $local
82
	 */
83
	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...
84
		$this->type = $type;
85
		$this->local = $local;
86
	}
87
88
89
	/**
90
	 * @return string
91
	 */
92
	public function getType(): string {
93
		return $this->type;
94
	}
95
96
	/**
97
	 * @param mixed $type
98
	 *
99
	 * @return GSEvent
100
	 */
101
	public function setType($type): self {
102
		$this->type = $type;
103
104
		return $this;
105
	}
106
107
108
	/**
109
	 * @return bool
110
	 */
111
	public function isLocal(): bool {
112
		return $this->local;
113
	}
114
115
	/**
116
	 * @param bool $local
117
	 *
118
	 * @return GSEvent
119
	 */
120
	public function setLocal(bool $local): self {
121
		$this->local = $local;
122
123
		return $this;
124
	}
125
126
127
	/**
128
	 * @return Circle
129
	 */
130
	public function getCircle(): Circle {
131
		return $this->circle;
132
	}
133
134
	/**
135
	 * @param Circle $circle
136
	 */
137
	public function setCircle(Circle $circle): void {
138
		$this->circle = $circle;
139
	}
140
141
	/**
142
	 * @return bool
143
	 */
144
	public function hasCircle(): bool {
145
		return ($this->circle !== null);
146
	}
147
148
149
	/**
150
	 * @return string
151
	 */
152
	public function getCircleId(): string {
153
		return $this->circleId;
154
	}
155
156
	/**
157
	 * @param string $circleId
158
	 *
159
	 * @return GSEvent
160
	 */
161
	public function setCircleId(string $circleId): self {
162
		$this->circleId = $circleId;
163
164
		return $this;
165
	}
166
167
168
	/**
169
	 * @return string
170
	 */
171
	public function getMemberId(): string {
172
		return $this->memberId;
173
	}
174
175
	/**
176
	 * @param string $memberId
177
	 *
178
	 * @return GSEvent
179
	 */
180
	public function setMemberId(string $memberId): self {
181
		$this->memberId = $memberId;
182
183
		return $this;
184
	}
185
186
187
	/**
188
	 * @return string
189
	 */
190
	public function getKey(): string {
191
		return $this->key;
192
	}
193
194
	/**
195
	 * @param string $key
196
	 */
197
	public function setKey(string $key): void {
198
		$this->key = $key;
199
	}
200
201
202
	/**
203
	 * @return bool
204
	 */
205
	public function isValid(): bool {
206
		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...
207
			return false;
208
		}
209
210
		return true;
211
	}
212
213
214
	/**
215
	 * @param string $json
216
	 *
217
	 * @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...
218
	 * @throws JsonException
219
	 * @throws ModelException
220
	 */
221
	public function importFromJson(string $json): self {
222
		$data = json_decode($json, true);
223
224
		if (!is_array($data)) {
225
			throw new JsonException('invalid JSON');
226
		}
227
228
		return $this->import($data);
229
	}
230
231
232
	/**
233
	 * @param array $data
234
	 *
235
	 * @return GSEvent
236
	 * @throws ModelException
237
	 */
238
	public function import(array $data): self {
239
		$this->setType($this->get('type', $data));
240
		$this->setKey($this->get('key', $data));
241
242
		if (array_key_exists('circle', $data)) {
243
			$this->setCircle(Circle::fromArray($data['circle']));
244
		}
245
246
//		$this->setMember($this->get('member', $data));
247
248
		if (!$this->isValid()) {
249
			throw new ModelException('invalid GSEvent');
250
		}
251
252
		return $this;
253
	}
254
255
256
	/**
257
	 * @return array
258
	 */
259
	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...
260
		$arr = [
261
			'type'   => $this->getType(),
262
			'circle' => $this->getCircle(),
263
			'member' => $this->getMemberId(),
264
			'key'    => $this->getKey()
265
		];
266
267
		$this->cleanArray($arr);
268
269
		return $arr;
270
	}
271
272
}
273
274