Completed
Push — federated-circles ( 450355...2a3998 )
by Maxence
04:51 queued 02:17
created

SharingFrame::getSharer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 $sharer;
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(string $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
		if ($this->getSharer() === null) {
118
			$this->setSharer($author);
119
		}
120
	}
121
122
	/**
123
	 * @return string
124
	 */
125
	public function getAuthor() {
126
		return $this->author;
127
	}
128
129
130
	/**
131
	 * @param string $sharer
132
	 */
133
	public function setSharer(string $sharer) {
134
		$this->sharer = $sharer;
135
	}
136
137
	/**
138
	 * @return string
139
	 */
140
	public function getSharer() {
141
		return $this->sharer;
142
	}
143
144
145
	/**
146
	 * @param string $uniqueId
147
	 *
148
	 * @return SharingFrame
149
	 */
150
	public function setUniqueId(string $uniqueId) {
151
		$this->uniqueId = $uniqueId;
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	public function getUniqueId() {
160
		return $this->uniqueId;
161
	}
162
163
	/**
164
	 * @return SharingFrame
165
	 */
166
	public function generateUniqueId() {
167
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(16));
168
		$this->setUniqueId($uniqueId);
169
170
		return $this;
171
	}
172
173
	/**
174
	 * @param array $payload
175
	 */
176
	public function setPayload(array $payload) {
177
		$this->payload = $payload;
178
	}
179
180
	/**
181
	 * @param bool $asJson
182
	 *
183
	 * @return array|string
184
	 */
185
	public function getPayload(bool $asJson = false) {
186
		if ($asJson) {
187
			return json_encode($this->payload);
188
		}
189
190
		return $this->payload;
191
	}
192
193
194
	/**
195
	 * @param array $headers
196
	 */
197
	public function setHeaders(array $headers) {
198
		$this->headers = $headers;
199
	}
200
201
	/**
202
	 * @param bool $asJson
203
	 *
204
	 * @return array|string
205
	 */
206
	public function getHeaders(bool $asJson = false) {
207
		if ($asJson) {
208
			return json_encode($this->headers);
209
		}
210
211
		return $this->headers;
212
	}
213
214
215
	/**
216
	 * @param int $creation
217
	 */
218
	public function setCreation($creation) {
219
		if ($creation === null) {
220
			return;
221
		}
222
223
		$this->creation = $creation;
224
	}
225
226
	/**
227
	 * @return int
228
	 */
229
	public function getCreation() {
230
		return $this->creation;
231
	}
232
233
234
	public function jsonSerialize() {
235
		return array(
236
			'circle_id'   => $this->getCircleId(),
237
			'circle_name' => $this->getCircleName(),
238
			'source'      => $this->getSource(),
239
			'type'        => $this->getType(),
240
			'author'      => $this->getAuthor(),
241
			'sharer'      => $this->getSharer(),
242
			'payload'     => $this->getPayload(),
243
			'creation'    => $this->getCreation(),
244
		);
245
	}
246
247
	public static function fromJSON($json) {
248
249
		$arr = json_decode($json, true);
250
		if (!key_exists('source', $arr)) {
251
			return null;
252
		}
253
254
		$share = new SharingFrame($arr['source'], $arr['type']);
255
		$share->setCircleId($arr['circle_id']);
256
		if (key_exists('circle_name', $arr)) {
257
			$share->setCircleName($arr['circle_name']);
258
		}
259
260
		$share->setAuthor($arr['author']);
261
		$share->setSharer($arr['sharer']);
262
		$share->setPayload($arr['payload']);
263
		$share->setCreation($arr['creation']);
264
265
		return $share;
266
	}
267
268
}