Plugin::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 14
c 0
b 0
f 0
ccs 6
cts 8
cp 0.75
rs 10
cc 3
nc 2
nop 0
crap 3.1406
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\PluginNotCompatibleException;
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 ParseError;
34
use function OCA\CMSPico\t;
35
36
class Plugin implements \JsonSerializable
37
{
38
	/** @var int */
39
	public const TYPE_SYSTEM = 1;
40
41
	/** @var int */
42
	public const TYPE_CUSTOM = 2;
43
44
	/** @var int[] */
45
	public const PLUGIN_API_VERSIONS = [
46
		Pico::API_VERSION_1,
47
		Pico::API_VERSION_2,
48
		Pico::API_VERSION_3,
49
	];
50
51
	/** @var MiscService */
52
	private $miscService;
53
54
	/** @var FolderInterface */
55
	private $folder;
56
57
	/** @var int */
58
	private $type;
59
60
	/** @var bool|null */
61
	private $compat;
62
63
	/** @var PluginNotCompatibleException|null */
64
	private $compatException;
65
66
	/**
67
	 * Plugin constructor.
68
	 *
69
	 * @param FolderInterface $folder
70
	 * @param int             $type
71
	 */
72 3
	public function __construct(FolderInterface $folder, int $type = self::TYPE_SYSTEM)
73
	{
74 3
		$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

74
		$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...
75
76 3
		$this->folder = $folder;
77 3
		$this->type = $type;
78 3
	}
79
80
	/**
81
	 * @return string
82
	 */
83 3
	public function getName(): string
84
	{
85 3
		return $this->folder->getName();
86
	}
87
88
	/**
89
	 * @return FolderInterface
90
	 */
91 3
	public function getFolder(): FolderInterface
92
	{
93 3
		return $this->folder;
94
	}
95
96
	/**
97
	 * @return int
98
	 */
99 3
	public function getType(): int
100
	{
101 3
		return $this->type;
102
	}
103
104
	/**
105
	 * @return bool
106
	 */
107 3
	public function isCompatible(): bool
108
	{
109 3
		if ($this->compat !== null) {
110 3
			return $this->compat;
111
		}
112
113
		try {
114 3
			$this->checkCompatibility();
115 3
			return true;
116
		} catch (PluginNotCompatibleException $e) {
117
			return false;
118
		}
119
	}
120
121
	/**
122
	 * @throws PluginNotCompatibleException
123
	 */
124 3
	public function checkCompatibility(): void
125
	{
126 3
		if ($this->compat === false) {
127
			throw $this->compatException;
128 3
		} elseif ($this->compat) {
129
			return;
130
		}
131
132
		$includeClosure = static function (string $pluginFile) {
133
			/** @noinspection PhpIncludeInspection */
134 3
			require_once($pluginFile);
135 3
		};
136
137
		try {
138
			try {
139 3
				$pluginFile = $this->getFolder()->getFile($this->getName() . '.php');
140 3
				$includeClosure($pluginFile->getLocalPath());
141
			} catch (InvalidPathException | NotFoundException $e) {
142
				throw new PluginNotCompatibleException(
143
					$this->getName(),
144
					t('Incompatible plugin: Plugin file "{file}" not found.'),
145
					[ 'file' => $this->getName() . '/' . $this->getName() . '.php' ]
146
				);
147
			} catch (ParseError $e) {
148
				throw new PluginNotCompatibleException(
149
					$this->getName(),
150
					t('Incompatible plugin: PHP parse error in file "{file}".'),
151
					[ 'file' => $this->getName() . '/' . $this->getName() . '.php' ]
152
				);
153
			}
154
155 3
			$className = $this->getName();
156 3
			if (!class_exists($className, false)) {
157
				throw new PluginNotCompatibleException(
158
					$this->getName(),
159
					t('Incompatible plugin: Plugin class "{class}" not found.'),
160
					[ 'class' => $className ]
161
				);
162
			}
163
164 3
			$apiVersion = Pico::API_VERSION_0;
165 3
			if (is_a($className, \PicoPluginInterface::class, true)) {
166 3
				$apiVersion = Pico::API_VERSION_1;
167 3
				if (defined($className . '::API_VERSION')) {
168
					/** @noinspection PhpUndefinedFieldInspection */
169
					$apiVersion = (int) $className::API_VERSION;
170
				}
171
			}
172
173 3
			if (!in_array($apiVersion, static::PLUGIN_API_VERSIONS, true)) {
174
				throw new PluginNotCompatibleException(
175
					$this->getName(),
176
					t('Incompatible plugin: Plugins for Pico CMS for Nextcloud must use one of the API versions '
177
							. '{compatApiVersions}, but this plugin uses API version {apiVersion}.'),
178
					[ 'compatApiVersions' => implode(', ', static::PLUGIN_API_VERSIONS), 'apiVersion' => $apiVersion ]
179
				);
180
			}
181
182 3
			$this->compat = true;
183 3
			$this->compatException = null;
184
		} catch (PluginNotCompatibleException $e) {
185
			$this->compat = false;
186
			$this->compatException = $e;
187
188
			throw $e;
189
		}
190 3
	}
191
192
	/**
193
	 * @return array
194
	 */
195 3
	public function toArray(): array
196
	{
197
		$data = [
198 3
			'name' => $this->getName(),
199 3
			'type' => $this->getType(),
200 3
			'compat' => $this->isCompatible(),
201
		];
202
203 3
		if (!$this->isCompatible() && ($this->compatException !== null)) {
204
			$data['compatReason'] = $this->compatException->getRawReason();
205
			$data['compatReasonData'] = $this->compatException->getRawReasonData();
206
		}
207
208 3
		return $data;
209
	}
210
211
	/**
212
	 * @return array
213
	 */
214 3
	public function jsonSerialize(): array
215
	{
216 3
		return $this->toArray();
217
	}
218
}
219