Passed
Pull Request — master (#77)
by Daniel
36:50
created

WebsiteCore::setName()   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 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
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
	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 string $value
210
	 *
211
	 * @return $this
212
	 */
213
	public function setOption(string $key, string $value): self
214
	{
215
		$this->options[$key] = $value;
216
		return $this;
217
	}
218
219
	/**
220
	 * @param string $key
221
	 *
222
	 * @return string
223
	 */
224
	public function getOption($key): string
225
	{
226
		return $this->options[$key] ?? '';
227
	}
228
229
	/**
230
	 * @param array|string $options
231
	 *
232
	 * @return $this
233
	 */
234 10
	public function setOptions($options): self
235
	{
236 10
		if (!is_array($options)) {
237
			$options = json_decode($options, true);
238
		}
239
240 10
		if ($options === null) {
241
			return $this;
242
		}
243
244 10
		$this->options = $options;
245 10
		return $this;
246
	}
247
248
	/**
249
	 * @param bool $json
250
	 *
251
	 * @return array|string
252
	 */
253 5
	public function getOptions(bool $json = false)
254
	{
255 5
		if ($json === true) {
256 4
			return json_encode($this->options);
257
		}
258
259 2
		return $this->options;
260
	}
261
262
	/**
263
	 * @param string $path
264
	 *
265
	 * @return $this
266
	 */
267 10
	public function setPath(string $path): self
268
	{
269 10
		$this->path = $path ? rtrim($path, '/') . '/' : '';
270 10
		return $this;
271
	}
272
273
	/**
274
	 * @return string
275
	 */
276 7
	public function getPath(): string
277
	{
278 7
		return $this->path;
279
	}
280
281
	/**
282
	 * @param int $creation
283
	 *
284
	 * @return $this
285
	 */
286 10
	public function setCreation(int $creation): self
287
	{
288 10
		$this->creation = $creation;
289 10
		return $this;
290
	}
291
292
	/**
293
	 * @return int
294
	 */
295 2
	public function getCreation(): int
296
	{
297 2
		return $this->creation;
298
	}
299
300
	/**
301
	 * @param string $source
302
	 *
303
	 * @return $this
304
	 */
305 10
	public function setTemplateSource(string $source): self
306
	{
307 10
		$this->templateSource = $source;
308 10
		return $this;
309
	}
310
311
	/**
312
	 * @return string
313
	 */
314 5
	public function getTemplateSource(): string
315
	{
316 5
		return $this->templateSource;
317
	}
318
319
	/**
320
	 * @param string $page
321
	 *
322
	 * @return $this
323
	 */
324 10
	public function setPage(string $page): self
325
	{
326 10
		$this->page = $page;
327 10
		return $this;
328
	}
329
330
	/**
331
	 * @return string
332
	 */
333 4
	public function getPage(): string
334
	{
335 4
		return $this->page;
336
	}
337
338
	/**
339
	 * @param string $viewer
340
	 *
341
	 * @return $this
342
	 */
343 10
	public function setViewer(string $viewer): self
344
	{
345 10
		$this->viewer = $viewer;
346 10
		return $this;
347
	}
348
349
	/**
350
	 * @return string
351
	 */
352
	public function getViewer(): string
353
	{
354
		return $this->viewer;
355
	}
356
357
	/**
358
	 * @param bool $proxyRequest
359
	 *
360
	 * @return $this
361
	 */
362 10
	public function setProxyRequest(bool $proxyRequest): self
363
	{
364 10
		$this->proxyRequest = $proxyRequest;
365 10
		return $this;
366
	}
367
368
	/**
369
	 * @return bool
370
	 */
371 2
	public function getProxyRequest(): bool
372
	{
373 2
		return $this->proxyRequest;
374
	}
375
376
	/**
377
	 * @return array
378
	 */
379 2
	public function jsonSerialize(): array
380
	{
381
		return [
382 2
			'id' => $this->getId(),
383 2
			'user_id' => $this->getUserId(),
384 2
			'name' => $this->getName(),
385 2
			'site' => $this->getSite(),
386 2
			'theme' => $this->getTheme(),
387 2
			'type' => $this->getType(),
388 2
			'options' => $this->getOptions(),
389 2
			'path' => $this->getPath(),
390 2
			'creation' => $this->getCreation(),
391 2
			'template' => $this->getTemplateSource(),
392 2
			'page' => $this->getPage(),
393
		];
394
	}
395
396
	/**
397
	 * @param array $data
398
	 *
399
	 * @throws \UnexpectedValueException
400
	 */
401 10
	public function fromArray(array $data)
402
	{
403 10
		if (!isset($data['user_id']) || !isset($data['name']) || !isset($data['site']) || !isset($data['path'])) {
404
			throw new \UnexpectedValueException();
405
		}
406
407 10
		$options = [];
408 10
		if (!empty($data['options'])) {
409 8
			$options = is_array($data['options']) ? $data['options'] : json_decode($data['options'], true);
410
		}
411
412 10
		$creation = 0;
413 10
		if (!empty($data['creation'])) {
414 8
			$creation = is_numeric($data['creation']) ? (int) $data['creation'] : strtotime($data['creation']);
415
		}
416
417 10
		$this->setId(isset($data['id']) ? (int) $data['id'] : 0)
418 10
			->setUserId($data['user_id'])
419 10
			->setName($data['name'])
420 10
			->setSite($data['site'])
421 10
			->setTheme($data['theme'] ?? 'default')
422 10
			->setType(isset($data['type']) ? (int) $data['type'] : self::TYPE_PUBLIC)
423 10
			->setOptions($options)
424 10
			->setPath($data['path'])
425 10
			->setCreation($creation)
426 10
			->setTemplateSource($data['template'] ?? '')
427 10
			->setPage($data['page'] ?? '')
428 10
			->setViewer($data['viewer'] ?? '')
429 10
			->setProxyRequest(!empty($data['proxyRequest']));
430 10
	}
431
432
	/**
433
	 * @param string $json
434
	 *
435
	 * @throws \UnexpectedValueException
436
	 */
437
	public function fromJSON(string $json)
438
	{
439
		$this->fromArray(json_decode($json, true));
440
	}
441
}
442