Passed
Push — release-3.0.0 ( e3b06e...069487 )
by Daniel
02:55
created

ext::version_is_ok()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 18
rs 9.9666
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content;
11
12
class ext extends \phpbb\extension\base
13
{
14
	/* @var \phpbb\user */
15
	protected $user;
16
17
	/* @var array */
18
	protected $required_extensions = [
19
		'blitze/sitemaker' => 'https://www.phpbb.com/customise/db/extension/phpbb_sitemaker_2'
20
	];
21
22
	/**
23
	 * Check whether or not the extension can be enabled.
24
	 *
25
	 * @return bool
26
	 */
27
	public function is_enableable()
28
	{
29
		$this->user = $this->container->get('user');
30
		$this->user->add_lang_ext('blitze/content', 'ext');
31
32
		$lang = $this->user->lang;
33
		$metadata = $this->get_metadata('blitze/content');
34
35
		$is_enableable = $this->phpbb_version_is_ok($lang, $metadata) && $this->required_exts_are_ok($lang, $metadata);
36
37
		$this->user->lang = $lang;
38
		return $is_enableable;
39
	}
40
41
	/**
42
	 * Check whether or not this extension can be enabled on the current phpBB version.
43
	 *
44
	 * @param array $lang
45
	 * @param array $metadata
46
	 * @return bool
47
	 */
48
	protected function phpbb_version_is_ok(array &$lang, array $metadata)
49
	{
50
		$req_phpbb_version = $metadata['extra']['soft-require']['phpbb/phpbb'];
51
		$phpbb_version = $this->container->get('config')['version'];
52
53
		if (!$this->version_is_ok($phpbb_version, $req_phpbb_version))
54
		{
55
			$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . $this->user->lang('PHPBB_VERSION_UNMET', $req_phpbb_version);
0 ignored issues
show
Deprecated Code introduced by
The function phpbb\user::lang() has been deprecated: 3.2.0-dev (To be removed 4.0.0) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
			$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . /** @scrutinizer ignore-deprecated */ $this->user->lang('PHPBB_VERSION_UNMET', $req_phpbb_version);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
56
			return false;
57
		}
58
59
		return true;
60
	}
61
62
	/**
63
	 * Check whether or not the extension's phpBB extension dependencies are available and installed.
64
	 *
65
	 * @param array $lang
66
	 * @param array $metadata
67
	 * @return bool
68
	 */
69
	protected function required_exts_are_ok(array &$lang, array $metadata)
70
	{
71
		foreach ($this->required_extensions as $extension => $download_url)
72
		{
73
			if (!$this->container->get('ext.manager')->is_available($extension))
74
			{
75
				$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . $this->user->lang('MISSING_REQUIRED_EXTENSION', $extension, $download_url);
0 ignored issues
show
Deprecated Code introduced by
The function phpbb\user::lang() has been deprecated: 3.2.0-dev (To be removed 4.0.0) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
				$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . /** @scrutinizer ignore-deprecated */ $this->user->lang('MISSING_REQUIRED_EXTENSION', $extension, $download_url);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
				return false;
77
			}
78
79
			$required_version = $metadata['require'][$extension];
80
			$current_version = $this->get_metadata($extension, 'version');
81
82
			if (!$this->version_is_ok($current_version, $required_version))
0 ignored issues
show
Bug introduced by
$current_version of type array is incompatible with the type string expected by parameter $current_version of blitze\content\ext::version_is_ok(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
			if (!$this->version_is_ok(/** @scrutinizer ignore-type */ $current_version, $required_version))
Loading history...
83
			{
84
				$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . $this->user->lang('EXTENSION_VERSION_UNMET', $required_version, $download_url, $extension, $current_version);
0 ignored issues
show
Deprecated Code introduced by
The function phpbb\user::lang() has been deprecated: 3.2.0-dev (To be removed 4.0.0) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

84
				$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . /** @scrutinizer ignore-deprecated */ $this->user->lang('EXTENSION_VERSION_UNMET', $required_version, $download_url, $extension, $current_version);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
85
				return false;
86
			}
87
			
88
			if (!$this->container->get('ext.manager')->is_enabled($extension))
89
			{
90
				$this->container->get('ext.manager')->enable($extension);
91
			}
92
		}
93
94
		return true;
95
	}
96
97
	/**
98
	 * @param string $current_version
99
	 * @param string $required_version
100
	 * @return bool
101
	 */
102
	protected function version_is_ok($current_version, &$required_version)
103
	{
104
		$required_version = html_entity_decode($required_version);
105
		list($min_req_version, $max_req_version) = explode(',', $required_version);
106
107
		$contraint = $this->get_version_constraint($min_req_version);
108
		if (!phpbb_version_compare($current_version, $contraint['version'], $contraint['operator']))
109
		{
110
			return false;
111
		}
112
113
		$contraint = $this->get_version_constraint($max_req_version);
114
		if ($constraint['version'] && !phpbb_version_compare($current_version, $contraint['version'], $contraint['operator']))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $constraint does not exist. Did you maybe mean $contraint?
Loading history...
115
		{
116
			return false;
117
		}
118
119
		return true;
120
	}
121
122
	/**
123
	 * @param string $version
124
	 * @return array
125
	 */
126
	protected function get_version_constraint($version)
127
	{
128
		if (preg_match('/^(\D+)(.+)/i', trim($version), $matches))
129
		{
130
			list(, $operator, $version) = $matches;
131
		}
132
133
		return [
134
			'version' => str_replace('@', '', $version),
135
			'operator' => $operator ?: '>=',
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $operator does not seem to be defined for all execution paths leading up to this point.
Loading history...
136
		];
137
	}
138
139
	/**
140
	 * Get composer metadata information
141
	 *
142
	 * @param string $name
143
	 * @param string $element
144
	 * @return array
145
	 */
146
	protected function get_metadata($name, $element = 'all')
147
	{
148
		$ext_manager = $this->container->get('ext.manager');
149
		$metadata_manager = $ext_manager->create_extension_metadata_manager($name);
150
		return $metadata_manager->get_metadata($element);
151
	}
152
}
153