Test Failed
Push — master ( 0beeeb...19c15a )
by Daniel
23:53
created

WebsiteCore::setCreation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
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 2
cts 2
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 */
40
	private $userId;
41
42
	/** @var string */
43
	private $name;
44
45
	/** @var string */
46
	private $site;
47
48
	/** @var string */
49
	private $theme = 'default';
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 int */
61
	private $creation;
62
63
	/** @var string|null */
64
	private $templateSource;
65
66
	/**
67
	 * WebsiteCore constructor.
68
	 *
69
	 * @param array|null $data
70
	 */
71
	public function __construct(array $data = null)
72
	{
73
		if ($data !== null) {
74
			$this->fromArray($data);
75
		}
76
	}
77
78
	/**
79
	 * @param int $id
80 3
	 *
81
	 * @return $this
82 3
	 */
83 3
	public function setId(int $id): self
84
	{
85
		$this->id = $id;
86
		return $this;
87 3
	}
88
89
	/**
90
	 * @return int
91
	 */
92
	public function getId(): int
93
	{
94 3
		return $this->id;
95
	}
96 3
97 3
	/**
98
	 * @param string $userId
99
	 *
100
	 * @return $this
101
	 */
102
	public function setUserId(string $userId): self
103
	{
104
		$this->userId = $userId;
105
		return $this;
106
	}
107
108
	/**
109
	 * @return string
110
	 */
111
	public function getUserId(): string
112
	{
113 3
		return $this->userId;
114
	}
115 3
116 3
	/**
117
	 * @param string $name
118
	 *
119
	 * @return $this
120
	 */
121
	public function setName(string $name): self
