Template   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 57.69%

Importance

Changes 0
Metric Value
wmc 17
eloc 55
dl 0
loc 145
ccs 30
cts 52
cp 0.5769
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 14 3
A getName() 0 3 1
A __construct() 0 6 1
A isCompatible() 0 11 3
A jsonSerialize() 0 3 1
A getFolder() 0 3 1
B checkCompatibility() 0 36 6
A getType() 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\TemplateNotCompatibleException;
28
use OCA\CMSPico\Files\FolderInterface;
29
use OCA\CMSPico\Service\MiscService;
30
use OCP\Files\InvalidPathException;
31
use OCP\Files\NotFoundException;
32
use function OCA\CMSPico\t;
33
34
class Template implements \JsonSerializable
35
{
36
	/** @var int */
37
	public const TYPE_SYSTEM = 1;
38
39
	/** @var int */
40
	public const TYPE_CUSTOM = 2;
41
42
	/** @var MiscService */
43
	private $miscService;
44
45
	/** @var FolderInterface */
46
	private $folder;
47
48
	/** @var int */
49
	private $type;
50
51
	/** @var bool|null */
52
	private $compat;
53
54
	/** @var TemplateNotCompatibleException|null */
55
	private $compatException;
56
57
	/**
58
	 * Template constructor.
59
	 *
60
	 * @param FolderInterface $folder
61
	 * @param int             $type
62
	 */
63 2
	public function __construct(FolderInterface $folder, int $type = self::TYPE_SYSTEM)
64
	{
65 2
		$this->miscService = \OC::$server->query(MiscService::class);
0 ignored issues
show
Deprecated Code introduced by
The function OC\ServerContainer::query() has been deprecated: 20.0.0 use \Psr\Container\ContainerInterface::get ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

65
		$this->miscService = /** @scrutinizer ignore-deprecated */ \OC::$server->query(MiscService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66
67 2
		$this->folder = $folder;
68 2
		$this->type = $type;
69 2
	}
70
71
	/**
72
	 * @return string
73
	 */
74 2
	public function getName(): string
75
	{
76 2
		return $this->folder->getName();
77
	}
78
79
	/**
80
	 * @return FolderInterface
81
	 */
82
	public function getFolder(): FolderInterface
83
	{
84
		return $this->folder;
85
	}
86
87
	/**
88
	 * @return int
89
	 */
90 2
	public function getType(): int
91
	{
92 2
		return $this->type;
93
	}
94
95
	/**
96
	 * @return bool
97
	 */
98 2
	public function isCompatible(): bool
99
	{
100 2
		if ($this->compat !== null) {
101 2
			return $this->compat;
102
		}
103
104
		try {
105 2
			$this->checkCompatibility();
106 2
			return true;
107
		} catch (TemplateNotCompatibleException $e) {
108
			return false;
109
		}
110
	}
111
112
	/**
113
	 * @throws TemplateNotCompatibleException
114
	 */
115 2
	public function checkCompatibility(): void
116
	{
117 2
		if ($this->compat === false) {
118
			throw $this->compatException;
119 2
		} elseif ($this->compat) {
120
			return;
121
		}
122
123
		try {
124
			try {
125 2
				$this->folder->getFolder('assets');
126
			} catch (InvalidPathException | NotFoundException $e) {
127
				throw new TemplateNotCompatibleException(
128
					$this->getName(),
129
					t('Incompatible template: Required directory "{file}" not found.'),
130
					[ 'file' => $this->getName() . '/assets/' ]
131
				);
132
			}
133
134
			try {
135 2
				$this->folder->getFolder('content');
136
			} catch (InvalidPathException | NotFoundException $e) {
137
				throw new TemplateNotCompatibleException(
138
					$this->getName(),
139
					t('Incompatible template: Required directory "{file}" not found.'),
140
					[ 'file' => $this->getName() . '/content/' ]
141
				);
142
			}
143
144 2
			$this->compat = true;
145 2
			$this->compatException = null;
146
		} catch (TemplateNotCompatibleException $e) {
147
			$this->compat = false;
148
			$this->compatException = $e;
149
150
			throw $e;
151
		}
152 2
	}
153
154
	/**
155
	 * @return array
156
	 */
157 2
	public function toArray(): array
158
	{
159
		$data = [
160 2
			'name' => $this->getName(),
161 2
			'type' => $this->getType(),
162 2
			'compat' => $this->isCompatible(),
163
		];
164
165 2
		if (!$this->isCompatible() && ($this->compatException !== null)) {
166
			$data['compatReason'] = $this->compatException->getRawReason();
167
			$data['compatReasonData'] = $this->compatException->getRawReasonData();
168
		}
169
170 2
		return $data;
171
	}
172
173
	/**
174
	 * @return array
175
	 */
176 2
	public function jsonSerialize(): array
177
	{
178 2
		return $this->toArray();
179
	}
180
}
181