Completed
Push — master ( 2b75e8...156030 )
by Faiz
02:20
created

Config   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 70.59%

Importance

Changes 7
Bugs 3 Features 4
Metric Value
wmc 11
c 7
b 3
f 4
lcom 1
cbo 0
dl 0
loc 89
ccs 24
cts 34
cp 0.7059
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildConfig() 0 17 3
A get() 0 11 2
A all() 0 4 1
A customTranslations() 0 20 4
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
        $this->config['translations'] = array_merge($this->config['translations'], $this->customTranslations());
13 78
    }
14
15
    /**
16
     * Build a config array. Merge user defined config with our default config.
17
     *
18
     * @param array $config User defined config
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
        // Merge translation with custom translation variable
34
        
35
36 90
        return $result;
37
    }
38
39
    /**
40
     * Get the config variable.
41
     *
42
     * @param string $val Variable name
43
     * @return array|string Variable value
44
     */
45 78
    public function get($val)
46
    {
47 78
        $configs = explode('.', $val);
48 78
        $first = $this->config[array_shift($configs)];
49
50 78
        foreach ($configs as $config) {
51 63
            $first = $first[$config];
52 52
        }
53
54 78
        return $first;
55
    }
56
57
    /**
58
     * Return all configurations.
59
     *
60
     * @return array
61
     */
62
    public function all()
63
    {
64
        return $this->config;
65
    }
66
67
    /**
68
     * Set/get custom translation
69
     *
70
     * @param string $id Translation ID
71
     * @return array|void
72
     */
73 90
    public function customTranslations($id = null)
74
    {
75 90
        $path = $this->config['storage_path'] . '/translation';
76
77 90
        $file = fopen($path, "a+");
78 78
        $contents = fread($file, filesize($path) ?: 1);
79
80 78
        $customTranslations = array_filter(explode(",", $contents));
81
82 78
        if ($id === null) {
83 78
            fclose($file);
84 78
            return $customTranslations;
85
        } else {
86
            if(!in_array($id, $customTranslations)){
87
                array_push($this->config['translations'], $id);
88
                fwrite($file, ",$id");
89
                fclose($file);
90
            }
91
        }
92
    }
93
}
94