Properties   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 51
c 1
b 0
f 0
dl 0
loc 134
rs 10
wmc 25

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateConfig() 0 10 3
B get() 0 35 9
A writeConfig() 0 12 3
A getAll() 0 8 3
A load() 0 15 5
A __construct() 0 4 2
1
<?php
2
3
namespace Ffcms\Core;
4
5
use Ffcms\Core\Exception\NativeException;
6
use Ffcms\Core\Helper\FileSystem\File;
7
use Ffcms\Core\Helper\Type\Any;
8
use Ffcms\Core\Helper\Type\Arr;
9
use Ffcms\Core\Helper\Type\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class Properties. Provide methods to work with ffcms configs.
14
 * @package Ffcms\Core
15
 */
16
class Properties
17
{
18
    protected $data;
19
20
    /**
21
     * Load default configuration
22
     * @throws NativeException
23
     */
24
    public function __construct()
25
    {
26
        if (!$this->load('default')) {
27
            throw new NativeException('Default configurations is not founded: /Private/Config/Default.php');
28
        }
29
    }
30
31
    /**
32
     * Load configurations from file in /Private/Config/
33
     * @param string $configName
34
     * @param bool $overload
35
     * @return bool
36
     */
37
    private function load(string $configName, $overload = false): bool
38
    {
39
        // check if always loaded
40
        if (Any::isArray($this->data) && array_key_exists($configName, $this->data) && !$overload) {
41
            return true;
42
        }
43
44
        // try to load from file
45
        $configFile = ucfirst(Str::lowerCase($configName)) . '.php';
46
        if (File::exist('/Private/Config/' . $configFile)) {
47
            $this->data[$configName] = File::inc('/Private/Config/' . $configFile, true);
48
            return true;
49
        }
50
51
        return false;
52
    }
53
54
    /**
55
     * Get config value by config key from configuration file
56
     * @param string $configKey
57
     * @param string $configFile
58
     * @param string|null $parseType
59
     * @return mixed
60
     */
61
    public function get(string $configKey, string $configFile = 'default', ?string $parseType = null)
62
    {
63
        $this->load($configFile);
64
        // check if configs for this file is loaded
65
        if (!isset($this->data[$configFile])) {
66
            return false;
67
        }
68
69
        // check if config key is exist
70
        if (!isset($this->data[$configFile][$configKey])) {
71
            return false;
72
        }
73
74
        $response = $this->data[$configFile][$configKey];
75
76
        // try to convert config value by defined parse type
77
        $parseType = Str::lowerCase($parseType);
78
        switch ($parseType) {
79
            case 'int':
80
            case 'integer':
81
                $response = (int)$response;
82
                break;
83
            case 'bool':
84
            case 'boolean':
85
                $response = (bool)$response;
86
                break;
87
            case 'float':
88
                $response = (float)$response;
89
                break;
90
            case 'double':
91
                $response = (double)$response;
92
                break;
93
        }
94
95
        return $response;
96
    }
97
98
    /**
99
     * Get all configuration data of selected file
100
     * @param string $configFile
101
     * @return array|null
102
     */
103
    public function getAll($configFile = 'default'): ?array
104
    {
105
        $this->load($configFile);
106
        if (!Any::isArray($this->data) || !array_key_exists($configFile, $this->data)) {
107
            return null;
108
        }
109
110
        return $this->data[$configFile];
111
    }
112
113
    /**
114
     * Update configuration data based on key-value array of new data. Do not pass multi-level array on new position without existing keys
115
     * @param string $configFile
116
     * @param array $newData
117
     * @param bool $mergeDeep
118
     * @return bool
119
     */
120
    public function updateConfig(string $configFile, array $newData, ?bool $mergeDeep = false): bool
121
    {
122
        $this->load($configFile);
123
        if (!isset($this->data[$configFile])) {
124
            return false;
125
        }
126
127
        $oldData = $this->data[$configFile];
128
        $saveData = ($mergeDeep ? Arr::mergeRecursive($oldData, $newData) : Arr::merge($oldData, $newData));
129
        return $this->writeConfig($configFile, $saveData);
130
    }
131
132
    /**
133
     * Write configurations data from array to cfg file
134
     * @param string $configFile
135
     * @param array $data
136
     * @return bool
137
     */
138
    public function writeConfig(string $configFile, array $data): bool
139
    {
140
        $path = '/Private/Config/' . ucfirst(Str::lowerCase($configFile)) . '.php';
141
        if (!File::exist($path) || !File::writable($path)) {
142
            return false;
143
        }
144
145
        $saveData = '<?php return ' . Arr::exportVar($data) . ';';
146
        File::write($path, $saveData);
147
        // overload config values if changed
148
        $this->load($configFile, true);
149
        return true;
150
    }
151
}
152