Test Failed
Push — master ( 42eb83...0beeeb )
by Daniel
23:28
created

WebsiteCore::setProxyRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\CMSPico\Model;
27
28
class WebsiteCore implements \JsonSerializable
29
{
30
	/** @var int */
31
	public const TYPE_PUBLIC = 1;
32
33
	/** @var int */
34
	public const TYPE_PRIVATE = 2;
35
36
	/** @var int */
37
	private $id;
38
39
	/** @var string|null */
40
	private $userId;
41
42
	/** @var string */
43
	private $name;
44
45
	/** @var string */
46
	private $theme = 'default';
47
48
	/** @var string */
49
	private $site;
50
51
	/** @var int */
52
	private $type = self::TYPE_PUBLIC;
53
54
	/** @var array */
55
	private $options = [];
56
57
	/** @var string */
58
	private $path;
59
60
	/** @var string */
61
	private $page;
62
63
	/** @var int */
64
	private $creation;
65
66
	/** @var string */
67
	private $viewer;
68
69
	/** @var bool */
70
	private $proxyRequest;
71
72
	/** @var string */
73
	private $templateSource;
74
75
	/**
76
	 * WebsiteCore constructor.
77
	 *
78
	 * @param array|string|null $data
79
	 */
80 3
	public function __construct($data = null)
81
	{
82 3
		if (is_array($data)) {
83 3
			$this->fromArray($data);
84
		} elseif ($data !== null) {
85
			$this->fromJSON($data);
86
		}
87 3
	}
88
89
	/**
90
	 * @param int $id
91
	 *
92
	 * @return $this
93
	 */
94 3
	public function setId(int $id): self
95
	{
96 3
		$this->id = $id;
97 3
		return $this;
98
	}
99
100
	/**
101
	 * @return int
102
	 */
103
	public function getId(): int
104
	{
105
		return $this->id;
106
	}
107
108
	/**
109
	 * @param string|null $userId
110
	 *
111
	 * @return $this
112
	 */
113 3
	public function setUserId(?string $userId): self
114
	{
115 3
		$this->userId = $userId;
116 3
		return $this;
117
	}
118
119
	/**
120
	 * @return string|null
121
	 */
122 3
	public function getUserId(): ?string
123
	{
124 3
		return $this->userId;
125
	}
126
127
	/**
128
	 * @param string $name
129
	 *
130
	 * @return $this
131
	 */
132 3
	public function setName(string $name): self
133
	{
134 3
		$this->name = $name;
135 3
		return $this;
136
	}
137
138
	/**
139
	 * @return string
140
	 */
141 3
	public function getName(): string
142
	{
143 3
		return $this->name;
144
	}
145
146
	/**
147
	 * @param string $site
148
	 *
149
	 * @return $this
150
	 */
151 3
	public function setSite(string $site): self
152
	{
153 3
		$this->site = $site;
154 3
		return $this;
155
	}
156
157
	/**
158
	 * @return string
159
	 */
160 3
	public function getSite(): string
161
	{
162 3
		return $this->site;
163
	}
164
165
	/**
166
	 * @param string $theme
167
	 *
168
	 * @return $this
169
	 */
170 3
	public function setTheme(string $theme): self
171
	{
172 3
		$this->theme = $theme;
173 3
		return $this;
174
	}
175
176
	/**
177
	 * @return string
178
	 */
179 3
	public function getTheme(): string
180
	{
181 3
		return $this->theme;
182
	}
183
184
	/**
185
	 * @param int $type
186
	 *
187
	 * @return $this
188
	 */
189 3
	public function setType(int $type): self
190
	{
191 3
		if (!in_array($type, [ self::TYPE_PUBLIC, self::TYPE_PRIVATE ], true)) {
192
			throw new \UnexpectedValueException();
193
		}
194
195 3
		$this->type = $type;
196 3
		return $this;
197
	}
198
199
	/**
200
	 * @return int
201
	 */
202
	public function getType(): int
203
	{
204
		return $this->type;
205
	}
206
207
	/**
208
	 * @param string $key
209
	 * @param mixed  $value
210
	 *
211
	 * @return $this
212
	 */
213
	public function setOption(string $key, $value): self
214
	{
215
		if ($value === null) {
216
			unset($this->options[$key]);
217
			return $this;
218
		}
219
220
		$this->options[$key] = $value;
221
		return $this;
222
	}
223
224
	/**
225
	 * @param string $key
226
	 *
227
	 * @return mixed
228
	 */
229
	public function getOption(string $key)
230
	{
231
		return $this->options[$key] ?? null;
232
	}
233
234
	/**
235
	 * @param array|string|null $options
236
	 *
237
	 * @return $this
238
	 */
239 3
	public function setOptions($options): self
240
	{
241 3
		if (is_string($options)) {
242
			$options = json_decode($options, true);
243
		}
244
245 3
		if ($options === null) {
246
			return $this;
247
		}
248
249 3
		$this->options = $options;
250 3
		return $this;
251
	}
252
253
	/**
254
	 * @return array
255
	 */
256
	public function getOptions(): array
257
	{
258
		return $this->options;
259
	}
260
261
	/**
262
	 * @return string
263
	 */
264
	public function getOptionsJSON(): string
265
	{
266
		return json_encode($this->options);
267
	}
268
269
	/**
270
	 * @param string $path
271
	 *
272
	 * @return $this
273
	 */
274 3
	public function setPath(string $path): self
275
	{
276 3
		$this->path = $path ? rtrim($path, '/') . '/' : '';
277 3
		return $this;
278
	}
279
280
	/**
281
	 * @return string
282
	 */
283 3
	public function getPath(): string
284
	{
285 3
		return $this->path;
286
	}
287
288
	/**
289
	 * @param int $creation
290
	 *
291
	 * @return $this
292
	 */
293 3
	public function setCreation(int $creation): self
294
	{
295 3
		$this->creation = $creation;
296 3
		return $this;
297
	}
298
299
	/**
300
	 * @return int
301
	 */
302
	public function getCreation(): int
303
	{
304
		return $this->creation;
305
	}
306
307
	/**
308
	 * @param string $source
309
	 *
310
	 * @return $this
311
	 */
312 3
	public function setTemplateSource(string $source): self
313
	{
314 3
		$this->templateSource = $source;
315 3
		return $this;
316
	}
317
318
	/**
319
	 * @return string
320
	 */
321 3
	public function getTemplateSource(): string
322
	{
323 3
		return $this->templateSource;
324
	}
325
326
	/**
327
	 * @param string $page
328
	 *
329
	 * @return $this
330
	 */
331 3
	public function setPage(string $page): self
332
	{
333 3
		$this->page = $page;
334 3
		return $this;
335
	}
336
337
	/**
338
	 * @return string
339
	 */
340
	public function getPage(): string
341
	{
342
		return $this->page;
343
	}
344
345
	/**
346
	 * @param string $viewer
347
	 *
348
	 * @return $this
349
	 */
350 3
	public function setViewer(string $viewer): self
351
	{
352 3
		$this->viewer = $viewer;
353 3
		return $this;
354
	}
355
356
	/**
357
	 * @return string
358
	 */
359
	public function getViewer(): string
360
	{
361
		return $this->viewer;
362
	}
363
364
	/**
365
	 * @param bool $proxyRequest
366
	 *
367
	 * @return $this
368
	 */
369 3
	public function setProxyRequest(bool $proxyRequest): self
370
	{
371 3
		$this->proxyRequest = $proxyRequest;
372 3
		return $this;
373
	}
374
375
	/**
376
	 * @return bool
377
	 */
378
	public function getProxyRequest(): bool
379
	{
380
		return $this->proxyRequest;
381
	}
382
383
	/**
384
	 * @return array
385
	 */
386
	public function getData(): array
387
	{
388
		return [
389
			'id' => $this->getId(),
390
			'user_id' => $this->getUserId(),
391
			'name' => $this->getName(),
392
			'site' => $this->getSite(),
393
			'theme' => $this->getTheme(),
394
			'type' => $this->getType(),
395
			'options' => $this->getOptions(),
396
			'path' => $this->getPath(),
397
			'creation' => $this->getCreation(),
398
			'template' => $this->getTemplateSource(),
399
			'page' => $this->getPage(),
400
		];
401
	}
402
403
	/**
404
	 * @return array
405
	 */
406
	public function jsonSerialize(): array
407
	{
408 3
		return $this->getData();
409
	}
410 3
411
	/**
412
	 * @param array $data
413
	 *
414 3
	 * @throws \UnexpectedValueException
415 3
	 */
416
	public function fromArray(array $data): void
417
	{
418
		if (!isset($data['user_id']) || !isset($data['name']) || !isset($data['site']) || !isset($data['path'])) {
419 3
			throw new \UnexpectedValueException();
420 3
		}
421
422
		$options = [];
423
		if (!empty($data['options'])) {
424 3
			$options = is_array($data['options']) ? $data['options'] : json_decode($data['options'], true);
425 3
		}
426 3
427 3
		$creation = 0;
428 3
		if (!empty($data['creation'])) {
429 3
			$creation = is_numeric($data['creation']) ? (int) $data['creation'] : strtotime($data['creation']);
430 3
		}
431 3
432 3
		$this->setId(isset($data['id']) ? (int) $data['id'] : 0)
433 3
			->setUserId($data['user_id'])
434 3
			->setName($data['name'])
435 3
			->setSite($data['site'])
436 3
			->setTheme($data['theme'] ?? 'default')
437 3
			->setType(isset($data['type']) ? (int) $data['type'] : self::TYPE_PUBLIC)
438
			->setOptions($options)
439
			->setPath($data['path'])
440
			->setCreation($creation)
441
			->setTemplateSource($data['template'] ?? '')
442
			->setPage($data['page'] ?? '')
443
			->setViewer($data['viewer'] ?? '')
444
			->setProxyRequest(!empty($data['proxyRequest']));
445
	}
446
447
	/**
448
	 * @param string $json
449
	 *
450
	 * @throws \UnexpectedValueException
451
	 */
452
	public function fromJSON(string $json): void
453
	{
454
		$this->fromArray(json_decode($json, true));
455
	}
456
}
457