Config   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 63
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 19 5
A __construct() 0 11 4
1
<?php
2
/**
3
 * The file handle the config.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace WPB\Support\Facades;
10
11
/**
12
 * The config class.
13
 *
14
 * @since      1.0.0
15
 *
16
 * @author     Md Abu Ahsan basir <[email protected]>
17
 */
18
class Config
19
{
20
    /**
21
     * The config.
22
     *
23
     * @since    1.0.0
24
     *
25
     * @var array The config array.
26
     */
27
    protected $config = [];
28
29
    /**
30
     * Factory class.
31
     *
32
     * @since    1.0.0
33
     *
34
     * @param array $options The defaut configuration option.
35
     *
36
     * @return void
37
     */
38
    public function __construct($options = [])
39
    {
40
        $dir = __DIR__.'/../../../../../../';
41
42
        if (!empty($options) && isset($options['paths']['root'])) {
43
            $dir = rtrim($options['paths']['root'], '/').'/';
44
        }
45
46
        foreach (glob($dir.'config/*.php') as $file) {
47
            $index = pathinfo($file)['filename'];
48
            $this->config[$index] = require_once $file;
49
        }
50
    }
51
52
    /**
53
     * Get the config value.
54
     *
55
     * @since    1.0.0
56
     *
57
     * @param string $config  The config key.
58
     * @param string $default The default config value.
59
     *
60
     * @return null|string
61
     */
62
    public function get($config, $default = null)
63
    {
64
        $keys = explode('.', $config);
65
        $filename = array_shift($keys);
66
        $data = $this->config[$filename];
67
68
        foreach ($keys as $key) {
69
            if (is_array($data) && array_key_exists($key, $data)) {
70
                $data = $data[$key];
71
            } else {
72
                $data = null;
73
            }
74
        }
75
76
        if (!$data) {
77
            $data = $default;
78
        }
79
80
        return $data;
81
    }
82
}
83