Config::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\WP\Support\Facades;
4
5
class Config
6
{
7
    protected $config = [];
8
9
    public function __construct($options = [])
10
    {
11
        $dir = __DIR__ . '/../../../../../../';
12
13
        if (! empty($options) && isset($options['paths']['root'])) {
14
            $dir = rtrim($options['paths']['root'], "/") . '/';
15
        }
16
        
17
        foreach (glob($dir . 'config/*.php') as $file) {
18
            $index = pathinfo($file)['filename'];
19
            $this->config[$index] = require_once $file;
20
        }
21
    }
22
23
    public function get($config, $default = null)
24
    {
25
        $keys = explode('.', $config);
26
        $filename = array_shift($keys);
27
        $data = $this->config[$filename];
28
29
        foreach ($keys as $key) {
30
            if (is_array($data) && array_key_exists($key, $data)) {
31
                $data = $data[$key];
32
            } else {
33
                $data = null;
34
            }
35
        }
36
37
        if (!$data) {
38
            $data = $default;
39
        }
40
41
        return $data;
42
    }
43
}
44