122 3
	{
123
		$this->name = $name;
124 3
		return $this;
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	public function getName(): string
131
	{
132 3
		return $this->name;
133
	}
134 3
135 3
	/**
136
	 * @param string $site
137
	 *
138
	 * @return $this
139
	 */
140
	public function setSite(string $site): self
141 3
	{
142
		$this->site = $site;
143 3
		return $this;
144
	}
145
146
	/**
147
	 * @return string
148
	 */
149
	public function getSite(): string
150
	{
151 3
		return $this->site;
152
	}
153 3
154 3
	/**
155
	 * @param string $theme
156
	 *
157
	 * @return $this
158
	 */
159
	public function setTheme(string $theme): self
160 3
	{
161
		$this->theme = $theme;
162 3
		return $this;
163
	}
164
165
	/**
166
	 * @return string
167
	 */
168
	public function getTheme(): string
169
	{
170 3
		return $this->theme;
171
	}
172 3
173 3
	/**
174
	 * @param int $type
175
	 *
176
	 * @return $this
177
	 */
178
	public function setType(int $type): self
179 3
	{
180
		if (!in_array($type, [ self::TYPE_PUBLIC, self::TYPE_PRIVATE ], true)) {
181 3
			throw new \UnexpectedValueException();
182
		}
183
184
		$this->type = $type;
185
		return $this;
186
	}
187
188
	/**
189 3
	 * @return int
190
	 */
191 3
	public function getType(): int
192
	{
193
		return $this->type;
194
	}
195 3
196 3
	/**
197
	 * @param string $key
198
	 * @param mixed  $value
199
	 *
200
	 * @return $this
201
	 */
202
	public function setOption(string $key, $value): self
203
	{
204
		if ($value === null) {
205
			unset($this->options[$key]);
206
			return $this;
207
		}
208
209
		$this->options[$key] = $value;
210
		return $this;
211
	}
212
213
	/**
214
	 * @param string $key
215
	 *
216
	 * @return mixed
217
	 */
218
	public function getOption(string $key)
219
	{
220
		return $this->options[$key] ?? null;
221
	}
222
223
	/**
224
	 * @param array|string|null $options
225
	 *
226
	 * @return $this
227
	 */
228
	public function setOptions($options): self
229
	{
230
		if (is_string($options)) {
231
			$options = json_decode($options, true);
232
		}
233
234
		if ($options === null) {
235
			return $this;
236
		}
237
238
		$this->options = $options;
239 3
		return $this;
240
	}
241 3
242
	/**
243
	 * @return array
244
	 */
245 3
	public function getOptions(): array
246
	{
247
		return $this->options;
248
	}
249 3
250 3
	/**
251
	 * @return string
252
	 */
253
	public function getOptionsJSON(): string
254
	{
255
		return json_encode($this->options);
256
	}
257
258
	/**
259
	 * @param string $path
260
	 *
261
	 * @return $this
262
	 */
263
	public function setPath(string $path): self
264
	{
265
		$this->path = $path ? rtrim($path, '/') . '/' : '';
266
		return $this;
267
	}
268
269
	/**
270
	 * @return string
271
	 */
272
	public function getPath(): string
273
	{
274 3
		return $this->path;
275
	}
276 3
277 3
	/**
278
	 * @param int $creation
279
	 *
280
	 * @return $this
281
	 */
282
	public function setCreation(int $creation): self
283 3
	{
284
		$this->creation = $creation;
285 3
		return $this;
286
	}
287
288
	/**
289
	 * @return int
290
	 */
291
	public function getCreation(): int
292
	{
293 3
		return $this->creation;
294
	}
295 3
296 3
	/**
297
	 * @param string $source
298
	 *
299
	 * @return $this
300
	 */
301
	public function setTemplateSource(string $source): self
302
	{
303
		$this->templateSource = $source;
304
		return $this;
305
	}
306
307
	/**
308
	 * @return string|null
309
	 */
310
	public function getTemplateSource(): ?string
311
	{
312 3
		return $this->templateSource;
313
	}
314 3
315 3
	/**
316
	 * @return array
317
	 */
318
	public function getData(): array
319
	{
320
		return [
321 3
			'id' => $this->getId(),
322
			'user_id' => $this->getUserId(),
323 3
			'name' => $this->getName(),
324
			'site' => $this->getSite(),
325
			'theme' => $this->getTheme(),
326
			'type' => $this->getType(),
327
			'options' => $this->getOptions(),
328
			'path' => $this->getPath(),
329
			'creation' => $this->getCreation(),
330
			'template' => $this->getTemplateSource(),
331 3
		];
332
	}
333 3
334 3
	/**
335
	 * @return array
336
	 */
337
	public function jsonSerialize(): array
338
	{
339
		return $this->getData();
340
	}
341
342
	/**
343
	 * @param array $data
344
	 *
345
	 * @throws \UnexpectedValueException
346
	 */
347
	public function fromArray(array $data): void
348
	{
349
		if (!isset($data['user_id']) || !isset($data['name']) || !isset($data['site']) || !isset($data['path'])) {
350 3
			throw new \UnexpectedValueException();
351
		}
352 3
353 3
		$options = [];
354
		if (!empty($data['options'])) {
355
			$options = is_array($data['options']) ? $data['options'] : json_decode($data['options'], true);
356
		}
357
358
		$creation = 0;
359
		if (!empty($data['creation'])) {
360
			$creation = is_numeric($data['creation']) ? (int) $data['creation'] : strtotime($data['creation']);
361
		}
362
363
		$this->setId(isset($data['id']) ? (int) $data['id'] : 0)
364
			->setUserId($data['user_id'])
365
			->setName($data['name'])
366
			->setSite($data['site'])
367
			->setTheme($data['theme'] ?? 'default')
368
			->setType(isset($data['type']) ? (int) $data['type'] : self::TYPE_PUBLIC)
369 3
			->setOptions($options)
370
			->setPath($data['path'])
371 3
			->setCreation($creation);
372 3
373
		if (!empty($data['template'])) {
374
			$this->setTemplateSource($data['template']);
375
		}
376
	}
377
}
378