Test Failed
Push — master ( f2c12e...48c706 )
by Justin
20:00 queued 13:21
created

Plugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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
	/**
66
	 * get directory name of plugin
67
	 *
68
	 * @return string directory name of plugin
69
	 */
70
	public function getName () : string {
71
		return $this->name;
72
	}
73
74
	public function getPath () : string {
75
		return PLUGIN_PATH . $this->name . "/";
76
	}
77
78
	public function getType () : string {
79
		$type = $this->json_data['type'];
80
81
		if (!in_array($type, self::$allowed_types)) {
82
			throw new IllegalStateException("plugin type '" . $type . "' (plugin '" . $this->name . "') is not supported!");
83
		}
84
85
		return $type;
86
	}
87
88
	public function getTitle () : string {
89
		return htmlentities($this->json_data['title']);
90
	}
91
92
	public function getDescription (string $lang_token = "") : string {
93
		$desc = $this->json_data['description'];
94
95
		if (is_array($desc)) {
96
			//several languages are supported
97
			if (empty($lang_token) || !isset($desc[$lang_token])) {
98
				//return english description
99
				return htmlentities($desc['en']);
100
			} else {
101
				return htmlentities($desc[$lang_token]);
102
			}
103
		} else {
104
			//use default language
105
			return htmlentities($desc);
106
		}
107
	}
108
109
	public function getVersion () : string {
110
		return $this->json_data['version'];
111
	}
112
113
	public function getInstalledVersion () : string {
114
		return (!empty($this->row) ? $this->row['version'] : "n/a");
115
	}
116
117
	public function getHomepage () : string {
118
		return (isset($this->json_data['homepage']) ? $this->json_data['homepage'] : "");
119
	}
120
121
	public function getLicense () : string {
122
		return $this->json_data['license'];
123
	}
124
125
	public function listAuthors () : array {
126
		return $this->json_data['authors'];
127
	}
128
129
	public function listSupportArray () : array {
130
		return $this->json_data['support'];
131
	}
132
133
	public function hasSourceLink () : bool {
134
		return isset($this->json_data['support']) && isset($this->json_data['support']['source']);
135
	}
136
137
	public function getSourceLink () : string {
138
		if ($this->hasSourceLink()) {
139
			return $this->json_data['support']['source'];
140
		} else {
141
			return "";
142
		}
143
	}
144
145
	public function hasIssuesLink () : bool {
146
		return isset($this->json_data['support']) && isset($this->json_data['support']['issues']);
147
	}
148
149
	public function getIssuesLink () : string {
150
		if ($this->hasIssuesLink()) {
151
			return $this->json_data['support']['issues'];
152
		} else {
153
			return "";
154
		}
155
	}
156
157
	public function hasSupportMail () : bool {
158
		return isset($this->json_data['support']) && isset($this->json_data['support']['email']);
159
	}
160
161
	public function getSupportMail () : string {
162
		if ($this->hasSupportMail()) {
163
			return $this->json_data['support']['email'];
164
		} else {
165
			return "";
166
		}
167
	}
168
169
	public function listSupportLinks () : array {
170
		$array = array();
171
172
		if ($this->hasIssuesLink()) {
173
			$array[] = array(
174
				'title' => Translator::translate("Issues"),
175
				'href' => $this->getIssuesLink()
176
			);
177
		}
178
179
		if ($this->hasSourceLink()) {
180
			$array[] = array(
181
				'title' => Translator::translate("Source"),
182
				'href' => $this->getSourceLink()
183
			);
184
		}
185
186
		if ($this->hasSupportMail()) {
187
			$array[] = array(
188
				'title' => Translator::translate("Mail"),
189
				'href' => "mailto:" . $this->getSupportMail(),
190
			);
191
		}
192
193
		return $array;
194
	}
195
196
	public function listKeywords () : array {
197
		return $this->json_data['keywords'];
198
	}
199
200
	public function listCategories () : array {
201
		return $this->json_data['categories'];
202
	}
203
204
	public function hasInstallJson () : bool {
205
		return isset($this->json_data['install']);
206
	}
207
208
	public function getInstallJsonFile () : string {
209
		return $this->json_data['install'];
210
	}
211
212
	public function getRequiredPlugins () : array {
213
		return $this->json_data['require'];
214
	}
215
216
	public function isInstalled () : bool {
217
		return (!empty($this->row) ? $this->row['installed'] == 1 : false);
218
	}
219
220
	public function isActivated () : bool {
221
		return (!empty($this->row) ? $this->row['activated'] == 1 : false);
222
	}
223
224
	public static function castPlugin (Plugin $plugin) : Plugin {
225
		return $plugin;
226
	}
227
228
}
229
230
?>
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...
231