Completed
Push — master ( 0b0682...ea1059 )
by Rougin
05:39
created

Config::set()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 40
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7.0671

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 24
cts 27
cp 0.8889
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 10
nop 5
crap 7.0671
1
<?php
2
3
namespace Rougin\Combustor\Common;
4
5
/**
6
 * Config
7
 *
8
 * A simple object-oriented interface for handling CodeIgniter's configurations.
9
 *
10
 * @package Combustor
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class Config
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $config;
19
20
    /**
21
     * @var string
22
     */
23
    public $fileName;
24
25
    /**
26
     * @var array
27
     */
28
    protected $lines;
29
30
    /**
31
     * @param string $config
32
     * @param string $configPath
33
     */
34 6
    public function __construct($config, $configPath)
35
    {
36 6
        $this->config = $config;
37 6
        $this->fileName = $configPath . '/' . $config . '.php';
38
39 6
        $content = file_get_contents($this->fileName);
40
41 6
        $this->lines = preg_split("/\\r\\n|\\r|\\n/", $content);
42 6
    }
43
44
    /**
45
     * Returns the specified value from the config item.
46
     *
47
     * @param  string       $item
48
     * @param  integer      $line
49
     * @param  string  $dataType
50
     * @return mixed
51
     */
52 6
    public function get($item, $line, $dataType)
53
    {
54 6
        $result = null;
55 6
        $value = null;
56
57
        switch ($dataType) {
58 6
            case 'array':
59 6
                $value = 'array\((.*?)\)';
60
61 6
                break;
62
            case 'boolean':
63
                $value = '(true|TRUE|false|FALSE)';
64
65
                break;
66
            case 'string':
67
                $value = '(.*?)';
68
69
                break;
70
        }
71
72 6
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $value . ';/';
73
74 6
        preg_match_all($pattern, $this->lines[$line], $match);
75
76
        switch ($dataType) {
77 6
            case 'array':
78 6
                $result = array_filter($match[1]);
79 6
                $data = '';
80
81 6
                if (! empty($result[0])) {
82
                    $data = $result[0];
83
                }
84
85 6
                $result = array_filter(explode(',', $data));
86 6
                $length = count($result);
87
88 6
                for ($i = 0; $i < $length; $i++) {
89
                    $result[$i] = str_replace(['\'', '"'], '', trim($result[$i]));
90
                }
91
92 6
                break;
93
            case 'boolean':
94
                $result = $match[1][0] == 'TRUE';
95
96
                break;
97
            case 'string':
98
                $result = $match[1][0];
99
100
                if ($result == '\'\'') {
101
                    $result = null;
102
                }
103
104
                if ($result[0] == '\'' && $result[strlen($result) - 1] == '\'') {
105
                    $result = substr($result, 1, strlen($result) - 2);
106
                }
107
108
                break;
109
        }
110
111 6
        return $result;
112
    }
113
114
    /**
115
     * Sets an value to an item in the config.
116
     *
117
     * @param string  $item
118
     * @param integer $line
119
     * @param mixed   $value
120
     * @param string  $dataType
121
     * @param boolean $exact
122
     */
123 6
    public function set($item, $line, $value, $dataType, $exact = false)
124
    {
125 6
        $data = null;
126 6
        $format = null;
127
128
        switch ($dataType) {
129 6
            case 'array':
130 6
                $length = count($value);
131
132 6
                for ($i = 0; $i < $length; $i++) {
133 6
                    $value[$i] = '\'' . $value[$i] . '\'';
134 6
                }
135
136 6
                $data = 'array(' . implode(', ', $value) . ')';
137 6
                $format = 'array\([^)]*\)';
138
139 6
                break;
140 6
            case 'boolean':
141
                $data = ($value) ? 'TRUE' : 'FALSE';
142
                $format = '[^)]*';
143
144
                break;
145 6
            case 'string':
146 6
                $data = '\'' . $value . '\'';
147 6
                $format = '(.*?)';
148
149 6
                break;
150
        }
151
152 6
        if ($exact) {
153 6
            $data = $value;
154 6
        }
155
156 6
        $pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $format . ';/';
157 6
        $replacement = '$' . $this->config . '[\'' . $item . '\'] = ' . $data . ';';
158
159 6
        $result = preg_replace($pattern, $replacement, $this->lines[$line]);
160
161 6
        $this->lines[$line] = $result;
162 6
    }
163
164
    /**
165
     * Saves the current config.
166
     *
167
     * @return void
168
     */
169 6
    public function save()
170
    {
171 6
        file_put_contents($this->fileName, (string) $this);
172 6
    }
173
174
    /**
175
     * Returns the whole class into a string.
176
     *
177
     * @return string
178
     */
179 6
    public function __toString()
180
    {
181 6
        return implode(PHP_EOL, $this->lines);
182
    }
183
}
184