Test Failed
Push — master ( 35d41d...0e5acb )
by Justin
27:48 queued 23:23
created

PluginInstaller   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 19
c 4
b 0
f 2
dl 0
loc 102
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
C checkVersion() 0 33 7
C checkRequirements() 0 52 11
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: 14:45
26
 */
27
28
class PluginInstaller {
29
30
	//plugin to install / deinstall
31
	protected $plugin = null;
32
33
	public function __construct(Plugin $plugin) {
34
		$this->plugin = $plugin;
35
	}
36
37
	/**
38
	 * check php version, php extensions and so on
39
	 *
40
	 * @return mixed true, if all required plugins are available, or an array with missing
41
	 */
42
	public function checkRequirements (bool $dontCheckPlugins = false) {
43
		//get require
44
		$require_array = $this->plugin->getRequiredPlugins();
45
46
		//get package list
47
		require(STORE_PATH . "package_list.php");
48
49
		$missing_plugins = array();
50
51
		//iterate through all requirements
52
		foreach ($require_array as $requirement=>$version) {
53
			if ($requirement === "php") {
54
				//TODO: check php version
55
			} else if (PHPUtils::startsWith($requirement, "ext-")) {
56
				//check php extension
57
			} else if (PHPUtils::startsWith($requirement, "package-")) {
58
				//TODO: check if package is installed
59
				$package = str_replace("package-", "", $requirement);
60
61
				//packages doesnt supports specific version
62
63
				if (!isset($package_list[$package])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $package_list does not exist. Did you maybe mean $package?
Loading history...
64
					$missing_plugins[] = $requirement;
65
				}
66
			} else if ($requirement === "core") {
67
				//TODO: check core version
68
69
				if ($version === "*") {
70
					//we dont have to check version
71
				} else {
72
					//get current version
73
					$array = explode(" ", Version::current()->getVersion());
74
					$current_core_version = $array[0];
75
76
					//check version
77
					if (!$this->checkVersion($version, $current_core_version)) {
78
						$missing_plugins[] = "core";
79
					}
80
				}
81
			} else {
82
				if (!$dontCheckPlugins) {
83
					continue;
84
				}
85
86
				//TODO: check installed plugins
87
			}
88
		}
89
90
		if (empty($missing_plugins)) {
91
			return true;
92
		} else {
93
			return $missing_plugins;
94
		}
95
	}
96
97
	protected function checkVersion (string $expected_version, $current_version) : bool {
98
		//check version
99
		if (is_numeric($expected_version)) {
100
			//a specific version is required
101
			if ($current_version !== $expected_version) {
102
				return false;
103
			} else {
104
				return true;
105
			}
106
		} else if ($expected_version === "*") {
107
			//every version is allowed
108
			return true;
109
		} else {
110
			//parse version string
111
112
			$operator_length = 0;
113
114
			for ($i = 0; $i < strlen($expected_version); $i++) {
115
				if (!is_numeric($expected_version[$i])) {
116
					$operator_length++;
117
				} else {
118
					break;
119
				}
120
			}
121
122
			//get operator and version
123
			$operator = substr($expected_version, 0, $operator_length);
124
			$version = substr($expected_version, $operator_length);
0 ignored issues
show
Unused Code introduced by
The assignment to $version is dead and can be removed.
Loading history...
125
126
			if (!empty($operator_length)) {
127
				return version_compare($current_version, $expected_version, $operator);
128
			} else {
129
				return version_compare($current_version, $expected_version);
130
			}
131
		}
132
	}
133
134
}
135
136
?>
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...
137