Completed
Push — develop ( f5bae7...6f4b81 )
by Daniel
08:40
created

settings::config_is_writable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2017 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\services\filemanager;
11
12
/**
13
* @package sitemaker
14
*/
15
class settings
16
{
17
	/** @var \phpbb\filesystem\filesystem */
18
	protected $filesystem;
19
20
	/** @var string */
21
	protected $config_path;
22
23
	/** @var string phpEx */
24
	protected $php_ext;
25
26
	/** @var string */
27
	protected $config_template;
28
29
	/** @var string */
30
	protected $config_version = '3.2.0';
31
32
	/** @var array */
33
	protected $filemanager_prop_types = array(
34
		'image_max_width'			=> 'integer',
35
		'image_max_height'			=> 'integer',
36 2
		'image_max_mode'			=> 'string',
37
		'image_resizing'			=> 'boolean',
38 2
		'image_resizing_width'		=> 'integer',
39 2
		'image_resizing_height'		=> 'integer',
40 2
		'image_resizing_mode'		=> 'string',
41
		'image_watermark'			=> 'string',
42 2
		'image_watermark_position'	=> 'string',
43 2
		'image_watermark_padding'	=> 'integer',
44
	);
45
46
	/**
47
	 * Constructor
48
	 *
49 2
	 * @param \phpbb\filesystem\filesystem	$filesystem			File system
50
	 * @param string						$config_path		Filemanager config path
51
	 * @param string						$php_ext			phpEx
52 2
	 */
53 2
	public function __construct(\phpbb\filesystem\filesystem $filesystem, $config_path, $php_ext)
54
	{
55
		$this->filesystem = $filesystem;
56
		$this->config_path = $config_path;
57 2
		$this->php_ext = $php_ext;
58
59 2
		$this->config_template = __DIR__ . '/default.config';
60 2
	}
61 2
62 2
	/**
63
	 * @param bool $check_file
64
	 * @return array
65 2
	 */
66
	public function get_settings($check_file = true)
67
	{
68
		// return empty array if filemanager is not installed
69
		if (!$this->is_installed())
70
		{
71
			return array();
72 2
		}
73
74 2
		$config_file = $this->get_config_file();
75 2
76
		if ($check_file)
77
		{
78
			$this->ensure_config_is_ready();
79
		}
80
81 1
		return include($config_file);
82
	}
83 1
84 1
	/**
85 1
	 * @param string $file
86
	 * @return void
87 1
	 */
88
	public function set_config_template($file)
89 1
	{
90 1
		$this->config_template = $file;
91 1
	}
92
93 1
	/**
94 1
	 * @return void
95
	 */
96
	public function ensure_config_is_ready()
97
	{
98
		$config_file = $this->get_config_file();
99
		$test_line = file($config_file)[1];
100
101 1
		if (strpos($test_line, 'Sitemaker ' . $this->config_version) === false)
102
		{
103 1
			$curr_settings = array();
104
105
			// we are already using sitemaker config but it is out of date
106 1
			if (strpos($test_line, 'Sitemaker'))
107 1
			{
108 1
				$curr_settings = $this->get_settings(false);
109 1
				$curr_settings = array_intersect_key($curr_settings, $this->filemanager_prop_types);
110 1
			}
111 1
112
			$this->filesystem->remove($config_file);
113 1
			$this->save($curr_settings);
114
		}
115
	}
116
117
	/**
118 2
	 * @return bool
119
	 */
120 2
	public function config_is_writable()
121
	{
122 2
		return $this->is_installed() && $this->filesystem->is_writable($this->get_config_file());
123 2
	}
124 2
125 2
	/**
126
	 * @param array $settings
127 2
	 * @return void
128
	 */
129
	public function save(array $settings)
130
	{
131
		if (sizeof($settings))
132
		{
133
			$config_file = $this->get_config_file();
134
			$config_str = file_get_contents($config_file);
135
136
			foreach ($settings as $prop => $value)
137
			{
138
				$this->type_cast_config_value($prop, $value);
139
140
				$config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", "	'$prop'$1=> $value,", $config_str);
141
			}
142
143
			$this->filesystem->dump_file(realpath($config_file), $config_str);
144
		}
145
	}
146
147
	/**
148
	 * @return bool
149
	 */
150
	protected function is_installed()
151
	{
152
		return is_dir($this->config_path);
153
	}
154
155
	/**
156
	 * @return string
157
	 */
158
	protected function get_config_file()
159
	{
160
		$config_file = $this->config_path . 'config.' . $this->php_ext;
161
162
		if (!$this->filesystem->exists($config_file))
163
		{
164
			$this->filesystem->copy($this->config_template, $config_file, true);
165
		}
166
167
		return $config_file;
168
	}
169
170
	/**
171
	 * @param string $prop
172
	 * @param mixed $value
173
	 * @return void
174
	 */
175
	protected function type_cast_config_value($prop, &$value)
176
	{
177
		$type = $this->filemanager_prop_types[$prop];
178
179
		settype($value, $type);
180
181
		switch ($type)
182
		{
183
			case 'boolean':
184
				$value = ($value) ? 'true' : 'false';
185
			break;
186
			case 'string':
187
				$value = "'$value'";
188
			break;
189
		}
190
	}
191
}
192