Config   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 294
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 294
rs 9.68
c 0
b 0
f 0
wmc 34
lcom 1
cbo 3

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __get() 0 8 2
A getInstance() 0 8 2
A setConfig() 0 20 4
A get() 0 4 1
A getNested() 0 14 3
A has() 0 16 3
A getDefault() 0 6 2
A set() 0 18 4
A remove() 0 16 3
A refresh() 0 4 1
A toArray() 0 8 2
A makeArray() 0 8 2
A validate() 0 6 2
A merge() 0 4 1
A count() 0 4 1
1
<?php
2
3
namespace Yarak\Config;
4
5
use Phalcon\DI;
6
use Yarak\Exceptions\InvalidConfig;
7
use Phalcon\Config as PhalconConfig;
8
9
class Config
10
{
11
    use PathHelpers;
12
13
    /**
14
     * Instance of self.
15
     *
16
     * @var Config
17
     */
18
    private static $instance;
19
20
    /**
21
     * Phalcon config instance.
22
     *
23
     * @var PhalconConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * The original config array.
29
     *
30
     * @var array
31
     */
32
    protected $original;
33
34
    /**
35
     * Default setting values.
36
     *
37
     * @var array
38
     */
39
    const DEFAULTS = [
40
        'migratorType'        => 'fileDate',
41
        'migrationRepository' => 'database',
42
    ];
43
44
    /**
45
     * Private constructor.
46
     *
47
     * @param array $configArray
0 ignored issues
show
Bug introduced by
There is no parameter named $configArray. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     */
49
    private function __construct()
50
    {
51
    }
52
53
    /**
54
     * Get config values by key from PhalconConfig.
55
     *
56
     * @param string $key
57
     *
58
     * @return mixed
59
     */
60
    public function __get($key)
61
    {
62
        if (isset($this->config[$key])) {
63
            return $this->config->$key;
64
        }
65
66
        return $this->getDefault($key);
67
    }
68
69
    /**
70
     * Get instance of self with config array set.
71
     *
72
     * @param array $configArray
0 ignored issues
show
Bug introduced by
There is no parameter named $configArray. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     *
74
     * @return Config
75
     */
76
    public static function getInstance()
77
    {
78
        if (empty(self::$instance)) {
79
            self::$instance = new self();
80
        }
81
82
        return self::$instance;
83
    }
84
85
    /**
86
     * Set config using config data in di or passed data.
87
     *
88
     * @param array|string $userConfig If string, config path. If array, data.
89
     * @param bool         $merge      If true, merge given config array into current.
90
     */
91
    public function setConfig($userConfig = null, $merge = true)
92
    {
93
        $this->config = \Phalcon\Di::getDefault()->get('config');
94
95
        if (is_string($userConfig)) {
96
            $this->config = $this->getNested(explode('.', $userConfig));
97
        } elseif (is_array($userConfig)) {
98
            $userConfig = new PhalconConfig($userConfig);
99
100
            if ($merge === true) {
101
                $this->merge($userConfig);
102
            } else {
103
                $this->config = $userConfig;
104
            }
105
        }
106
107
        $this->original = $this->toArray();
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get a value by key from config.
114
     *
115
     * @param string|array $key
116
     *
117
     * @return mixed
118
     */
119
    public function get($key)
120
    {
121
        return $this->$key;
122
    }
123
124
    /**
125
     * Get nested values.
126
     *
127
     * @param array $keyArray
128
     *
129
     * @return mixed
130
     */
131
    public function getNested(array $keyArray)
132
    {
133
        $current = $this->config;
134
135
        foreach ($keyArray as $key) {
136
            if ($current[$key] === null) {
137
                return $this->getDefault($key);
138
            }
139
140
            $current = $current->$key;
141
        }
142
143
        return $current;
144
    }
145
146
    /**
147
     * Return true if config array has given value.
148
     *
149
     * @param mixed $value
150
     *
151
     * @return bool
152
     */
153
    public function has($value)
154
    {
155
        $current = $this->config;
156
157
        foreach ($this->makeArray($value) as $configItem) {
158
            if ($current[$configItem] === null) {
159
                $current = $this->getDefault($configItem);
160
161
                break;
162
            }
163
164
            $current = $current->$configItem;
165
        }
166
167
        return $current !== null;
168
    }
169
170
    /**
171
     * Get a setting's default value.
172
     *
173
     * @param string $key
174
     *
175
     * @return mixed|null
176
     */
177
    public function getDefault($key)
178
    {
179
        if (array_key_exists($key, self::DEFAULTS)) {
180
            return self::DEFAULTS[$key];
181
        }
182
    }
183
184
    /**
185
     * Set an item in the config.
186
     *
187
     * @param mixed $keys
188
     * @param mixed $value
189
     */
190
    public function set($keys, $value)
191
    {
192
        $temp = &$this->config;
193
194
        $keys = $this->makeArray($keys);
195
196
        $count = count($keys);
197
198
        foreach ($keys as $index => $key) {
199
            if ($count === $index + 1) {
200
                return $temp[$key] = $value;
201
            } elseif ($temp[$key] === null) {
202
                $temp[$key] = new PhalconConfig();
203
            }
204
205
            $temp = &$temp[$key];
206
        }
207
    }
208
209
    /**
210
     * Remove an item from the config.
211
     *
212
     * @param mixed $keys
213
     */
214
    public function remove($keys)
215
    {
216
        $temp = &$this->config;
217
218
        $keys = $this->makeArray($keys);
219
220
        $count = count($keys);
221
222
        foreach ($keys as $key) {
223
            if ($key === $keys[$count - 1]) {
224
                unset($temp[$key]);
225
            } else {
226
                $temp = &$temp[$key];
227
            }
228
        }
229
    }
230
231
    /**
232
     * Set the config array to its original values.
233
     */
234
    public function refresh()
235
    {
236
        $this->config = $this->config->__set_state($this->original);
237
    }
238
239
    /**
240
     * Return the config array.
241
     *
242
     * @return array
243
     */
244
    public function toArray()
245
    {
246
        if (is_string($this->config)) {
247
            return $this->config;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->config; (string) is incompatible with the return type documented by Yarak\Config\Config::toArray of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
248
        }
249
250
        return $this->config->toArray();
251
    }
252
253
    /**
254
     * Make a variable an array if not one already.
255
     *
256
     * @param mixed $value
257
     *
258
     * @return array
259
     */
260
    protected function makeArray($value)
261
    {
262
        if (!is_array($value)) {
263
            return [$value];
264
        }
265
266
        return $value;
267
    }
268
269
    /**
270
     * Validate that a setting exists.
271
     *
272
     * @param array $settings
273
     *
274
     * @throws InvalidConfig
275
     */
276
    public function validate(array $settings)
277
    {
278
        if (!$this->has($settings)) {
279
            throw InvalidConfig::configValueNotFound(implode(' -> ', $settings));
280
        }
281
    }
282
283
    /**
284
     * Merge the given config with the current one.
285
     *
286
     * @param PhalconConfig $config
287
     */
288
    public function merge(PhalconConfig $config)
289
    {
290
        $this->config = $this->config->merge($config);
291
    }
292
293
    /**
294
     * Return the config key count.
295
     *
296
     * @return int
297
     */
298
    public function count()
299
    {
300
        return $this->config->count();
301
    }
302
}
303