Completed
Push — develop ( 56e698...1a7010 )
by Daniel
08:15
created

settings::set_config_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\filesystem\filesystem	$filesystem			File system
33
	 * @param string						$config_path		Filemanager config path
34
	 * @param string						$php_ext			phpEx
35
	 */
36 2
	public function __construct(\phpbb\filesystem\filesystem $filesystem, $config_path, $php_ext)
37
	{
38 2
		$this->filesystem = $filesystem;
39 2
		$this->config_path = $config_path;
40 2
		$this->php_ext = $php_ext;
41
42 2
		$this->config_template = __DIR__ . '/default.config';
43 2
	}
44
45
	/**
46
	 * @return array|void
47
	 */
48 2
	public function get_settings()
49
	{
50 2
		$config_file = $this->get_config_file();
51
52 2
		if (!$this->filesystem->exists($config_file))
53 2
		{
54
			$this->filesystem->copy($this->config_template, $config_file, true);
55
		}
56
57 2
		return include($config_file);
58
	}
59
60
	/**
61
	 * @param string $file
62
	 * @return void
63
	 */
64 2
	public function set_config_template($file)
65
	{
66 2
		$this->config_template = $file;
67 2
	}
68
69
	/**
70
	 * @param array $settings
71
	 * @return void
72
	 */
73 1
	public function save(array $settings)
74
	{
75 1
		$curr_settings = $this->get_settings();
76 1
		$config_file = $this->get_config_file();
77 1
		$config_str = file_get_contents($config_file);
78
79 1
		foreach ($settings as $prop => $value)
80
		{
81 1
			$this->type_cast_config_value($curr_settings[$prop], $value);
82 1
			$config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", "	'$prop'$1=> $value,", $config_str);
83 1
		}
84
85 1
		$this->filesystem->dump_file($config_file, $config_str);
86 1
	}
87
88
	/**
89
	 * @param mixed $curr_val
90
	 * @param mixed $value
91
	 * @return void
92
	 */
93 1
	protected function type_cast_config_value($curr_val, &$value)
94
	{
95 1
		$type = gettype($curr_val);
96
		switch ($type)
97
		{
98 1
			case 'string':
99 1
				$value = "'$value'";
100 1
			break;
101 1
			case 'integer':
102 1
				$value = (int) $value;
103 1
			break;
104
		}
105 1
	}
106
107
	/**
108
	 * @return string
109
	 */
110 2
	protected function get_config_file()
111
	{
112 2
		return $this->config_path . 'config.' . $this->php_ext;
113
	}
114
}
115