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 ( f7dde3...9c92ff )
by Vermeulen
02:27
created

Config::loadFiles()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
7
class Config
8
{
9
    protected $configDirName = '';
10
11
    protected $configDir     = '';
12
13
    protected $configFiles   = [];
14
15
    protected $config        = [];
16
17
    public function __construct($configDirName)
18
    {
19
        $this->configDirName = $configDirName;
20
        $this->configDir     = CONFIG_DIR.$this->configDirName;
21
    }
22
    
23
    public function loadFiles()
24
    {
25
        $this->searchAllConfigsFiles($this->configDir);
26
        
27
        foreach ($this->configFiles as $fileKey => $filePath) {
28
            $this->loadConfigFile($fileKey, $filePath);
29
        }
30
    }
31
32
    protected function searchAllConfigsFiles($dirPath, $pathFromRoot = '')
33
    {
34
        if (!file_exists($dirPath)) {
35
            return;
36
        }
37
38
        $listFiles = array_diff(scandir($dirPath), ['.', '..']);
39
40
        foreach ($listFiles as $file) {
41
            $keyFile  = $pathFromRoot.$file;
42
            $readPath = $dirPath.'/'.$file;
43
44
            if (is_file($readPath)) {
45
                $this->configFiles[$keyFile] = $readPath;
46
                continue;
47
            }
48
49
            if (is_link($readPath)) {
50
                $this->configFiles[$keyFile] = realpath($readPath);
51
                continue;
52
            }
53
54
            if (is_dir($readPath)) {
55
                $this->searchAllConfigsFiles($readPath, $pathFromRoot.$file.'/');
56
                continue;
57
            }
58
        }
59
    }
60
61
    protected function loadConfigFile($fileKey, $filePath)
62
    {
63
        $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION);
64
65
        if ($fileExtension === 'json') {
66
            $this->loadJsonConfigFile($fileKey, $filePath);
67
            return;
68
        }
69
70
        if ($fileExtension === 'php') {
71
            $this->loadPhpConfigFile($fileKey, $filePath);
72
            return;
73
        }
74
75
        //@TODO : YAML
76
    }
77
78
    protected function loadJsonConfigFile($fileKey, $filePath)
79
    {
80
        $json   = file_get_contents($filePath);
81
        $config = json_decode($json);
82
83
        if ($config === null) {
84
            throw new Exception(json_last_error_msg());
85
        }
86
87
        $this->config[$fileKey] = $config;
88
    }
89
90
    protected function loadPhpConfigFile($fileKey, $filePath)
91
    {
92
        $this->config[$fileKey] = require($filePath);
93
    }
94
95
    public function getConfig($key, $file = null)
96
    {
97
        $nbConfigFile = count($this->config);
98
99
        if ($file === null && $nbConfigFile > 1) {
100
            throw new Exception('Please indicate a file for get config '.$key);
101
        }
102
103
        if ($nbConfigFile === 1) {
104
            $file = key($this->config);
105
        }
106
107
        if (!isset($this->config[$file])) {
108
            throw new Exception('The file '.$file.' not exist for config '.$key);
109
        }
110
111
        $config = (array) $this->config[$file];
112
113
        if (!isset($config[$key])) {
114
            throw new Exception('The config key '.$key.' not exist in config');
115
        }
116
117
        return $config[$key];
118
    }
119
}
120