Completed
Push — master ( 4bc2d8...2b75e8 )
by Faiz
04:14
created

Config::buildConfig()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 4
Bugs 2 Features 2
Metric Value
c 4
b 2
f 2
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3.2098
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class Config
6
{
7
    private $config;
8
9 90
    public function __construct(array $config = [])
10
    {
11 90
        $this->config = $this->buildConfig($config);
12 90
    }
13
14
    /**
15
     * Build a config array. Merge user defined config with our default config.
16
     *
17
     * @param array $config User defined config
18
     *
19
     * @return array New configuration array
20
     */
21 90
    private function buildConfig(array $config = [])
22
    {
23
        // Merge our config with user config
24 90
        $result = array_replace_recursive((include realpath(__DIR__.'/../../config/quran.php')), $config);
25
26
        // If function storage_path is exist (laravel), we update the path to laravel's storage path
27 90
        if (function_exists('storage_path') && php_sapi_name() !== 'cli') {
28
            $result['storage_path'] = storage_path('app/' . $result['storage_path']);
29
        } else {
30 90
            $result['storage_path'] = realpath(__DIR__ . '/../..') . '/' . $result['storage_path'];
31
        }
32
33 90
        return $result;
34
    }
35
36
    /**
37
     * Get the config variable.
38
     *
39
     * @param string $val Variable name
40
     *
41
     * @return array|string Variable value
42
     */
43 87
    public function get($val)
44
    {
45 87
        $configs = explode('.', $val);
46 87
        $first = $this->config[array_shift($configs)];
47
48 87
        foreach ($configs as $config) {
49 69
            $first = $first[$config];
50 58
        }
51
52 87
        return $first;
53
    }
54
55
    /**
56
     * Return all configurations.
57
     *
58
     * @return array
59
     */
60 3
    public function all()
61
    {
62 3
        return $this->config;
63
    }
64
}
65