Passed
Push — develop ( 726672...797038 )
by Daniel
03:29
created

ext::version_is_ok()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 20
ccs 0
cts 0
cp 0
crap 20
rs 9.9
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\sitemaker;
11
12
class ext extends \phpbb\extension\base
13
{
14 2
	/** array */
15
	protected $errors = [];
16 2
17 2
	/**
18
	 * Check whether or not the extension can be enabled.
19
	 *
20
	 * @return bool
21
	 */
22
	public function is_enableable()
23
	{
24
		$user = $this->container->get('user');
25
		$user->add_lang_ext('blitze/sitemaker', 'ext');
26
27
		$lang = $user->lang;
28
		$metadata = $this->get_metadata('blitze/sitemaker');
29
30
		$req_phpbb_version = $metadata['extra']['soft-require']['phpbb/phpbb'];
31
		$phpbb_version = $this->container->get('config')['version'];
32
		$is_enableable = $this->version_is_ok($phpbb_version, $req_phpbb_version) && $this->image_directory_is_ready();
33
34
		if (!$is_enableable)
35
		{
36
			$errors = array_map([$user, 'lang'], array_filter($this->errors), [$req_phpbb_version]);
37
			$lang['EXTENSION_NOT_ENABLEABLE'] .= '<br>' . join('<br />', $errors);
38
		}
39
40
		$user->lang = $lang;
41
		return $is_enableable;
42
	}
43
44
	/**
45
	 * return bool
46
	*/
47
	protected function image_directory_is_ready()
48
	{
49
		$root_path = $this->container->getParameter('core.root_path');
50
51
		if (!is_dir($root_path . 'images/sitemaker_uploads') && !is_writable($root_path . 'images'))
52
		{
53
			$this->errors[] = 'IMAGE_DIRECTORY_NOT_WRITABLE';
54
			return false;
55
		}
56
		return true;
57
	}
58
59
	/**
60
	 * @param string $current_version
61
	 * @param string $required_version
62
	 * @return bool
63
	 */
64
	protected function version_is_ok($current_version, &$required_version)
65
	{
66
		$required_version = html_entity_decode($required_version);
67
		list($min_req_version, $max_req_version) = array_pad(explode(',', $required_version), 2, '');
68
69
		$constraint = $this->get_version_constraint($min_req_version);
70
		if (!phpbb_version_compare($current_version, $constraint['version'], $constraint['operator']))
71
		{
72
			$this->errors[] = 'PHPBB_VERSION_UNMET';
73
			return false;
74
		}
75
76
		$constraint = $this->get_version_constraint($max_req_version);
77
		if ($constraint['version'] && !phpbb_version_compare($current_version, $constraint['version'], $constraint['operator']))
78
		{
79
			$this->errors[] = 'PHPBB_VERSION_UNMET';
80
			return false;
81
		}
82
83
		return true;
84
	}
85
86
	/**
87
	 * @param string $version
88
	 * @return array
89
	 */
90
	protected function get_version_constraint($version)
91
	{
92
		$operator = '';
93
		if (preg_match('/^(\D+)(.+)/i', trim($version), $matches))
94
		{
95
			list(, $operator, $version) = $matches;
96
		}
97
98
		return [
99
			'version' => str_replace('@', '', $version),
100
			'operator' => $operator ?: '>=',
101
		];
102
	}
103
104
	/**
105
	 * Get composer metadata information
106
	 *
107
	 * @param string $name
108
	 * @return array
109
	 */
110
	protected function get_metadata($name)
111
	{
112
		$ext_manager = $this->container->get('ext.manager');
113
		$metadata_manager = $ext_manager->create_extension_metadata_manager($name);
114
		return $metadata_manager->get_metadata('all');
115
	}
116
}
117