Test Failed
Push — master ( a62dde...6536c0 )
by Justin
60:04 queued 55:59
created

Plugin::getLicense()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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