ConfigEditor::set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Console-Kit library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/console-kit
9
 */
10
11
namespace ConsoleHelpers\ConsoleKit\Config;
12
13
14
class ConfigEditor
15
{
16
17
	/**
18
	 * Filename, where config is stored.
19
	 *
20
	 * @var string
21
	 */
22
	protected $filename;
23
24
	/**
25
	 * Settings.
26
	 *
27
	 * @var array
28
	 */
29
	protected $settings = array();
30
31
	/**
32
	 * Creates config instance.
33
	 *
34
	 * @param string $filename Filename.
35
	 * @param array  $defaults Defaults.
36
	 */
37 8
	public function __construct($filename, array $defaults = array())
38
	{
39 8
		$this->filename = $filename;
40 8
		$this->load($defaults);
41 8
	}
42
43
	/**
44
	 * Returns config value.
45
	 *
46
	 * @param string $name    Config setting name.
47
	 * @param mixed  $default Default value.
48
	 *
49
	 * @return mixed
50
	 */
51 5
	public function get($name, $default = null)
52
	{
53 5
		if ( substr($name, -1) == '.' ) {
54 2
			$ret = array();
55
56 2
			foreach ( $this->settings as $setting_name => $setting_value ) {
57 2
				if ( preg_match('/^' . preg_quote($name, '/') . '/', $setting_name) ) {
58 2
					$ret[$setting_name] = $setting_value;
59 2
				}
60 2
			}
61
62 2
			return $ret;
63
		}
64
65 5
		return array_key_exists($name, $this->settings) ? $this->settings[$name] : $default;
66
	}
67
68
	/**
69
	 * Returns all config settings.
70
	 *
71
	 * @return array
72
	 */
73 1
	public function getAll()
74
	{
75 1
		return $this->settings;
76
	}
77
78
	/**
79
	 * Sets config value.
80
	 *
81
	 * @param string $name  Config setting name.
82
	 * @param mixed  $value Config setting value.
83
	 *
84
	 * @return void
85
	 */
86 2
	public function set($name, $value)
87
	{
88 2
		if ( $value === null ) {
89 1
			unset($this->settings[$name]);
90 1
		}
91
		else {
92 1
			$this->settings[$name] = $value;
93
		}
94
95 2
		$this->store();
96 2
	}
97
98
	/**
99
	 * Loads config contents from disk.
100
	 *
101
	 * @param array $defaults Defaults.
102
	 *
103
	 * @return void
104
	 */
105 8
	protected function load(array $defaults)
106
	{
107 8
		if ( file_exists($this->filename) ) {
108 6
			$stored_settings = json_decode(file_get_contents($this->filename), true);
109 6
			$new_defaults = array_diff_key($defaults, $stored_settings);
110
111 6
			if ( $new_defaults ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $new_defaults of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112 2
				$this->settings = array_merge($stored_settings, $new_defaults);
113 2
				$this->store();
114 2
			}
115
			else {
116 5
				$this->settings = $stored_settings;
117
			}
118
119 6
			return;
120
		}
121
122 2
		$this->settings = $defaults;
123 2
		$this->store();
124 2
	}
125
126
	/**
127
	 * Stores config contents to the disk.
128
	 *
129
	 * @return void
130
	 */
131 6
	protected function store()
132
	{
133 6
		$options = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
134 6
		file_put_contents($this->filename, json_encode($this->settings, $options));
135 6
	}
136
137
}
138