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

GSWrapper::import()   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 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
38
39
/**
40
 * Class GSEvent
41
 *
42
 * @package OCA\Circles\Model\GlobalScale
43
 */
44
class GSWrapper implements JsonSerializable {
45
46
47
	use TArrayTools;
48
49
50
	/** @var string */
51
	private $token = '';
52
53
	/** @var GSEvent */
54
	private $event;
55
56
	/** @var int */
57
	private $creation;
58
59
60
	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...
61
	}
62
63
64
	/**
65
	 * @return string
66
	 */
67
	public function getToken(): string {
68
		return $this->token;
69
	}
70
71
	/**
72
	 * @param string $token
73
	 *
74
	 * @return GSWrapper
75
	 */
76
	public function setToken(string $token): self {
77
		$this->token = $token;
78
79
		return $this;
80
	}
81
82
83
	/**
84
	 * @return GSEvent
85
	 */
86
	public function getEvent(): GSEvent {
87
		return $this->event;
88
	}
89
90
	/**
91
	 * @param GSEvent $event
92
	 *
93
	 * @return GSWrapper
94
	 */
95
	public function setEvent(GSEvent $event): self {
96
		$this->event = $event;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * @return bool
103
	 */
104
	public function hasEvent(): bool {
105
		return ($this->event !== null);
106
	}
107
108
109
	/**
110
	 * @return int
111
	 */
112
	public function getCreation(): int {
113
		return $this->creation;
114
	}
115
116
	/**
117
	 * @param int $creation
118
	 *
119
	 * @return GSWrapper
120
	 */
121
	public function setCreation(int $creation): self {
122
		$this->creation = $creation;
123
124
		return $this;
125
	}
126
127
128
	/**
129
	 * @param array $data
130
	 *
131
	 * @return GSWrapper
132
	 * @throws JsonException
133
	 * @throws ModelException
134
	 */
135
	public function import(array $data): self {
136
		$this->setToken($this->get('id', $data));
137
138
		$event = new GSEvent();
139
		$event->importFromJson($this->get('event', $data));
140
141
		$this->setEvent($event);
142
143
		$this->setCreation($this->getInt('creation', $data));
144
145
		return $this;
146
	}
147
148
149
	/**
150
	 * @return array
151
	 */
152
	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...
153
		$arr = [
154
			'id'       => $this->getToken(),
155
			'event'    => $this->getEvent(),
156
			'creation' => $this->getCreation()
157
		];
158
159
		$this->cleanArray($arr);
160
161
		return $arr;
162
	}
163
164
}
165
166