Passed
Push — master ( fa41e0...4bd63f )
by Daniel
07:07 queued 12s
created

WebsiteCore::getOptionsJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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