Completed
Push — develop ( ae80e4...ae707d )
by Daniel
10:44
created

settings::ensure_config_is_ready()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 18
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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.1.1';
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
		'aviary_apiKey'				=> 'string',
45
		'aviary_active'				=> 'boolean',
46
	);
47
48
	/**
49 2
	 * Constructor
50
	 *
51
	 * @param \phpbb\filesystem\filesystem	$filesystem			File system
52 2
	 * @param string						$config_path		Filemanager config path
53 2
	 * @param string						$php_ext			phpEx
54
	 */
55
	public function __construct(\phpbb\filesystem\filesystem $filesystem, $config_path, $php_ext)
56
	{
57 2
		$this->filesystem = $filesystem;
58
		$this->config_path = $config_path;
59 2
		$this->php_ext = $php_ext;
60 2
61 2
		$this->config_template = __DIR__ . '/default.config';
62 2
	}
63
64
	/**
65 2
	 * @param bool $check_file
66
	 * @return array
67
	 */
68
	public function get_settings($check_file = true)
69
	{
70
		// return empty array if filemanager is not installed
71
		if (!is_dir($this->config_path))
72 2
		{
73
			return array();
74 2
		}
75 2
76
		$config_file = $this->get_config_file();
77
78
		if ($check_file)
79
		{
80
			$this->ensure_config_is_ready();
81 1
		}
82
83 1
		return include($config_file);
84 1
	}
85 1
86
	/**
87 1
	 * @param string $file
88
	 * @return void
89 1
	 */
90 1
	public function set_config_template($file)
91 1
	{
92
		$this->config_template = $file;
93 1
	}
94 1
95
	/**
96
	 * @return void
97
	 */
98
	public function ensure_config_is_ready()
99
	{
100
		$config_file = $this->get_config_file();
101 1
		$test_line = file($config_file)[1];
102
103 1
		if (strpos($test_line, 'Sitemaker ' . $this->config_version) === false)
104
		{
105
			$curr_settings = array();
106 1
107 1
			// we are already using sitemaker config but it is out of date
108 1
			if (strpos($test_line, 'Sitemaker'))
109 1
			{
110 1
				$curr_settings = $this->get_settings(false);
111 1
				$curr_settings = array_intersect_key($curr_settings, $this->filemanager_prop_types);
112
			}
113 1
114
			$this->filesystem->remove($config_file);
115
			$this->save($curr_settings);
116
		}
117
	}
118 2
119
	/**
120 2
	 * @param array $settings
121
	 * @return void
122 2
	 */
123 2
	public function save(array $settings)
124 2
	{
125 2
		$config_file = $this->get_config_file();
126
		if (sizeof($settings))
127 2
		{
128
			$config_str = file_get_contents($config_file);
129
130
			foreach ($settings as $prop => $value)
131
			{
132
				$this->type_cast_config_value($prop, $value);
133
134
				$config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", "	'$prop'$1=> $value,", $config_str);
135
			}
136
137
			$this->filesystem->dump_file(realpath($config_file), $config_str);
138
		}
139
	}
140
141
	/**
142
	 * @return string
143
	 */
144
	protected function get_config_file()
145
	{
146
		$config_file = $this->config_path . 'config.' . $this->php_ext;
147
148
		if (!$this->filesystem->exists($config_file))
149
		{
150
			$this->filesystem->copy($this->config_template, $config_file, true);
151
		}
152
153
		return $config_file;
154
	}
155
156
	
157
	/**
158
	 * @param string $prop
159
	 * @param mixed $value
160
	 * @return void
161
	 */
162
	protected function type_cast_config_value($prop, &$value)
163
	{
164
		$type = $this->filemanager_prop_types[$prop];
165
166
		settype($value, $type);
167
168
		switch ($type)
169
		{
170
			case 'boolean':
171
				$value = ($value) ? 'true' : 'false';
172
			break;
173
			case 'string':
174
				$value = "'$value'";
175
			break;
176
		}
177
	}
178
}
179