Completed
Push — master ( 6ab195...1e296c )
by Faiz
02:51
created

Config   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 76
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 11 2
A buildConfig() 0 18 3
A all() 0 4 1
A addTranslation() 0 11 2
1
<?php
2
3
namespace FaizShukri\Quran\Supports;
4
5
class Config
6
{
7
    private $config;
8
9
    public $configFile;
10
11 93
    public function __construct(array $config = [])
12
    {
13 93
        $this->config = $this->buildConfig($config);
14 93
    }
15
16
    /**
17
     * Build a config array. Merge user defined config with our default config.
18
     *
19
     * @param array $config User defined config
20
     * @return array New configuration array
21
     */
22 93
    private function buildConfig(array $config = [])
23
    {
24 93
        $this->configFile = realpath(__DIR__ . '/../../config/quran.php');
25
26
        // Merge our config with user config
27 93
        $result = array_replace_recursive((include $this->configFile), $config);
28
29
        // If function storage_path is exist (laravel), we update the path to laravel's storage path
30 93
        if (function_exists('storage_path') && php_sapi_name() !== 'cli') {
31
            $result['storage_path'] = storage_path('app/' . $result['storage_path']);
32
        } else {
33 93
            $result['storage_path'] = realpath(__DIR__ . '/../..') . '/' . $result['storage_path'];
34
        }
35
36
        // Merge translation with custom translation variable
37
38 93
        return $result;
39
    }
40
41
    /**
42
     * Get the config variable.
43
     *
44
     * @param string $val Variable name
45
     * @return array|string Variable value
46
     */
47 123
    public function get($val)
48
    {
49 123
        $configs = explode('.', $val);
50 123
        $first = $this->config[array_shift($configs)];
51
52 123
        foreach ($configs as $config) {
53 87
            $first = $first[$config];
54
        }
55
56 123
        return $first;
57
    }
58
59
    /**
60
     * Return all configurations.
61
     *
62
     * @return array
63
     */
64 3
    public function all()
65
    {
66 3
        return $this->config;
67
    }
68
69 3
    public function addTranslation($id)
70
    {
71 3
        if (in_array($id, $this->config['translations'])) {
72
            return;
73
        }
74
75 3
        $configString = file_get_contents($this->configFile, true);
76 3
        $result = preg_replace('/([\'\"]translations[\'\"]\s+\=\>\s+\[[a-z\"\'\.\,\s]+)/i', "\\1, '$id'", $configString);
77 3
        file_put_contents($this->configFile, $result);
78 3
        array_push($this->config['translations'], $id);
79 3
    }
80
}
81