Passed
Pull Request — master (#99)
by Daniel
26:12 queued 03:19
created

Template::checkCompatibility()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 18.1218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 36
ccs 7
cts 23
cp 0.3043
rs 8.8977
cc 6
nc 7
nop 0
crap 18.1218
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\TemplateNotCompatibleException;
28
use OCA\CMSPico\Files\FolderInterface;
29
use OCA\CMSPico\Service\MiscService;
30
use OCP\Files\InvalidPathException;
31
use OCP\Files\NotFoundException;
32
33
class Template implements \JsonSerializable
34
{
35
	/** @var int */
36
	public const TYPE_SYSTEM = 1;
37
38
	/** @var int */
39
	public const TYPE_CUSTOM = 2;
40
41
	/** @var MiscService */
42
	private $miscService;
43
44
	/** @var FolderInterface */
45
	private $folder;
46
47
	/** @var int */
48
	private $type;
49
50
	/** @var bool|null */
51
	private $compat;
52
53
	/** @var TemplateNotCompatibleException|null */
54
	private $compatException;
55
56
	/**
57
	 * Template constructor.
58
	 *
59
	 * @param FolderInterface $folder
60
	 * @param int             $type
61
	 */
62 1
	public function __construct(FolderInterface $folder, int $type = self::TYPE_SYSTEM)
63
	{
64 1
		$this->miscService = \OC::$server->query(MiscService::class);
65
66 1
		$this->folder = $folder;
67 1
		$this->type = $type;
68 1
	}
69
70
	/**
71
	 * @return string
72
	 */
73 1
	public function getName(): string
74
	{
75 1
		return $this->folder->getName();
76
	}
77
78
	/**
79
	 * @return FolderInterface
80
	 */
81
	public function getFolder(): FolderInterface
82
	{
83
		return $this->folder;
84
	}
85
86
	/**
87
	 * @return int
88
	 */
89 1
	public function getType(): int
90
	{
91 1
		return $this->type;
92
	}
93
94
	/**
95
	 * @return bool
96
	 */
97 1
	public function isCompatible(): bool
98
	{
99 1
		if ($this->compat !== null) {
100 1
			return $this->compat;
101
		}
102
103
		try {
104 1
			$this->checkCompatibility();
105 1
			return true;
106
		} catch (TemplateNotCompatibleException $e) {
107
			return false;
108
		}
109
	}
110
111
	/**
112
	 * @throws TemplateNotCompatibleException
113
	 */
114 1
	public function checkCompatibility(): void
115
	{
116 1
		if ($this->compat === false) {
117
			throw $this->compatException;
118 1
		} elseif ($this->compat) {
119
			return;
120
		}
121
122
		try {
123
			try {
124 1
				$this->folder->getFolder('assets');
125
			} catch (InvalidPathException | NotFoundException $e) {
126
				throw new TemplateNotCompatibleException(
127
					$this->getName(),
128
					'Incompatible template: Required directory "{file}" not found.',
129
					[ 'file' => $this->getName() . '/assets/' ]
130
				);
131
			}
132
133
			try {
134 1
				$this->folder->getFolder('content');
135
			} catch (InvalidPathException | NotFoundException $e) {
136
				throw new TemplateNotCompatibleException(
137
					$this->getName(),
138
					'Incompatible template: Required directory "{file}" not found.',
139
					[ 'file' => $this->getName() . '/content/' ]
140
				);
141
			}
142
143 1
			$this->compat = true;
144 1
			$this->compatException = null;
145
		} catch (TemplateNotCompatibleException $e) {
146
			$this->compat = false;
147
			$this->compatException = $e;
148
149
			throw $e;
150
		}
151 1
	}
152
153
	/**
154
	 * @return array
155
	 */
156 1
	public function toArray(): array
157
	{
158
		$data = [
159 1
			'name' => $this->getName(),
160 1
			'type' => $this->getType(),
161 1
			'compat' => $this->isCompatible(),
162
		];
163
164 1
		if (!$this->isCompatible() && ($this->compatException !== null)) {
165
			$data['compatReason'] = $this->compatException->getRawReason();
166
			$data['compatReasonData'] = $this->compatException->getRawReasonData();
167
		}
168
169 1
		return $data;
170
	}
171
172
	/**
173
	 * @return array
174
	 */
175 1
	public function jsonSerialize(): array
176
	{
177 1
		return $this->toArray();
178
	}
179
}
180