GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.0 ( 317852...6b466d )
by Vermeulen
01:33
created

Config::setConfigForFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
7
/**
8
 * Class to load all files from a directory in config dir.
9
 */
10
class Config
11
{
12
    /**
13
     * @const ERR_JSON_PARSE Exception code if the parse of a json file fail.
14
     */
15
    const ERR_JSON_PARSE = 1302001;
16
    
17
    /**
18
     * @const ERR_GETVALUE_FILE_NOT_INDICATED Exception code if the file to use
19
     * is not indicated into the method getValue().
20
     * (only if there are many config files)
21
     */
22
    const ERR_GETVALUE_FILE_NOT_INDICATED = 1302002;
23
    
24
    /**
25
     * @const ERR_GETVALUE_FILE_NOT_FOUND Exception code if the file to use is
26
     * not found.
27
     */
28
    const ERR_FILE_NOT_FOUND = 1302003;
29
    
30
    /**
31
     * @const ERR_GETVALUE_KEY_NOT_FOUND Exception code if the asked config key
32
     * not exist.
33
     */
34
    const ERR_KEY_NOT_FOUND = 1303004;
35
    
36
    /**
37
     * @const ERR_VALUE_FORMAT Exception code if the value for a filename is
38
     * not an array or an object.
39
     */
40
    const ERR_VALUE_FORMAT = 1303005;
41
    
42
    /**
43
     * @var string $configDirName Directory's name in config dir
44
     */
45
    protected $configDirName = '';
46
47
    /**
48
     * @var string $configDir Complete path of the readed directory
49
     */
50
    protected $configDir = '';
51
52
    /**
53
     * @var string[] $configFiles List of files to read
54
     */
55
    protected $configFiles = [];
56
57
    /**
58
     * @var array $config List of config value found
59
     */
60
    protected $config = [];
61
62
    /**
63
     * Constructor
64
     * Define properties configDirName and configDir
65
     * 
66
     * @param string $configDirName Directory's name in config dir
67
     */
68
    public function __construct($configDirName)
69
    {
70
        $this->configDirName = $configDirName;
71
        $this->configDir     = CONFIG_DIR.$this->configDirName;
72
    }
73
    
74
    /**
75
     * Getter accessor to $config property
76
     * 
77
     * @return array
78
     */
79
    public function getConfig()
80
    {
81
        return $this->config;
82
    }
83
    
84
    /**
85
     * Getter accessor to a value into the $config property
86
     * 
87
     * @param string $file
88
     * 
89
     * @return mixed
90
     */
91
    public function getConfigForFile($file)
92
    {
93
        return $this->config[$file];
94
    }
95
    
96
    /**
97
     * Search and load all config files which has been found
98
     * 
99
     * @return void
100
     */
101
    public function loadFiles()
102
    {
103
        $this->searchAllConfigFiles($this->configDir);
104
        
105
        foreach ($this->configFiles as $fileKey => $filePath) {
106
            $this->loadConfigFile($fileKey, $filePath);
107
        }
108
    }
109
110
    /**
111
     * Search all config files in a directory
112
     * Search also in sub-directory (2nd parameter)
113
     * 
114
     * @param string $dirPath The directory path where is run the search
115
     * @param string $pathIntoFirstDir (default '') Used when this method
116
     *  reads a subdirectory. It's the path of the readed directory during
117
     *  the first call to this method.
118
     * 
119
     * @return void
120
     */
121
    protected function searchAllConfigFiles($dirPath, $pathIntoFirstDir = '')
122
    {
123
        if (!file_exists($dirPath)) {
124
            return;
125
        }
126
127
        //Remove some value in list of file
128
        $listFiles = array_diff(scandir($dirPath), ['.', '..']);
129
130
        foreach ($listFiles as $file) {
131
            $fileKey  = $pathIntoFirstDir.$file;
132
            $readPath = $dirPath.'/'.$file;
133
134
            if (is_file($readPath)) {
135
                $this->configFiles[$fileKey] = $readPath;
136
            } elseif (is_link($readPath)) {
137
                $this->configFiles[$fileKey] = realpath($readPath);
138
            } elseif (is_dir($readPath)) {
139
                $this->searchAllConfigFiles(
140
                    $readPath,
141
                    $pathIntoFirstDir.$file.'/'
142
                );
143
            }
144
        }
145
    }
146
147
    /**
148
     * Load a config file.
149
     * Find the file's extension and call the method to parse the file
150
     * 
151
     * @param string $fileKey The file's key. Most of the time, the path to
152
     *  the file from the $this->configDir value
153
     * @param string $filePath The path to the file
154
     * 
155
     * @return void
156
     */
157
    protected function loadConfigFile($fileKey, $filePath)
158
    {
159
        $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
160
161
        if ($fileExtension === 'json') {
162
            $this->loadJsonConfigFile($fileKey, $filePath);
163
            return;
164
        }
165
166
        if ($fileExtension === 'php') {
167
            $this->loadPhpConfigFile($fileKey, $filePath);
168
            return;
169
        }
170
171
        //@TODO : YAML
172
    }
173
174
    /**
175
     * Load a json config file
176
     * 
177
     * @param string $fileKey The file's key. Most of the time, the path to
178
     *  the file from the $this->configDir value
179
     * @param string $filePath The path to the file
180
     * 
181
     * @return void
182
     * 
183
     * @throws Exception If there is an error from the json parser
184
     */
185
    protected function loadJsonConfigFile($fileKey, $filePath)
186
    {
187
        $json   = file_get_contents($filePath);
188
        $config = json_decode($json);
189
190
        if ($config === null) {
191
            throw new Exception(
192
                json_last_error_msg(),
193
                $this::ERR_JSON_PARSE
194
            );
195
        }
196
197
        $this->config[$fileKey] = $config;
198
    }
199
200
    /**
201
     * Load a php config file
202
     * 
203
     * @param string $fileKey The file's key. Most of the time, the path to
204
     *  the file from the $this->configDir value
205
     * @param string $filePath The path to the file
206
     * 
207
     * @return void
208
     */
209
    protected function loadPhpConfigFile($fileKey, $filePath)
210
    {
211
        $this->config[$fileKey] = require($filePath);
212
    }
213
214
    /**
215
     * Return a config value for a key
216
     * 
217
     * @param string $key The asked key for the value
218
     * @param string $file (default null) If many file is loaded, the file name
219
     *  where is the key. Is the file is into a sub-directory, the
220
     *  sub-directory should be present.
221
     * 
222
     * @return mixed
223
     * 
224
     * @throws Exception If file parameter is null and there are many files. Or
225
     *  if the file not exist. Or if the key not exist.
226
     */
227
    public function getValue($key, $file = null)
228
    {
229
        $nbConfigFile = count($this->config);
230
231
        if ($file === null && $nbConfigFile > 1) {
232
            throw new Exception(
233
                'There are many config files. Please indicate the file to'
234
                .' obtain the config '.$key,
235
                $this::ERR_GETVALUE_FILE_NOT_INDICATED
236
            );
237
        }
238
239
        if ($nbConfigFile === 1) {
240
            $file = key($this->config);
241
        }
242
243
        if (!isset($this->config[$file])) {
244
            throw new Exception(
245
                'The file '.$file.' has not been found for config '.$key,
246
                $this::ERR_FILE_NOT_FOUND
247
            );
248
        }
249
250
        $config = (array) $this->config[$file];
251
        if (!array_key_exists($key, $config)) {
252
            throw new Exception(
253
                'The config key '.$key.' has not been found',
254
                $this::ERR_KEY_NOT_FOUND
255
            );
256
        }
257
258
        return $config[$key];
259
    }
260
    
261
    /**
262
     * Setter to modify the all config value for a specific filename.
263
     * 
264
     * @param string $filename The filename config to modify
265
     * @param array|\stdClass $config The new config value
266
     * 
267
     * @return $this
268
     * 
269
     * @throws \Exception If the new value if not an array or an object.
270
     */
271
    public function setConfigForFile($filename, $config)
272
    {
273
        if (!is_array($config) && !is_object($config)) {
274
            throw new Exception(
275
                'The config value shoud be an array or an object.',
276
                $this::ERR_VALUE_FORMAT
277
            );
278
        }
279
        
280
        $this->config[$filename] = $config;
281
        
282
        return $this;
283
    }
284
    
285
    /**
286
     * Setter to modify a config key into the config of a filename
287
     * 
288
     * @param string $filename The filename config to modify
289
     * @param string $configKey The name of the key to modify
290
     * @param mixed $configValue The new value for the config key
291
     * 
292
     * @return $this
293
     * 
294
     * @throws \Exception If the key has not been found
295
     */
296
    public function setConfigKeyForFile($filename, $configKey, $configValue)
297
    {
298
        if (!isset($this->config[$filename])) {
299
            $this->config[$filename] = new \stdClass;
300
        }
301
        
302
        if (is_array($this->config[$filename])) {
303
            $this->config[$filename][$configKey] = $configValue;
304
        } elseif (is_object($this->config[$filename])) {
305
            $this->config[$filename]->{$configKey} = $configValue;
306
        } else {
307
            throw new Exception(
308
                'The config key '.$configKey.' has not been found',
309
                $this::ERR_KEY_NOT_FOUND
310
            );
311
        }
312
        
313
        return $this;
314
    }
315
}
316