Passed
Pull Request — master (#77)
by Daniel
47:29
created

Theme   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 68.18%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 44
c 1
b 0
f 1
dl 0
loc 128
ccs 30
cts 44
cp 0.6818
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isCompatible() 0 11 3
A getName() 0 3 1
A __construct() 0 4 1
A checkCompatibility() 0 24 5
A getType() 0 3 1
A toArray() 0 14 3
A jsonSerialize() 0 3 1
A getFolder() 0 3 1
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Model;
26
27
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException;
28
use OCA\CMSPico\Files\LocalFolder;
29
30
class Theme implements \JsonSerializable
31
{
32
	/** @var int */
33
	const THEME_TYPE_SYSTEM = 1;
34
35
	/** @var int */
36
	const THEME_TYPE_CUSTOM = 2;
37
38
	/** @var LocalFolder */
39
	private $folder;
40
41
	/** @var int */
42
	private $type;
43
44
	/** @var bool|null */
45
	private $compat;
46
47
	/** @var ThemeNotCompatibleException|null */
48
	private $compatException;
49
50
	/**
51
	 * Plugin constructor.
52
	 *
53
	 * @param LocalFolder $folder
54
	 * @param int         $type
55
	 */
56 1
	public function __construct(LocalFolder $folder, int $type = self::THEME_TYPE_SYSTEM)
57
	{
58 1
		$this->folder = $folder;
59 1
		$this->type = $type;
60 1
	}
61
62
	/**
63
	 * @return string
64
	 */
65 1
	public function getName(): string
66
	{
67 1
		return $this->folder->getName();
68
	}
69
70
	/**
71
	 * @return LocalFolder
72
	 */
73 1
	public function getFolder(): LocalFolder
74
	{
75 1
		return $this->folder;
76
	}
77
78
	/**
79
	 * @return int
80
	 */
81 1
	public function getType(): int
82
	{
83 1
		return $this->type;
84
	}
85
86
	/**
87
	 * @return bool
88
	 */
89 1
	public function isCompatible(): bool
90
	{
91 1
		if ($this->compat !== null) {
92 1
			return $this->compat;
93
		}
94
95
		try {
96 1
			$this->checkCompatibility();
97 1
			return true;
98
		} catch (ThemeNotCompatibleException $e) {
99
			return false;
100
		}
101
	}
102
103
	/**
104
	 * @throws ThemeNotCompatibleException
105
	 */
106 1
	public function checkCompatibility()
107
	{
108 1
		if ($this->compat === false) {
109
			throw $this->compatException;
110 1
		} elseif ($this->compat) {
111
			return;
112
		}
113
114
		try {
115 1
			if (!is_file($this->getFolder()->getLocalPath() . '/index.twig')) {
116
				throw new ThemeNotCompatibleException(
117
					$this->getName(),
118
					'Incompatible theme: Twig template "{file}" not found.',
119
					[ 'file' => $this->getName() . '/index.twig' ]
120
				);
121
			}
122
123 1
			$this->compat = true;
124 1
			$this->compatException = null;
125
		} catch (ThemeNotCompatibleException $e) {
126
			$this->compat = false;
127
			$this->compatException = $e;
128
129
			throw $e;
130
		}
131 1
	}
132
133
	/**
134
	 * @return array
135
	 */
136 1
	public function toArray(): array
137
	{
138
		$data = [
139 1
			'name' => $this->getName(),
140 1
			'type' => $this->getType(),
141 1
			'compat' => $this->isCompatible(),
142
		];
143
144 1
		if (!$this->isCompatible() && ($this->compatException !== null)) {
145
			$data['compatReason'] = $this->compatException->getRawReason();
146
			$data['compatReasonData'] = $this->compatException->getRawReasonData();
147
		}
148
149 1
		return $data;
150
	}
151
152
	/**
153
	 * @return array
154
	 */
155 1
	public function jsonSerialize(): array
156
	{
157 1
		return $this->toArray();
158
	}
159
}
160