Completed
Push — master ( 2079de...41fdc1 )
by Maxence
02:55
created

SharingFrame::getHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
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
32
class SharingFrame implements \JsonSerializable {
33
34
	/** @var string */
35
	private $source;
36
37
	/** @var string */
38
	private $type;
39
40
	/** @var Circle */
41
	private $circle;
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($source, $type) {
62
		$this->source = (string)$source;
63
		$this->type = (string)$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 Circle $circle
83
	 */
84
	public function setCircle($circle) {
85
		$this->circle = $circle;
86
	}
87
88
	/**
89
	 * @return Circle
90
	 */
91
	public function getCircle() {
92
		return $this->circle;
93
	}
94
95
96
	/**
97
	 * @param string $author
98
	 */
99
	public function setAuthor($author) {
100
		$this->author = (string)$author;
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	public function getAuthor() {
107
		return $this->author;
108
	}
109
110
111
	/**
112
	 * @param string $cloudId
113
	 */
114
	public function setCloudId($cloudId) {
115
		$this->cloudId = $cloudId;
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function getCloudId() {
122
		return $this->cloudId;
123
	}
124
125
126
	/**
127
	 * @param string $uniqueId
128
	 *
129
	 * @return SharingFrame
130
	 */
131
	public function setUniqueId($uniqueId) {
132
		$this->uniqueId = (string)$uniqueId;
133
134
		return $this;
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	public function getUniqueId() {
141
		return $this->uniqueId;
142
	}
143
144
	/**
145
	 * @return SharingFrame
146
	 */
147
	public function generateUniqueId() {
148
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(16));
149
		$this->setUniqueId($uniqueId);
150
151
		return $this;
152
	}
153
154
	/**
155
	 * @param array $payload
156
	 */
157
	public function setPayload($payload) {
158
		$this->payload = $payload;
159
	}
160
161
	/**
162
	 * @param bool $asJson
163
	 *
164
	 * @return array|string
165
	 */
166
	public function getPayload($asJson = false) {
167
		if ($asJson) {
168
			return json_encode($this->payload);
169
		}
170
171
		return $this->payload;
172
	}
173
174
175
	/**
176
	 * @param array $headers
177
	 */
178
	public function setHeaders($headers) {
179
		$this->headers = $headers;
180
	}
181
182
	/**
183
	 * @param bool $asJson
184
	 *
185
	 * @return array|string
186
	 */
187
	public function getHeaders($asJson = false) {
188
		if ($asJson) {
189
			return json_encode($this->headers);
190
		}
191
192
		return $this->headers;
193
	}
194
195
196
	/**
197
	 * @param string $k
198
	 *
199
	 * @return string
200
	 */
201
	public function getHeader($k) {
202
		if ($this->headers === null) {
203
			return null;
204
		}
205
206
		$k = (string)$k;
207
		if (!key_exists($k, $this->headers)) {
208
			return null;
209
		}
210
211
		return $this->headers[$k];
212
	}
213
214
	/**
215
	 * @param string $k
216
	 * @param string $v
217
	 */
218
	public function setHeader($k, $v) {
219
		$this->headers[(string)$k] = $v;
220
	}
221
222
223
	/**
224
	 * @param int $creation
225
	 */
226
	public function setCreation($creation) {
227
		if ($creation === null) {
228
			return;
229
		}
230
231
		$this->creation = $creation;
232
	}
233
234
	/**
235
	 * @return int
236
	 */
237
	public function getCreation() {
238
		return $this->creation;
239
	}
240
241
242
	/**
243
	 * @return bool
244
	 */
245
	public function isLocal() {
246
		return ($this->getCloudId() === null);
247
	}
248
249
250
	/**
251
	 * @return bool
252
	 */
253
	public function is0Circle() {
254
		return ($this->getCloudId() === null);
255
	}
256
257
	/**
258
	 * @throws SharingFrameSourceCannotBeAppCirclesException
259
	 */
260
	public function cannotBeFromCircles() {
261
		if (strtolower($this->getSource()) === Application::APP_NAME) {
262
			throw new SharingFrameSourceCannotBeAppCirclesException();
263
		}
264
	}
265
266
267
	public function jsonSerialize() {
268
		return array(
269
			'unique_id' => $this->getUniqueId(),
270
			'circle'    => $this->getCircle()->getArray(false, true),
271
			'source'    => $this->getSource(),
272
			'type'      => $this->getType(),
273
			'author'    => $this->getAuthor(),
274
			'cloud_id'  => $this->getCloudId(),
275
			'headers'   => $this->getHeaders(),
276
			'payload'   => $this->getPayload(),
277
			'creation'  => $this->getCreation(),
278
		);
279
	}
280
281
	public static function fromJSON($json) {
282
283
		$arr = json_decode($json, true);
284
		if (!is_array($arr) || !key_exists('source', $arr)) {
285
			return null;
286
		}
287
288
		$share = new SharingFrame($arr['source'], $arr['type']);
289
290
		$circle = new Circle();
291
		if (key_exists('circle', $arr)) {
292
			$circle = Circle::fromArray($arr['circle']);
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Circle::fromArray() has been deprecated.

This method has been deprecated.

Loading history...
293
		}
294
295
		// TODO 0.15.0 - remove those 3 conditions
296
		if (key_exists('circle_type', $arr)) {
297
			$circle->setType($arr['circle_type']);
298
		}
299
		if (key_exists('circle_name', $arr)) {
300
			$circle->setName($arr['circle_name']);
301
		}
302
		if (key_exists('circle_id', $arr)) {
303
			$circle->setId($arr['circle_id']);
304
		}
305
		$share->setCircle($circle);
306
307
		if (key_exists('headers', $arr)) {
308
			$share->setHeaders($arr['headers']);
309
		}
310
311
		if (key_exists('cloud_id', $arr)) {
312
			$share->setCloudID($arr['cloud_id']);
313
		}
314
315
		$share->setUniqueId($arr['unique_id']);
316
		$share->setAuthor($arr['author']);
317
		$share->setPayload($arr['payload']);
318
		$share->setCreation($arr['creation']);
319
320
		return $share;
321
	}
322
323
}
324
325