Test Failed
Push — master ( a73431...1c8a87 )
by Justin
52:02 queued 47:13
created

Plugin::getDescription()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: JuKuCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 08.04.2018
25
 * Time: 12:31
26
 */
27
28
class Plugin {
29
30
	//directory name of plugin
31
	protected $name = "";
32
33
	//database row
34
	protected $row = array();
35
36
	protected $json_data = null;
37
38
	protected static $allowed_types = array("library", "metaplugin", "project");
39
40
	/**
41
	 * default constructor
42
	 *
43
	 * @param string $name directory name of plugin
44
	 * @param array $row optional database row from plugin
45
	 */
46
	public function __construct(string $name, array $row = array()) {
47
		$this->name = $name;
48
		$this->row = $row;
49
	}
50
51
	/**
52
	 * load plugin.json file
53
	 */
54
	public function load () {
55
		$file_path = PLUGIN_PATH . $this->name . "/plugin.json";
56
57
		//check, if file exists
58
		if (!file_exists($file_path)) {
59
			throw new IllegalStateException("plugin.json for plugin '" . $this->name . "' does not exists (expected path: '" . $file_path . "')!");
60
		}
61
62
		$this->json_data = json_decode(file_get_contents($file_path), true);
63
	}
64
65
	public function loadRow () {
66
		if (Cache::contains("plugins", "plugin_row_" . $this->name)) {
67
			$this->row = Cache::get("plugins", "plugin_row_" . $this->name);
68
		} else {
69
			$row = Database::getInstance()->getRow("SELECT * FROM `{praefix}plugins` WHERE `name` = :name; ");
70
71
			Cache::put("plugins", "plugin_row_" . $this->name, $row);
72
73
			$this->row = $row;
74
		}
75
	}
76
77
	/**
78
	 * get directory name of plugin
79
	 *
80
	 * @return string directory name of plugin
81
	 */
82
	public function getName () : string {
83
		return $this->name;
84
	}
85
86
	public function getPath () : string {
87
		return PLUGIN_PATH . $this->name . "/";
88
	}
89
90
	public function exists () : bool {
91
		$file_path = PLUGIN_PATH . $this->name . "/plugin.json";
92
93
		return file_exists($file_path);
94
	}
95
96
	public function getType () : string {
97
		$type = $this->json_data['type'];
98
99
		if (!in_array($type, self::$allowed_types)) {
100
			throw new IllegalStateException("plugin type '" . $type . "' (plugin '" . $this->name . "') is not supported!");
101
		}
102
103
		return $type;
104
	}
105
106
	public function getTitle () : string {
107
		return htmlentities($this->json_data['title']);
108
	}
109
110
	public function getDescription (string $lang_token = "") : string {
111
		$desc = $this->json_data['description'];
112
113
		if (is_array($desc)) {
114
			//several languages are supported
115
			if (empty($lang_token) || !isset($desc[$lang_token])) {
116
				//return english description
117
				return htmlentities($desc['en']);
118
			} else {
119
				return htmlentities($desc[$lang_token]);
120
			}
121
		} else {
122
			//use default language
123
			return htmlentities($desc);
124
		}
125
	}
126
127
	public function getVersion () : string {
128
		return $this->json_data['version'];
129
	}
130
131
	public function getInstalledVersion () : string {
132
		return (!empty($this->row) ? $this->row['version'] : "n/a");
133
	}
134
135
	public function getHomepage () : string {
136
		return (isset($this->json_data['homepage']) ? $this->json_data['homepage'] : "");
137
	}
138
139
	public function getLicense () : string {
140
		return $this->json_data['license'];
141
	}
142
143
	public function listAuthors () : array {
144
		return $this->json_data['authors'];
145
	}
146
147
	public function listSupportArray () : array {
148
		return $this->json_data['support'];
149
	}
150
151
	public function hasSourceLink () : bool {
152
		return isset($this->json_data['support']) && isset($this->json_data['support']['source']);
153
	}
154
155
	public function getSourceLink () : string {
156
		if ($this->hasSourceLink()) {
157
			return $this->json_data['support']['source'];
158
		} else {
159
			return "";
160
		}
161
	}
162
163
	public function hasIssuesLink () : bool {
164
		return isset($this->json_data['support']) && isset($this->json_data['support']['issues']);
165
	}
166
167
	public function getIssuesLink () : string {
168
		if ($this->hasIssuesLink()) {
169
			return $this->json_data['support']['issues'];
170
		} else {
171
			return "";
172
		}
173
	}
174
175
	public function hasSupportMail () : bool {
176
		return isset($this->json_data['support']) && isset($this->json_data['support']['email']);
177
	}
178
179
	public function getSupportMail () : string {
180
		if ($this->hasSupportMail()) {
181
			return $this->json_data['support']['email'];
182
		} else {
183
			return "";
184
		}
185
	}
186
187
	public function listSupportLinks () : array {
188
		$array = array();
189
190
		if ($this->hasIssuesLink()) {
191
			$array[] = array(
192
				'title' => Translator::translate("Issues"),
193
				'href' => $this->getIssuesLink()
194
			);
195
		}
196
197
		if ($this->hasSourceLink()) {
198
			$array[] = array(
199
				'title' => Translator::translate("Source"),
200
				'href' => $this->getSourceLink()
201
			);
202
		}
203
204
		if ($this->hasSupportMail()) {
205
			$array[] = array(
206
				'title' => Translator::translate("Mail"),
207
				'href' => "mailto:" . $this->getSupportMail(),
208
			);
209
		}
210
211
		return $array;
212
	}
213
214
	public function listKeywords () : array {
215
		return $this->json_data['keywords'];
216
	}
217
218
	public function listCategories () : array {
219
		return $this->json_data['categories'];
220
	}
221
222
	public function hasInstallJson () : bool {
223
		return isset($this->json_data['installation']) && $this->json_data['installation'] == true;
224
	}
225
226
	public function getInstallJsonFile () : string {
227
		return "install.json";
228
	}
229
230
	public function getRequiredPlugins () : array {
231
		return $this->json_data['require'];
232
	}
233
234
	public function isAlpha () : bool {
235
		return PHPUtils::endsWith($this->getVersion(), "-alpha");
236
	}
237
238
	public function isBeta () : bool {
239
		return PHPUtils::endsWith($this->getVersion(), "-beta");
240
	}
241
242
	public function isInstalled () : bool {
243
		if (empty($this->row)) {
244
			throw new IllegalStateException("plugin instance wasnt loaded with row.");
245
		}
246
247
		return (!empty($this->row) ? $this->row['installed'] == 1 : false);
248
	}
249
250
	public function isUpgradeAvailable () : bool {
251
		//check, if local and installed version are different
252
		return $this->getVersion() !== $this->getInstalledVersion();
253
	}
254
255
	public function isActivated () : bool {
256
		return (!empty($this->row) ? $this->row['activated'] == 1 : false);
257
	}
258
259
	public static function castPlugin (Plugin $plugin) : Plugin {
260
		return $plugin;
261
	}
262
263
}
264
265
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
266