Completed
Push — develop ( 79843d...c72359 )
by Daniel
12:15
created

settings::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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