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

GSWrapper::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
38
39
/**
40
 * Class GSEvent
41
 *
42
 * @package OCA\Circles\Model\GlobalScale
43
 */
44
class GSWrapper implements JsonSerializable {
45
46
47
	const TYPE_CIRCLE_CREATION = 'circles_create';
48
49
50
	use TArrayTools;
51
52
53
	/** @var string */
54
	private $token = '';
55
56
	/** @var GSEvent */
57
	private $event;
58
59
	/** @var array */
60
	private $instances = [];
61
62
	/** @var int */
63
	private $creation;
64
65
66
	function __construct() {
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...
67
	}
68
69
70
	/**
71
	 * @return string
72
	 */
73
	public function getToken(): string {
74
		return $this->token;
75
	}
76
77
	/**
78
	 * @param string $token
79
	 *
80
	 * @return GSWrapper
81
	 */
82
	public function setToken(string $token): self {
83
		$this->token = $token;
84
85
		return $this;
86
	}
87
88
89
	/**
90
	 * @return GSEvent
91
	 */
92
	public function getEvent(): GSEvent {
93
		return $this->event;
94
	}
95
96
	/**
97
	 * @param GSEvent $event
98
	 *
99
	 * @return GSWrapper
100
	 */
101
	public function setEvent(GSEvent $event): self {
102
		$this->event = $event;
103
104
		return $this;
105
	}
106
107
	/**
108
	 * @return bool
109
	 */
110
	public function hasEvent(): bool {
111
		return ($this->event !== null);
112
	}
113
114
115
	/**
116
	 * @return array
117
	 */
118
	public function getInstances(): array {
119
		return $this->instances;
120
	}
121
122
	/**
123
	 * @param array $instances
124
	 *
125
	 * @return GSWrapper
126
	 */
127
	public function setInstances(array $instances): self {
128
		$this->instances = $instances;
129
130
		return $this;
131
	}
132
133
134
	/**
135
	 * @return int
136
	 */
137
	public function getCreation(): int {
138
		return $this->creation;
139
	}
140
141
	/**
142
	 * @param int $creation
143
	 *
144
	 * @return GSWrapper
145
	 */
146
	public function setCreation(int $creation): self {
147
		$this->creation = $creation;
148
149
		return $this;
150
	}
151
152
153
	/**
154
	 * @param array $data
155
	 *
156
	 * @return GSWrapper
157
	 * @throws JsonException
158
	 * @throws ModelException
159
	 */
160
	public function import(array $data): self {
161
		$this->setToken($this->get('id', $data));
162
163
		$event = new GSEvent();
164
		$event->importFromJson($this->get('event', $data));
165
166
		$this->setEvent($event);
167
168
		$this->setInstances($this->getArray('instances', $data));
169
		$this->setCreation($this->getInt('creation', $data));
170
171
		return $this;
172
	}
173
174
175
	/**
176
	 * @return array
177
	 */
178
	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...
179
		$arr = [
180
			'id'        => $this->getToken(),
181
			'event'     => $this->getEvent(),
182
			'instances' => $this->getInstances(),
183
			'creation'  => $this->getCreation()
184
		];
185
186
		$this->cleanArray($arr);
187
188
		return $arr;
189
	}
190
191
}
192
193