Completed
Push — master ( 4a1ba1...57797d )
by Maxence
02:13
created

SharingFrame::getCircleFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
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
use OCA\Circles\AppInfo\Application;
30
use OCA\Circles\Exceptions\SharingFrameSourceCannotBeAppCirclesException;
31
use OCA\Circles\Service\MiscService;
32
33
class SharingFrame implements \JsonSerializable {
34
35
	/** @var string */
36
	private $source;
37
38
	/** @var string */
39
	private $type;
40
41
	/** @var Circle */
42
	private $circle;
43
44
	/** @var string */
45
	private $author;
46
47
	/** @var string */
48
	private $cloudId;
49
50
	/** @var array */
51
	private $payload;
52
53
	/** @var array */
54
	private $headers;
55
56
	/** @var int */
57
	private $creation;
58
59
	/** @var string */
60
	private $uniqueId;
61
62
	public function __construct($source, $type) {
63
		$this->source = (string)$source;
64
		$this->type = (string)$type;
65
	}
66
67
68
	/**
69
	 * @return string
70
	 */
71
	public function getSource() {
72
		return $this->source;
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	public function getType() {
79
		return $this->type;
80
	}
81
82
	/**
83
	 * @param Circle $circle
84
	 */
85
	public function setCircle($circle) {
86
		$this->circle = $circle;
87
	}
88
89
	/**
90
	 * @return Circle
91
	 */
92
	public function getCircle() {
93
		return $this->circle;
94
	}
95
96
97
	/**
98
	 * @param string $author
99
	 */
100
	public function setAuthor($author) {
101
		$this->author = (string)$author;
102
	}
103
104
	/**
105
	 * @return string
106
	 */
107
	public function getAuthor() {
108
		return $this->author;
109
	}
110
111
112
	/**
113
	 * @param string $cloudId
114
	 */
115
	public function setCloudId($cloudId) {
116
		$this->cloudId = $cloudId;
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122
	public function getCloudId() {
123
		return $this->cloudId;
124
	}
125
126
127
	/**
128
	 * @param string $uniqueId
129
	 *
130
	 * @return SharingFrame
131
	 */
132
	public function setUniqueId($uniqueId) {
133
		$this->uniqueId = (string)$uniqueId;
134
135
		return $this;
136
	}
137
138
	/**
139
	 * @return string
140
	 */
141
	public function getUniqueId() {
142
		return $this->uniqueId;
143
	}
144
145
	/**
146
	 * @return SharingFrame
147
	 */
148
	public function generateUniqueId() {
149
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(16));
150
		$this->setUniqueId($uniqueId);
151
152
		return $this;
153
	}
154
155
	/**
156
	 * @param array $payload
157
	 */
158
	public function setPayload($payload) {
159
		$this->payload = $payload;
160
	}
161
162
	/**
163
	 * @param bool $asJson
164
	 *
165
	 * @return array|string
166
	 */
167
	public function getPayload($asJson = false) {
168
		if ($asJson) {
169
			return json_encode($this->payload);
170
		}
171
172
		return $this->payload;
173
	}
174
175
176
	/**
177
	 * @param array $headers
178
	 */
179
	public function setHeaders($headers) {
180
		$this->headers = $headers;
181
	}
182
183
	/**
184
	 * @param bool $asJson
185
	 *
186
	 * @return array|string
187
	 */
188
	public function getHeaders($asJson = false) {
189
		if ($asJson) {
190
			return json_encode($this->headers);
191
		}
192
193
		return $this->headers;
194
	}
195
196
197
	/**
198
	 * @param string $k
199
	 *
200
	 * @return string
201
	 */
202
	public function getHeader($k) {
203
		if ($this->headers === null) {
204
			return null;
205
		}
206
207
		$k = (string)$k;
208
		if (!key_exists($k, $this->headers)) {
209
			return null;
210
		}
211
212
		return $this->headers[$k];
213
	}
214
215
	/**
216
	 * @param string $k
217
	 * @param string $v
218
	 */
219
	public function setHeader($k, $v) {
220
		$this->headers[(string)$k] = $v;
221
	}
222
223
224
	/**
225
	 * @param int $creation
226
	 */
227
	public function setCreation($creation) {
228
		if ($creation === null) {
229
			return;
230
		}
231
232
		$this->creation = $creation;
233
	}
234
235
	/**
236
	 * @return int
237
	 */
238
	public function getCreation() {
239
		return $this->creation;
240
	}
241
242
243
	/**
244
	 * @return bool
245
	 */
246
	public function isLocal() {
247
		return ($this->getCloudId() === null);
248
	}
249
250
251
	/**
252
	 * @return bool
253
	 */
254
	public function is0Circle() {
255
		return ($this->getCloudId() === null);
256
	}
257
258
	/**
259
	 * @throws SharingFrameSourceCannotBeAppCirclesException
260
	 */
261
	public function cannotBeFromCircles() {
262
		if (strtolower($this->getSource()) === Application::APP_NAME) {
263
			throw new SharingFrameSourceCannotBeAppCirclesException();
264
		}
265
	}
266
267
268
	public function jsonSerialize() {
269
		return array(
270
			'unique_id' => $this->getUniqueId(),
271
			'circle'    => $this->getCircle()
272
								->getArray(false, true),
273
			'source'    => $this->getSource(),
274
			'type'      => $this->getType(),
275
			'author'    => $this->getAuthor(),
276
			'cloud_id'  => $this->getCloudId(),
277
			'headers'   => $this->getHeaders(),
278
			'payload'   => $this->getPayload(),
279
			'creation'  => $this->getCreation(),
280
		);
281
	}
282
283
	public static function fromJSON($json) {
284
285
		$arr = json_decode($json, true);
286
		if (!is_array($arr) || !key_exists('source', $arr)) {
287
			return null;
288
		}
289
290
		$share = new SharingFrame($arr['source'], $arr['type']);
291
		$share->setCircle(self::getCircleFromArray($arr));
292
		$share->setHeaders(self::getHeadersFromArray($arr));
293
		if (key_exists('cloud_id', $arr)) {
294
			$share->setCloudID($arr['cloud_id']);
295
		}
296
297
		$share->setUniqueId($arr['unique_id']);
298
		$share->setAuthor($arr['author']);
299
		$share->setPayload($arr['payload']);
300
		$share->setCreation($arr['creation']);
301
302
		return $share;
303
	}
304
305
306
	/**
307
	 * @param array $arr
308
	 *
309
	 * @return array
310
	 */
311
	private static function getHeadersFromArray($arr) {
312
313
		$headers = [];
314
		if (key_exists('headers', $arr)) {
315
			$headers = $arr['headers'];
316
		}
317
318
		return $headers;
319
	}
320
321
322
	/**
323
	 * @param array $arr
324
	 *
325
	 * @return Circle
326
	 */
327
	private static function getCircleFromArray($arr) {
328
		$circle = Circle::fromArray(MiscService::get($arr, 'circle', null));
329
330
		$circle->setType(MiscService::get($arr, 'circle_type'));
331
		$circle->setName(MiscService::get($arr, 'circle_name'));
332
		$circle->setId(MiscService::get($arr, 'circle_id'));
333
334
		return $circle;
335
	}
336
}
337
338