Completed
Push — federated-circles ( 1e2c73...673aab )
by Maxence
02:33
created

SharingFrame::fromJSON()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 9
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Model;
28
29
class SharingFrame implements \JsonSerializable {
30
31
	/** @var string */
32
	private $source;
33
34
	/** @var string */
35
	private $type;
36
37
	/** @var int */
38
	private $circleId;
39
40
	/** @var string */
41
	private $circleName;
42
43
	/** @var string */
44
	private $author;
45
46
	/** @var string */
47
	private $cloudId;
48
49
	/** @var array */
50
	private $payload;
51
52
	/** @var array */
53
	private $headers;
54
55
	/** @var int */
56
	private $creation;
57
58
	/** @var string */
59
	private $uniqueId;
60
61
	public function __construct(string $source, string $type) {
62
		$this->source = $source;
63
		$this->type = $type;
64
	}
65
66
67
	/**
68
	 * @return string
69
	 */
70
	public function getSource() {
71
		return $this->source;
72
	}
73
74
	/**
75
	 * @return string
76
	 */
77
	public function getType() {
78
		return $this->type;
79
	}
80
81
	/**
82
	 * @param int $circleId
83
	 */
84
	public function setCircleId(int $circleId) {
85
		$this->circleId = $circleId;
86
	}
87
88
	/**
89
	 * @return int
90
	 */
91
	public function getCircleId() {
92
		return $this->circleId;
93
	}
94
95
96
	/**
97
	 * @param string $circleName
98
	 */
99
	public function setCircleName($circleName) {
100
		$this->circleName = $circleName;
101
	}
102
103
	/**
104
	 * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
105
	 */
106
	public function getCircleName() {
107
		return $this->circleName;
108
	}
109
110
111
	/**
112
	 * @param string $author
113
	 */
114
	public function setAuthor(string $author) {
115
		$this->author = $author;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function getAuthor() {
122
		return $this->author;
123
	}
124
125
126
	/**
127
	 * @param string $cloudId
128
	 */
129
	public function setCloudId($cloudId) {
130
		$this->cloudId = $cloudId;
131
	}
132
133
	/**
134
	 * @return string
135
	 */
136
	public function getCloudId() {
137
		return $this->cloudId;
138
	}
139
140
141
	/**
142
	 * @param string $uniqueId
143
	 *
144
	 * @return SharingFrame
145
	 */
146
	public function setUniqueId(string $uniqueId) {
147
		$this->uniqueId = $uniqueId;
148
149
		return $this;
150
	}
151
152
	/**
153
	 * @return string
154
	 */
155
	public function getUniqueId() {
156
		return $this->uniqueId;
157
	}
158
159
	/**
160
	 * @return SharingFrame
161
	 */
162
	public function generateUniqueId() {
163
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(16));
164
		$this->setUniqueId($uniqueId);
165
166
		return $this;
167
	}
168
169
	/**
170
	 * @param array $payload
171
	 */
172
	public function setPayload($payload) {
173
		$this->payload = $payload;
174
	}
175
176
	/**
177
	 * @param bool $asJson
178
	 *
179
	 * @return array|string
180
	 */
181
	public function getPayload(bool $asJson = false) {
182
		if ($asJson) {
183
			return json_encode($this->payload);
184
		}
185
186
		return $this->payload;
187
	}
188
189
190
	/**
191
	 * @param array $headers
192
	 */
193
	public function setHeaders($headers) {
194
		$this->headers = $headers;
195
	}
196
197
	/**
198
	 * @param bool $asJson
199
	 *
200
	 * @return array|string
201
	 */
202
	public function getHeaders(bool $asJson = false) {
203
		if ($asJson) {
204
			return json_encode($this->headers);
205
		}
206
207
		return $this->headers;
208
	}
209
210
211
	/**
212
	 * @param $k
213
	 *
214
	 * @return string
215
	 */
216
	public function getHeader(string $k) {
217
		if ($this->headers === null) {
218
			return null;
219
		}
220
221
		if (!key_exists($k, $this->headers)) {
222
			return null;
223
		}
224
225
		return $this->headers[$k];
226
	}
227
228
	/**
229
	 * @param string $k
230
	 * @param string $v
231
	 */
232
	public function setHeader(string $k, $v) {
233
		$this->headers[$k] = $v;
234
	}
235
236
237
	/**
238
	 * @param int $creation
239
	 */
240
	public function setCreation($creation) {
241
		if ($creation === null) {
242
			return;
243
		}
244
245
		$this->creation = $creation;
246
	}
247
248
	/**
249
	 * @return int
250
	 */
251
	public function getCreation() {
252
		return $this->creation;
253
	}
254
255
256
	public function jsonSerialize() {
257
		return array(
258
			'circle_id'   => $this->getCircleId(),
259
			'circle_name' => $this->getCircleName(),
260
			'unique_id'   => $this->getUniqueId(),
261
			'source'      => $this->getSource(),
262
			'type'        => $this->getType(),
263
			'author'      => $this->getAuthor(),
264
			'cloud_id'    => $this->getCloudId(),
265
			'headers'     => $this->getHeaders(),
266
			'payload'     => $this->getPayload(),
267
			'creation'    => $this->getCreation(),
268
		);
269
	}
270
271
	public static function fromJSON($json) {
272
273
		$arr = json_decode($json, true);
274
		if (!key_exists('source', $arr)) {
275
			return null;
276
		}
277
278
		$share = new SharingFrame($arr['source'], $arr['type']);
279
		$share->setCircleId($arr['circle_id']);
280
		if (key_exists('circle_name', $arr)) {
281
			$share->setCircleName($arr['circle_name']);
282
		}
283
284
		if (key_exists('headers', $arr)) {
285
			$share->setHeaders($arr['headers']);
286
		}
287
288
		if (key_exists('cloud_id', $arr)) {
289
			$share->setCloudID($arr['cloud_id']);
290
		}
291
292
		$share->setUniqueId($arr['unique_id']);
293
		$share->setAuthor($arr['author']);
294
		$share->setPayload($arr['payload']);
295
		$share->setCreation($arr['creation']);
296
297
		return $share;
298
	}
299
300
}