Passed
Push — renovate/configure ( 0990eb...046e0a )
by
unknown
20:50
created

settings::get_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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