Configuration::loadConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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
        if (!file_exists($configFileName)) {
176
            throw new Exception("File ".getcwd()."/".$configFileName." not found.");
177
        }
178
        $config = parse_ini_file($configFileName, TRUE); 
179
        if (!$pdoProvided) {
180
            $this->dsn = $this->loadMandatoryVariable($config, "database", "dsn");
181
        }
182
        
183
        $this->hitTableName = $this->loadMandatoryVariable($config, "database", "db_hit_table");
184
        $this->fromTableName = $this->loadMandatoryVariable($config, "database", "db_from_table");        
185
        $this->optionsTableName = $this->loadMandatoryVariable($config, "database", "db_options_table");
186
        $this->urlTableName = $this->loadMandatoryVariable($config, "database", "db_url_table");
187
        $this->useOnMemoryDB = $this->getBooleanParameter($config, "database", "use_onmemorydb");
188
                
189
        $this->storeTime = $this->getBooleanParameter($config, "options", "store_time");
190
        $this->storeUser = $this->getBooleanParameter($config, "options", "store_user"); 
191
        $this->storeFromInfo = $this->getBooleanParameter($config, "options", "store_from_info"); 
192
        $this->removeQueryString = $this->getBooleanParameter($config, "options", "remove_query_string");  
193
        
194
        $this->user = $this->getStringParameter($config, "database", "user"); 
195
        $this->password = $this->getStringParameter($config, "database", "password");  
196
    }
197
198
    /*
199
     * @param name string
200
     */
201
    private static function getStringParameter($config, $section, $name)
202
    {    
203
        return isset($config[$section][$name]) ? $config[$section][$name] : NULL;
204
    }
205
    
206
    /*
207
     * @param name string
208
     */
209
    private static function getBooleanParameter($config, $section, $name)
210
    {   
211
        return isset($config[$section][$name]) ? $config[$section][$name]!==0 : false;
212
    }
213
214
    /*
215
     * @param configFileName string
216
     * @param section string
217
     * @param varname string
218
     *
219
     * @return string
220
     */
221
    private function loadMandatoryVariable($configFile, $section, $varname)
222
    {
223
        try {
224
            return $configFile[$section][$varname];
225
        } catch (Exception $e) {
226
            throw new Exception("Error loading config file. Variable $varname in section [$section] not found. Check the configuration file.");
227
        }
228
    }
229
230
    /*
231
     * @param configFileName string
232
     */
233
    public static function loadConfig($configFileName)
234
    {
235
        $config = new Configuration($configFileName);
236
        return $config;
237
    }
238
239
}
240