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 ( 572920...bfe862 )
by Vermeulen
02:24
created

Config::searchAllConfigFiles()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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