Completed
Pull Request — master (#362)
by Maxence
02:14
created

GSShare::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Model\GlobalScale;
31
32
33
use daita\MySmallPhpTools\Traits\TArrayTools;
34
use JsonSerializable;
35
36
37
/**
38
 * Class GSShare
39
 *
40
 * @package OCA\Circles\Model\GlobalScale
41
 */
42
class GSShare implements JsonSerializable {
43
44
45
	use TArrayTools;
46
47
48
	/** @var int */
49
	private $id = 0;
50
51
	/** @var string */
52
	private $circleId = '';
53
54
	/** @var string */
55
	private $defaultMountPoint = '';
56
57
	/** @var string */
58
	private $mountPoint = '';
59
60
	/** @var int */
61
	private $parent = -1;
62
63
	/** @var string */
64
	private $owner = '';
65
66
	/** @var string */
67
	private $instance = '';
68
69
	/** @var string */
70
	private $token = '';
71
72
	/** @var string */
73
	private $password = '';
74
75
76
	/**
77
	 * GSShare constructor.
78
	 *
79
	 * @param string $circleId
80
	 * @param string $token
81
	 */
82
	public function __construct(string $circleId = '', string $token = '') {
83
		$this->circleId = $circleId;
84
		$this->token = $token;
85
	}
86
87
88
	/**
89
	 * @return int
90
	 */
91
	public function getId(): int {
92
		return $this->id;
93
	}
94
95
	public function setId(int $id): self {
96
		$this->id = $id;
97
98
		return $this;
99
	}
100
101
102
	/**
103
	 *
104
	 * @return string
105
	 */
106
	public function getCircleId(): string {
107
		return $this->circleId;
108
	}
109
110
	/**
111
	 * @param string $circleId
112
	 *
113
	 * @return GSShare
114
	 */
115
	public function setCircleId(string $circleId): self {
116
		$this->circleId = $circleId;
117
118
		return $this;
119
	}
120
121
122
	/**
123
	 * @return string
124
	 */
125
	public function getDefaultMountPoint(): string {
126
		return $this->defaultMountPoint;
127
	}
128
129
	/**
130
	 * @param string $mountPoint
131
	 *
132
	 * @return GSShare
133
	 */
134
	public function setDefaultMountPoint(string $mountPoint): self {
135
		$this->defaultMountPoint = $mountPoint;
136
137
		return $this;
138
	}
139
140
141
	/**
142
	 * @param string $userId
143
	 *
144
	 * @return string
145
	 */
146
	public function getMountPoint(string $userId = ''): string {
147
		$mountPoint = $this->mountPoint;
148
149
		if ($mountPoint === '') {
150
			$mountPoint = $this->defaultMountPoint;
151
		}
152
153
		if ($userId === '') {
154
			return $mountPoint;
155
		}
156
157
		return '/' . $userId . '/files/' . ltrim($mountPoint, '/');
158
	}
159
160
	/**
161
	 * @param string $mountPoint
162
	 *
163
	 * @return GSShare
164
	 */
165
	public function setMountPoint(string $mountPoint): self {
166
		$this->mountPoint = $mountPoint;
167
168
		return $this;
169
	}
170
171
172
	/**
173
	 * @return int
174
	 */
175
	public function getParent(): int {
176
		return $this->parent;
177
	}
178
179
	/**
180
	 * @param int $parent
181
	 *
182
	 * @return GSShare
183
	 */
184
	public function setParent(int $parent): self {
185
		$this->parent = $parent;
186
187
		return $this;
188
	}
189
190
191
	/**
192
	 * @return string
193
	 */
194
	public function getOwner(): string {
195
		return $this->owner;
196
	}
197
198
	/**
199
	 * @param string $owner
200
	 *
201
	 * @return GSShare
202
	 */
203
	public function setOwner(string $owner): self {
204
		$this->owner = $owner;
205
206
		return $this;
207
	}
208
209
210
	/**
211
	 * @return string
212
	 */
213
	public function getInstance(): string {
214
		return $this->instance;
215
	}
216
217
	/**
218
	 * @param string $instance
219
	 *
220
	 * @return GSShare
221
	 */
222
	public function setInstance(string $instance): self {
223
		$this->instance = $instance;
224
225
		return $this;
226
	}
227
228
229
	/**
230
	 * @return string
231
	 */
232
	public function getToken(): string {
233
		return $this->token;
234
	}
235
236
237
	/**
238
	 * @param string $token
239
	 *
240
	 * @return GSShare
241
	 */
242
	public function setToken(string $token): self {
243
		$this->token = $token;
244
245
		return $this;
246
	}
247
248
249
	/**
250
	 * @return string
251
	 */
252
	public function getPassword(): string {
253
		return $this->password;
254
	}
255
256
	/**
257
	 * @param string $password
258
	 *
259
	 * @return GSShare
260
	 */
261
	public function setPassword(string $password): self {
262
		$this->password = $password;
263
264
		return $this;
265
	}
266
267
268
	/**
269
	 * @param array $data
270
	 *
271
	 * @return GSShare
272
	 */
273
	public function importFromDatabase(array $data): self {
274
		$this->setId($this->getInt('id', $data));
275
		$this->setCircleId($this->get('circle_id', $data));
276
		$this->setOwner($this->get('owner', $data));
277
		$this->setInstance($this->get('instance', $data));
278
		$this->setToken($this->get('token', $data));
279
		$this->setParent($this->getInt('parent', $data));
280
		$this->setMountPoint($this->get('gsshares_mountpoint', $data));
281
		$this->setDefaultMountPoint($this->get('mountpoint', $data));
282
283
		return $this;
284
	}
285
286
287
	/**
288
	 * @param string $userId
289
	 * @param string $protocol
290
	 *
291
	 * @return array
292
	 */
293
	public function toMount(string $userId, string $protocol = 'https'): array {
294
		return [
295
			'owner'       => $this->getOwner(),
296
			'remote'      => $protocol . '://' . $this->getInstance(),
297
			'token'       => $this->getToken(),
298
			'share_token' => $this->getToken(),
299
			'password'    => $this->getPassword(),
300
			'mountpoint'  => $this->getMountPoint($userId)
301
		];
302
	}
303
304
305
	/**
306
	 * @return array
307
	 */
308
	function jsonSerialize(): array {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
309
		return [
310
			'id'                => $this->getId(),
311
			'defaultMountPoint' => $this->getDefaultMountPoint(),
312
			'mountPoint'        => $this->getMountPoint(),
313
			'parent'            => $this->getParent(),
314
			'owner'             => $this->getOwner(),
315
			'instance'          => $this->getInstance(),
316
			'token'             => $this->getToken(),
317
			'password'          => $this->getPassword()
318
		];
319
	}
320
321
}
322
323