Configuration   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A merge() 0 5 1
A mergeArray() 0 10 2
A getMergedValue() 0 8 3
1
<?php
2
3
namespace Brofist\Configuration;
4
5
class Configuration implements ConfigurationInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    private $config;
11
12
    public function __construct(array $config = [])
13
    {
14
        $this->config = $config;
15
    }
16
17
    public function toArray()
18
    {
19
        return $this->config;
20
    }
21
22
    public function merge(ConfigurationInterface $other)
23
    {
24
        $arrayConfig = $this->mergeArray($this->toArray(), $other->toArray());
25
        return new self($arrayConfig);
26
    }
27
28
    /**
29
     * @return array
30
     */
31
    protected function mergeArray(array $original, array $other)
32
    {
33
        $newConfig = $original;
34
35
        foreach ($other as $key => $value) {
36
            $newConfig[$key] = $this->getMergedValue($newConfig[$key], $value);
37
        }
38
39
        return $newConfig;
40
    }
41
42
    /**
43
     * @param mixed $old
44
     * @param mixed $old
45
     *
46
     * @return mixed
47
     */
48
    protected function getMergedValue($old, $new)
49
    {
50
        if (is_array($old) && is_array($new)) {
51
            return array_merge($old, $new);
52
        }
53
54
        return $new;
55
    }
56
}
57