Completed
Pull Request — master (#106)
by Maxence
02:33
created

SharingFrame::setUniqueId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\SharingFrameSourceCannotBeAppCirclesException;
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 $circleUniqueId;
41
42
	/** @var string */
43
	private $circleName;
44
45
	/** @var int */
46
	private $circleType;
47
48
	/** @var string */
49
	private $author;
50
51
	/** @var string */
52
	private $cloudId;
53
54
	/** @var array */
55
	private $payload;
56
57
	/** @var array */
58
	private $headers;
59
60
	/** @var int */
61
	private $creation;
62
63
	/** @var string */
64
	private $uniqueId;
65
66
	public function __construct($source, $type) {
67
		$this->source = (string)$source;
68
		$this->type = (string)$type;
69
	}
70
71
72
	/**
73
	 * @return string
74
	 */
75
	public function getSource() {
76
		return $this->source;
77
	}
78
79
	/**
80
	 * @return string
81
	 */
82
	public function getType() {
83
		return $this->type;
84
	}
85
86
	/**
87
	 * @param string $circleUniqueId
88
	 */
89
	public function setCircleId($circleUniqueId) {
90
		$this->circleUniqueId = $circleUniqueId;
0 ignored issues
show
Documentation Bug introduced by
The property $circleUniqueId was declared of type integer, but $circleUniqueId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
91
	}
92
93
	/**
94
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

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