ext::image_directory_is_ready()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 12
rs 10
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|array
21
	 */
22
	public function is_enableable()
23
	{
24
		$user = $this->container->get('user');
25
		$user->add_lang_ext('blitze/sitemaker', 'ext');
26
27
		$metadata = $this->get_metadata('blitze/sitemaker');
28
29
		$req_phpbb_version = $metadata['extra']['soft-require']['phpbb/phpbb'];
30
		$phpbb_version = $this->container->get('config')['version'];
31
		$version_ok = $this->version_is_ok($phpbb_version, $req_phpbb_version);
32
		$img_dir_ok = $this->image_directory_is_ready();
33
34
		if (!$version_ok || !$img_dir_ok)
35
		{
36
			$errors = array_merge(['EXTENSION_NOT_ENABLEABLE'], $this->errors);
37
			return array_map([$user, 'lang'], array_filter($errors), ['', $req_phpbb_version]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_map(array($...', $req_phpbb_version)) returns the type array which is incompatible with the return type mandated by phpbb\extension\extensio...erface::is_enableable() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
38
		}
39
40
		return true;
41
	}
42
43
	/**
44
	 * return bool
45
	*/
46
	protected function image_directory_is_ready()
47
	{
48
		$root_path = $this->container->getParameter('core.root_path');
49
		$filesystem = $this->container->get('filesystem');
50
		$images_folder = $root_path . 'images';
51
		$uploads_folder = $images_folder . '/sitemaker_uploads';
52
53
		if (!$filesystem->exists($uploads_folder) && !$filesystem->is_writable($images_folder))
54
		{
55
			$this->errors[] = 'IMAGE_DIRECTORY_NOT_WRITABLE';
56
			return false;
57
		}
58
		return true;
59
	}
60
61
	/**
62
	 * @param string $current_version
63
	 * @param string $required_version
64
	 * @return bool
65
	 */
66
	protected function version_is_ok($current_version, &$required_version)
67
	{
68
		$required_version = html_entity_decode($required_version);
69
		list($min_req_version, $max_req_version) = array_pad(explode(',', $required_version), 2, '');
70
71
		$constraint = $this->get_version_constraint($min_req_version);
72
		if (!phpbb_version_compare($current_version, $constraint['version'], $constraint['operator']))
73
		{
74
			$this->errors[] = 'PHPBB_VERSION_UNMET';
75
			return false;
76
		}
77
78
		$constraint = $this->get_version_constraint($max_req_version);
79
		if ($constraint['version'] && !phpbb_version_compare($current_version, $constraint['version'], $constraint['operator']))
80
		{
81
			$this->errors[] = 'PHPBB_VERSION_UNMET';
82
			return false;
83
		}
84
85
		return true;
86
	}
87
88
	/**
89
	 * @param string $version
90
	 * @return array
91
	 */
92
	protected function get_version_constraint($version)
93
	{
94
		$operator = '';
95
		if (preg_match('/^(\D+)(.+)/i', trim($version), $matches))
96
		{
97
			list(, $operator, $version) = $matches;
98
		}
99
100
		return [
101
			'version' => str_replace('@', '', $version),
102
			'operator' => $operator ?: '>=',
103
		];
104
	}
105
106
	/**
107
	 * Get composer metadata information
108
	 *
109
	 * @param string $name
110
	 * @return array
111
	 */
112
	protected function get_metadata($name)
113
	{
114
		$ext_manager = $this->container->get('ext.manager');
115
		$metadata_manager = $ext_manager->create_extension_metadata_manager($name);
116
		return $metadata_manager->get_metadata('all');
117
	}
118
}
119