Test Setup Failed
Push — master ( e9c97c...d2f071 )
by
unknown
44:25
created

WebsiteCore::setOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
ccs 5
cts 7
cp 0.7143
rs 10
cc 3
nc 4
nop 1
crap 3.2098
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
	const TYPE_PUBLIC = 1;
32
33
	/** @var int */
34
	const TYPE_PRIVATE = 2;
35
36
	/** @var int */
37
	private $id;
38
39
	/** @var string */
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 10
	public function __construct($data = null)
81
	{
82 10
		if (is_array($data)) {
83 10
			$this->fromArray($data);
84
		} elseif ($data !== null) {
85
			$this->fromJSON($data);
86
		}
87 10
	}
88
89
	/**
90
	 * @param int $id
91
	 *
92
	 * @return $this
93
	 */
94 10
	public function setId(int $id): self
95
	{
96 10
		$this->id = $id;
97 10
		return $this;
98
	}
99
100
	/**
101
	 * @return int
102
	 */
103 4
	public function getId(): int
104
	{
105 4
		return $this->id;
106
	}
107
108
	/**
109
	 * @param string $userId
110
	 *
111
	 * @return $this
112
	 */
113 10
	public function setUserId(string $userId): self
114
	{
115 10
		$this->userId = $userId;
116 10
		return $this;
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122 9
	public function getUserId(): string
123
	{
124 9
		return $this->userId;
125
	}
126
127
	/**
128
	 * @param string $name
129
	 *
130
	 * @return $this
131
	 */
132 10
	public function setName(string $name): self
133
	{
134 10
		$this->name = $name;
135 10
		return $this;
136
	}
137
138
	/**
139
	 * @return string
140
	 */
141 7
	public function getName(): string
142
	{
143 7
		return $this->name;
144
	}
145
146
	/**
147
	 * @param string $site
148
	 *
149
	 * @return $this
150
	 */
151 10
	public function setSite(string $site): self
152
	{
153 10
		$this->site = $site;
154 10
		return $this;
155
	}
156
157
	/**
158
	 * @return string
159
	 */
160 7
	public function getSite(): string
161
	{
162 7
		return $this->site;
163
	}
164
165
	/**
166
	 * @param string $theme
167
	 *
168
	 * @return $this
169
	 */
170 10
	public function setTheme(string $theme): self
171
	{
172 10
		$this->theme = $theme;
173 10
		return $this;
174
	}
175
176
	/**
177
	 * @return string
178
	 */
179 7
	public function getTheme(): string
180
	{
181 7
		return $this->theme;
182
	}
183
184
	/**
185
	 * @param int $type
186
	 *
187
	 * @return $this
188
	 */
189 10
	public function setType(int $type): self
190
	{
191 10
		if (!in_array($type, [ self::TYPE_PUBLIC, self::TYPE_PRIVATE ], true)) {
192
			throw new \UnexpectedValueException();
193
		}
194
195 10
		$this->type = $type;
196 10
		return $this;
197
	}
198
199
	/**
200
	 * @return int
201
	 */
202 7
	public function getType(): int
203
	{
204 7
		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($key)
230
	{
231
		return $this->options[$key] ?? null;
232
	}
233
234
	/**
235
	 * @param array|string $options
236
	 *
237
	 * @return $this
238
	 */
239 10
	public function setOptions($options): self
240
	{
241 10
		if (is_string($options)) {
242
			$options = json_decode($options, true);
243
		}
244
245 10
		if ($options === null) {
246
			return $this;
247
		}
248
249 10
		$this->options = $options;
250 10
		return $this;
251
	}
252
253
	/**
254
	 * @param bool $json
255
	 *
256
	 * @return array|string
257
	 */
258 5
	public function getOptions(bool $json = false)
259
	{
260 5
		if ($json === true) {
261 4
			return json_encode($this->options);
262
		}
263
264 2
		return $this->options;
265
	}
266
267
	/**
268
	 * @param string $path
269
	 *
270
	 * @return $this
271
	 */
272 10
	public function setPath(string $path): self
273
	{
274 10
		$this->path = $path ? rtrim($path, '/') . '/' : '';
275 10
		return $this;
276
	}
277
278
	/**
279
	 * @return string
280
	 */
281 7
	public function getPath(): string
282
	{
283 7
		return $this->path;
284
	}
285
286
	/**
287
	 * @param int $creation
288
	 *
289
	 * @return $this
290
	 */
291 10
	public function setCreation(int $creation): self
292
	{
293 10
		$this->creation = $creation;
294 10
		return $this;
295
	}
296
297
	/**
298
	 * @return int
299
	 */
300 2
	public function getCreation(): int
301
	{
302 2
		return $this->creation;
303
	}
304
305
	/**
306
	 * @param string $source
307
	 *
308
	 * @return $this
309
	 */
310 10
	public function setTemplateSource(string $source): self
311
	{
312 10
		$this->templateSource = $source;
313 10
		return $this;
314
	}
315
316
	/**
317
	 * @return string
318
	 */
319 5
	public function getTemplateSource(): string
320
	{
321 5
		return $this->templateSource;
322
	}
323
324
	/**
325
	 * @param string $page
326
	 *
327
	 * @return $this
328
	 */
329 10
	public function setPage(string $page): self
330
	{
331 10
		$this->page = $page;
332 10
		return $this;
333
	}
334
335
	/**
336
	 * @return string
337
	 */
338 4
	public function getPage(): string
339
	{
340 4
		return $this->page;
341
	}
342
343
	/**
344
	 * @param string $viewer
345
	 *
346
	 * @return $this
347
	 */
348 10
	public function setViewer(string $viewer): self
349
	{
350 10
		$this->viewer = $viewer;
351 10
		return $this;
352
	}
353
354
	/**
355
	 * @return string
356
	 */
357
	public function getViewer(): string
358
	{
359
		return $this->viewer;
360
	}
361
362
	/**
363
	 * @param bool $proxyRequest
364
	 *
365
	 * @return $this
366
	 */
367 10
	public function setProxyRequest(bool $proxyRequest): self
368
	{
369 10
		$this->proxyRequest = $proxyRequest;
370 10
		return $this;
371
	}
372
373
	/**
374
	 * @return bool
375
	 */
376 2
	public function getProxyRequest(): bool
377
	{
378 2
		return $this->proxyRequest;
379
	}
380
381
	/**
382
	 * @return array
383
	 */
384 2
	public function jsonSerialize(): array
385
	{
386
		return [
387 2
			'id' => $this->getId(),
388 2
			'user_id' => $this->getUserId(),
389 2
			'name' => $this->getName(),
390 2
			'site' => $this->getSite(),
391 2
			'theme' => $this->getTheme(),
392 2
			'type' => $this->getType(),
393 2
			'options' => $this->getOptions(),
394 2
			'path' => $this->getPath(),
395 2
			'creation' => $this->getCreation(),
396 2
			'template' => $this->getTemplateSource(),
397 2
			'page' => $this->getPage(),
398
		];
399
	}
400
401
	/**
402
	 * @param array $data
403
	 *
404
	 * @throws \UnexpectedValueException
405
	 */
406 10
	public function fromArray(array $data)
407
	{
408 10
		if (!isset($data['user_id']) || !isset($data['name']) || !isset($data['site']) || !isset($data['path'])) {
409
			throw new \UnexpectedValueException();
410
		}
411
412 10
		$options = [];
413 10
		if (!empty($data['options'])) {
414 8
			$options = is_array($data['options']) ? $data['options'] : json_decode($data['options'], true);
415
		}
416
417 10
		$creation = 0;
418 10
		if (!empty($data['creation'])) {
419 8
			$creation = is_numeric($data['creation']) ? (int) $data['creation'] : strtotime($data['creation']);
420
		}
421
422 10
		$this->setId(isset($data['id']) ? (int) $data['id'] : 0)
423 10
			->setUserId($data['user_id'])
424 10
			->setName($data['name'])
425 10
			->setSite($data['site'])
426 10
			->setTheme($data['theme'] ?? 'default')
427 10
			->setType(isset($data['type']) ? (int) $data['type'] : self::TYPE_PUBLIC)
428 10
			->setOptions($options)
429 10
			->setPath($data['path'])
430 10
			->setCreation($creation)
431 10
			->setTemplateSource($data['template'] ?? '')
432 10
			->setPage($data['page'] ?? '')
433 10
			->setViewer($data['viewer'] ?? '')
434 10
			->setProxyRequest(!empty($data['proxyRequest']));
435 10
	}
436
437
	/**
438
	 * @param string $json
439
	 *
440
	 * @throws \UnexpectedValueException
441
	 */
442
	public function fromJSON(string $json)
443
	{
444
		$this->fromArray(json_decode($json, true));
445
	}
446
}
447