Passed
Push — master ( 0ed305...0da594 )
by Ruben
01:49
created

Configuration   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 43
dl 0
loc 197
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * (c) Ruben Dorado <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace SiteAnalyzer;
11
12
use Exception;
13
14
/**
15
 * class SiteAnalyzer
16
 *
17
 * @package   SiteAnalyzer		
18
 * @author    Ruben Dorado <[email protected]>
19
 * @copyright 2018 Ruben Dorado
20
 * @license   http://www.opensource.org/licenses/MIT The MIT License
21
 */
22
final class Configuration
23
{
24
25
    /**
26
     * @var string
27
     */
28
    protected $dsn;
29
30
    /**
31
     * @var string
32
     */
33
    protected $user;
34
35
    /**
36
     * @var string
37
     */
38
    protected $password;
39
40
    /**
41
     * @var string
42
     */
43
    protected $hitTableName;
44
45
    /**
46
     * @var string
47
     */
48
    protected $optionsTableName;
49
    
50
    /**
51
     * @var string
52
     */
53
    protected $fromTableName;
54
    
55
    /**
56
     * @var string
57
     */
58
    protected $urlTableName;
59
    
60
    /**
61
     * @var boolean
62
     */
63
    protected $storeTime;
64
65
    /**
66
     * @var boolean
67
     */
68
    protected $storeUser;
69
    
70
    /**
71
     * @var boolean
72
     */
73
    protected $storeFromInfo;
74
    
75
    /**
76
     * @var boolean
77
     */
78
    protected $removeQueryString;
79
80
    /**
81
     * @var boolean
82
     */
83
    protected $useOnMemoryDB;
84
    
85
    
86
    /*
87
     * @return string
88
     */
89
    public function getDsn()
90
    {
91
        return $this->dsn;
92
    }
93
94
    /*
95
     * @return string
96
     */
97
    public function getUser()
98
    {
99
        return $this->user;
100
    }
101
102
    /*
103
     * @return string
104
     */
105
    public function getPassword()
106
    {
107
        return $this->password;
108
    }
109
110
    /*
111
     * @return boolean
112
     */
113
    public function getStoreTime()
114
    {
115
        return $this->storeTime;
116
    }
117
118
    /*
119
     * @return string
120
     */
121
    public function getHitTableName()
122
    {
123
        return $this->hitTableName;
124
    }
125
126
    
127
    /*
128
     * @return boolean
129
     */
130
    public function getRemoveQueryString()
131
    {
132
        
133
        return $this->removeQueryString;
134
    }
135
    
136
137
    /*
138
     * @return string
139
     */
140
    public function getOptionsTableName()
141
    {
142
        return $this->optionsTableName;
143
    }
144
    
145
    /*
146
     * @return string
147
     */
148
    public function getFromTableName()
149
    {
150
        return $this->fromTableName;
151
    }
152
153
    /*
154
     * @return string
155
     */
156
    public function getUrlTableName()
157
    {
158
        return $this->urlTableName;
159
    }
160
    
161
    /*
162
     * @return boolean
163
     */
164
    public function getUseOnMemoryDB()
165
    {
166
        return $this->useOnMemoryDB;
167
    }    
168
    
169
    /*
170
     * @param configFileName string
171
     * @param pdoProvided boolean
172
     */
173
    public function __construct($configFileName, $pdoProvided = FALSE)
174
    {    
175
        $config = parse_ini_file($configFileName, TRUE); 
176
        if (!$pdoProvided) {
177
            $this->dsn = $this->loadMandatoryVariable($config, "database", "dsn");
178
        }
179
        
180
        $this->hitTableName = $this->loadMandatoryVariable($config, "database", "db_hit_table");
181
        $this->fromTableName = $this->loadMandatoryVariable($config, "database", "db_from_table");        
182
        $this->optionsTableName = $this->loadMandatoryVariable($config, "database", "db_options_table");
183
        $this->urlTableName = $this->loadMandatoryVariable($config, "database", "db_url_table");
184
        $this->useOnMemoryDB = $this->loadMandatoryVariable($config, "database", "use_onmemorydb")=="yes";
185
        
186
        $this->storeTime = $this->getBooleanParameter($config, "options", "store_time");
187
        $this->storeUser = = $this->getBooleanParameter($config, "options", "store_user"); 
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '=' on line 187 at column 27
Loading history...
188
        $this->storeFromInfo = $this->getBooleanParameterisset($config, "options", "store_from_info"); 
189
        $this->removeQueryString = $this->getBooleanParameterisset($config, "options", "remove_query_string");  
190
        
191
        $this->user = $this->getStringParameter($config, "database", "user"); 
192
        $this->password = $this->getStringParameter($config, "database", "password");  
193
    }
194
195
    /*
196
     * @param name string
197
     */
198
    private static function getStringParameter($config, $section, $name)
199
    {    
200
        return isset($config[$section][$name]) ?  $config[$section][$name] : NULL;
201
    }
202
    
203
    /*
204
     * @param name string
205
     */
206
    private static function getBooleanParameter($config, $section, $name)
207
    {    
208
        return isset($config[$section][$name]) ? strtolower($config[$section][$name])=="yes" : false;
209
    }
210
211
    /*
212
     * @param configFileName string
213
     * @param section string
214
     * @param varname string
215
     *
216
     * @return string
217
     */
218
    private function loadMandatoryVariable($configFile, $section, $varname)
219
    {
220
        try {
221
            return $configFile[$section][$varname];
222
        } catch (Exception $e) {
223
            throw new Exception("Error loading config file. Variable $varname in section [$section] not found. Check the configuration file.");
224
        }
225
    }
226
227
228
    /*
229
     * @param configFileName string
230
     */
231
    public static function loadConfig($configFileName)
232
    {
233
        $config = new Configuration($configFileName);
234
        return $config;
235
    }
236
237
238
}
239