Completed
Push — federated-circles ( 545292...450355 )
by Maxence
02:38
created

SharingFrame::getUniqueId()   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 int */
53
	private $creation;
54
55
	/** @var string */
56
	private $uniqueId;
57
58
	public function __construct(string $source, string $type) {
59
		$this->source = $source;
60
		$this->type = $type;
61
	}
62
63
64
	/**
65
	 * @return string
66
	 */
67
	public function getSource() {
68
		return $this->source;
69
	}
70
71
	/**
72
	 * @return string
73
	 */
74
	public function getType() {
75
		return $this->type;
76
	}
77
78
	/**
79
	 * @param int $circleId
80
	 */
81
	public function setCircleId(int $circleId) {
82
		$this->circleId = $circleId;
83
	}
84
85
	/**
86
	 * @return int
87
	 */
88
	public function getCircleId() {
89
		return $this->circleId;
90
	}
91
92
93
	/**
94
	 * @param string $circleName
95
	 */
96
	public function setCircleName(string $circleName) {
97
		$this->circleName = $circleName;
98
	}
99
100
	/**
101
	 * @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...
102
	 */
103
	public function getCircleName() {
104
		return $this->circleName;
105
	}
106
107
108
	/**
109
	 * @param string $author
110
	 */
111
	public function setAuthor(string $author) {
112
		$this->author = $author;
113
114
		if ($this->getSharer() === null) {
115
			$this->setSharer($author);
116
		}
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122
	public function getAuthor() {
123
		return $this->author;
124
	}
125
126
127
	/**
128
	 * @param string $sharer
129
	 */
130
	public function setSharer(string $sharer) {
131
		$this->sharer = $sharer;
132
	}
133
134
	/**
135
	 * @return string
136
	 */
137
	public function getSharer() {
138
		return $this->sharer;
139
	}
140
141
142
	/**
143
	 * @param string $uniqueId
144
	 *
145
	 * @return SharingFrame
146
	 */
147
	public function setUniqueId(string $uniqueId) {
148
		$this->uniqueId = $uniqueId;
149
150
		return $this;
151
	}
152
153
	/**
154
	 * @return string
155
	 */
156
	public function getUniqueId() {
157
		return $this->uniqueId;
158
	}
159
160
	/**
161
	 * @return SharingFrame
162
	 */
163
	public function generateUniqueId() {
164
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(16));
165
		$this->setUniqueId($uniqueId);
166
167
		return $this;
168
	}
169
170
	/**
171
	 * @param array $payload
172
	 */
173
	public function setPayload(array $payload) {
174
		$this->payload = $payload;
175
	}
176
177
	/**
178
	 * @param bool $asJson
179
	 *
180
	 * @return array|string
181
	 */
182
	public function getPayload(bool $asJson = false) {
183
		if ($asJson) {
184
			return json_encode($this->payload);
185
		}
186
187
		return $this->payload;
188
	}
189
190
191
	/**
192
	 * @param int $creation
193
	 */
194
	public function setCreation($creation) {
195
		if ($creation === null) {
196
			return;
197
		}
198
199
		$this->creation = $creation;
200
	}
201
202
	/**
203
	 * @return int
204
	 */
205
	public function getCreation() {
206
		return $this->creation;
207
	}
208
209
210
	public function jsonSerialize() {
211
		return array(
212
			'circle_id'   => $this->getCircleId(),
213
			'circle_name' => $this->getCircleName(),
214
			'source'      => $this->getSource(),
215
			'type'        => $this->getType(),
216
			'author'      => $this->getAuthor(),
217
			'sharer'      => $this->getSharer(),
218
			'payload'     => $this->getPayload(),
219
			'creation'    => $this->getCreation(),
220
		);
221
	}
222
223
	public static function fromJSON($json) {
224
225
		$arr = json_decode($json, true);
226
		if (!key_exists('source', $arr)) {
227
			return null;
228
		}
229
230
		$share = new SharingFrame($arr['source'], $arr['type']);
231
		$share->setCircleId($arr['circle_id']);
232
		if (key_exists('circle_name', $arr)) {
233
			$share->setCircleName($arr['circle_name']);
234
		}
235
236
		$share->setAuthor($arr['author']);
237
		$share->setSharer($arr['sharer']);
238
		$share->setPayload($arr['payload']);
239
		$share->setCreation($arr['creation']);
240
241
		return $share;
242
	}
243
244
}