Test Failed
Push — master ( c86ad9...7f1f0b )
by Justin
09:47 queued 04:55
created

Plugin::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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 $this->row['version'];
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 isInstalled () : bool {
126
		return (!empty($this->row) ? $this->row['installed'] == 1 : false);
127
	}
128
129
	public function isActivated () : bool {
130
		return $this->row['activated'] == 1;
131
	}
132
133
	public static function castPlugin (Plugin $plugin) : Plugin {
134
		return $plugin;
135
	}
136
137
}
138
139
?>
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...
140