Completed
Push — master ( 3bbe82...a6ef08 )
by Maxence
02:32
created

SharingFrame::setHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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