Test Failed
Push — master ( e34111...40eb7a )
by Daniel
21:12
created

Theme::getFolder()   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 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
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) 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\FolderInterface;
29
use OCA\CMSPico\Pico;
30
use OCA\CMSPico\Service\MiscService;
31
use OCP\Files\InvalidPathException;
32
use OCP\Files\NotFoundException;
33
use OCP\Files\NotPermittedException;
34
use Symfony\Component\Yaml\Exception\ParseException as YamlParseException;
35
use Symfony\Component\Yaml\Parser as YamlParser;
36
use function OCA\CMSPico\t;
37
38
class Theme implements \JsonSerializable
39
{
40
	/** @var int */
41
	public const TYPE_SYSTEM = 1;
42
43
	/** @var int */
44
	public const TYPE_CUSTOM = 2;
45
46
	/** @var int[] */
47
	public const THEME_API_VERSIONS = [
48
		Pico::API_VERSION_0,
49
		Pico::API_VERSION_1,
50
		Pico::API_VERSION_2,
51
		Pico::API_VERSION_3,
52
	];
53
54
	/** @var MiscService */
55
	private $miscService;
56
57
	/** @var FolderInterface */
58
	private $folder;
59
60
	/** @var int */
61
	private $type;
62
63
	/** @var bool|null */
64
	private $compat;
65
66
	/** @var ThemeNotCompatibleException|null */
67
	private $compatException;
68
69
	/**
70
	 * Theme constructor.
71
	 *
72
	 * @param FolderInterface $folder
73
	 * @param int             $type
74
	 */
75 1
	public function __construct(FolderInterface $folder, int $type = self::TYPE_SYSTEM)
76
	{
77 1
		$this->miscService = \OC::$server->query(MiscService::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like OC::server->query(OCA\CM...ice\MiscService::class) can also be of type stdClass. However, the property $miscService is declared as type OCA\CMSPico\Service\MiscService. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
78
79 1
		$this->folder = $folder;
80 1
		$this->type = $type;
81 1
	}
82
83
	/**
84
	 * @return string
85
	 */
86 1
	public function getName(): string
87
	{
88 1
		return $this->folder->getName();
89
	}
90
91
	/**
92
	 * @return FolderInterface
93
	 */
94 1
	public function getFolder(): FolderInterface
95
	{
96 1
		return $this->folder;
97
	}
98
99
	/**
100
	 * @return int
101
	 */
102 1
	public function getType(): int
103
	{
104 1
		return $this->type;
105
	}
106
107
	/**
108
	 * @return bool
109
	 */
110 1
	public function isCompatible(): bool
111
	{
112 1
		if ($this->compat !== null) {
113 1
			return $this->compat;
114
		}
115
116
		try {
117 1
			$this->checkCompatibility();
118 1
			return true;
119
		} catch (ThemeNotCompatibleException $e) {
120
			return false;
121
		}
122
	}
123
124
	/**
125
	 * @throws ThemeNotCompatibleException
126
	 */
127 1
	public function checkCompatibility(): void
128
	{
129 1
		if ($this->compat === false) {
130
			throw $this->compatException;
131 1
		} elseif ($this->compat) {
132
			return;
133
		}
134
135
		try {
136
			try {
137 1
				$this->getFolder()->getFile('index.twig');
138
			} catch (InvalidPathException | NotFoundException $e) {
139
				throw new ThemeNotCompatibleException(
140
					$this->getName(),
141
					t('Incompatible theme: Twig template "{file}" not found.'),
142
					[ 'file' => $this->getName() . '/index.twig' ]
143
				);
144
			}
145
146
			try {
147 1
				$themeConfigFile = $this->getFolder()->getFile('pico-theme.yml');
148
				$themeConfigYaml = $themeConfigFile->getContent();
149
150
				$themeConfig = (new YamlParser())->parse($themeConfigYaml);
151
				$themeConfig = is_array($themeConfig) ? $themeConfig : [];
152 1
			} catch (InvalidPathException | NotFoundException | NotPermittedException | YamlParseException $e) {
153 1
				$themeConfig = [];
154
			}
155
156 1
			$apiVersion = Pico::API_VERSION_0;
157 1
			if (isset($themeConfig['api_version'])) {
158
				if (is_int($themeConfig['api_version']) || preg_match('/^[0-9]+$/', $themeConfig['api_version'])) {
159
					$apiVersion = (int)$themeConfig['api_version'];
160
				}
161
			}
162
163 1
			if (!in_array($apiVersion, static::THEME_API_VERSIONS, true)) {
164
				throw new ThemeNotCompatibleException(
165
					$this->getName(),
166
					t('Incompatible theme: Themes for Pico CMS for Nextcloud must use one of the API versions '
167
							. '{compatApiVersions}, but this theme uses API version {apiVersion}.'),
168
					[ 'compatApiVersions' => implode(', ', static::THEME_API_VERSIONS), 'apiVersion' => $apiVersion ]
169
				);
170
			}
171
172 1
			$this->compat = true;
173 1
			$this->compatException = null;
174
		} catch (ThemeNotCompatibleException $e) {
175
			$this->compat = false;
176
			$this->compatException = $e;
177
178
			throw $e;
179
		}
180 1
	}
181
182
	/**
183
	 * @return array
184
	 */
185 1
	public function toArray(): array
186
	{
187
		$data = [
188 1
			'name' => $this->getName(),
189 1
			'type' => $this->getType(),
190 1
			'compat' => $this->isCompatible(),
191
		];
192
193 1
		if (!$this->isCompatible() && ($this->compatException !== null)) {
194
			$data['compatReason'] = $this->compatException->getRawReason();
195
			$data['compatReasonData'] = $this->compatException->getRawReasonData();
196
		}
197
198 1
		return $data;
199
	}
200
201
	/**
202
	 * @return array
203
	 */
204 1
	public function jsonSerialize(): array
205
	{
206 1
		return $this->toArray();
207
	}
208
}
209