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 ( 043bf5...92a966 )
by Vermeulen
02:10
created

Config::getValue()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.439
cc 6
eloc 15
nc 7
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
     * @var string $configDirName Directory's name in config dir
14
     */
15
    protected $configDirName = '';
16
17
    /**
18
     * @var string $configDir Complete path of the readed directory
19
     */
20
    protected $configDir = '';
21
22
    /**
23
     * @var string[] $configFiles List of files to read
24
     */
25
    protected $configFiles = [];
26
27
    /**
28
     * @var array $config List of config value found
29
     */
30
    protected $config = [];
31
32
    /**
33
     * Constructor
34
     * Define properties configDirName and configDir
35
     * 
36
     * @param string $configDirName Directory's name in config dir
37
     */
38
    public function __construct($configDirName)
39
    {
40
        $this->configDirName = $configDirName;
41
        $this->configDir     = CONFIG_DIR.$this->configDirName;
42
    }
43
    
44
    /**
45
     * Getter accessor to $config property
46
     * 
47
     * @return array
48
     */
49
    public function getConfig()
50
    {
51
        return $this->config;
52
    }
53
    
54
    /**
55
     * Getter accessor to a value into the $config property
56
     * 
57
     * @param type $file
58
     * 
59
     * @return type
60
     */
61
    public function getConfigForFile($file)
62
    {
63
        return $this->config[$file];
64
    }
65
    
66
    /**
67
     * Search and load all config files which has been found
68
     * 
69
     * @return void
70
     */
71
    public function loadFiles()
72
    {
73
        $this->searchAllConfigFiles($this->configDir);
74
        
75
        foreach ($this->configFiles as $fileKey => $filePath) {
76
            $this->loadConfigFile($fileKey, $filePath);
77
        }
78
    }
79
80
    /**
81
     * Search all config files in a directory
82
     * Search also in sub-directory (2nd parameter)
83
     * 
84
     * @param string $dirPath The directory path where is run the search
85
     * @param string $pathIntoFirstDir (default '') Used when this method
86
     *  reads a subdirectory. It's the path of the readed directory during
87
     *  the first call to this method.
88
     * 
89
     * @return void
90
     */
91
    protected function searchAllConfigFiles($dirPath, $pathIntoFirstDir = '')
92
    {
93
        if (!file_exists($dirPath)) {
94
            return;
95
        }
96
97
        //Remove some value in list of file
98
        $listFiles = array_diff(scandir($dirPath), ['.', '..']);
99
100
        foreach ($listFiles as $file) {
101
            $fileKey  = $pathIntoFirstDir.$file;
102
            $readPath = $dirPath.'/'.$file;
103
104
            if (is_file($readPath)) {
105
                $this->configFiles[$fileKey] = $readPath;
106
            } elseif (is_link($readPath)) {
107
                $this->configFiles[$fileKey] = realpath($readPath);
108
            } elseif (is_dir($readPath)) {
109
                $this->searchAllConfigFiles(
110
                    $readPath,
111
                    $pathIntoFirstDir.$file.'/'
112
                );
113
            }
114
        }
115
    }
116
117
    /**
118
     * Load a config file.
119
     * Find the file's extension and call the method to parse the file
120
     * 
121
     * @param string $fileKey The file's key. Most of the time, the path to
122
     *  the file from the $this->configDir value
123
     * @param string $filePath The path to the file
124
     * 
125
     * @return void
126
     */
127
    protected function loadConfigFile($fileKey, $filePath)
128
    {
129
        $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
130
131
        if ($fileExtension === 'json') {
132
            $this->loadJsonConfigFile($fileKey, $filePath);
133
            return;
134
        }
135
136
        if ($fileExtension === 'php') {
137
            $this->loadPhpConfigFile($fileKey, $filePath);
138
            return;
139
        }
140
141
        //@TODO : YAML
142
    }
143
144
    /**
145
     * Load a json config file
146
     * 
147
     * @param string $fileKey The file's key. Most of the time, the path to
148
     *  the file from the $this->configDir value
149
     * @param string $filePath The path to the file
150
     * 
151
     * @return void
152
     * 
153
     * @throws Exception If there is an error from the json parser
154
     */
155
    protected function loadJsonConfigFile($fileKey, $filePath)
156
    {
157
        $json   = file_get_contents($filePath);
158
        $config = json_decode($json);
159
160
        if ($config === null) {
161
            throw new Exception(json_last_error_msg());
162
        }
163
164
        $this->config[$fileKey] = $config;
165
    }
166
167
    /**
168
     * Load a php config file
169
     * 
170
     * @param string $fileKey The file's key. Most of the time, the path to
171
     *  the file from the $this->configDir value
172
     * @param string $filePath The path to the file
173
     * 
174
     * @return void
175
     */
176
    protected function loadPhpConfigFile($fileKey, $filePath)
177
    {
178
        $this->config[$fileKey] = require($filePath);
179
    }
180
181
    /**
182
     * Return a config value for a key
183
     * 
184
     * @param string $key The asked key for the value
185
     * @param string $file (default null) If many file is loaded, the file name
186
     *  where is the key. Is the file is into a sub-directory, the
187
     *  sub-directory should be present.
188
     * 
189
     * @return mixed
190
     * 
191
     * @throws Exception If file parameter is null and there are many files. Or
192
     *  if the file not exist. Or if the key not exist.
193
     */
194
    public function getValue($key, $file = null)
195
    {
196
        $nbConfigFile = count($this->config);
197
198
        if ($file === null && $nbConfigFile > 1) {
199
            throw new Exception(
200
                'There are many config files. Please indicate the file to'
201
                .' obtain the config '.$key
202
            );
203
        }
204
205
        if ($nbConfigFile === 1) {
206
            $file = key($this->config);
207
        }
208
209
        if (!isset($this->config[$file])) {
210
            throw new Exception(
211
                'The file '.$file.' has not been found for config '.$key
212
            );
213
        }
214
215
        $config = (array) $this->config[$file];
216
        if (!array_key_exists($key, $config)) {
217
            throw new Exception('The config key '.$key.' has not been found');
218
        }
219
220
        return $config[$key];
221
    }
222
}
223