Passed
Push — master ( 18ff88...32a943 )
by Michael
39s
created

ConfigCollection   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 84
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A set() 0 13 3
A get() 0 8 2
A exists() 0 4 1
A clear() 0 6 2
A keys() 0 4 1
A all() 0 4 1
1
<?php
2
3
namespace micmania1\config;
4
5
class ConfigCollection implements ConfigCollectionInterface
6
{
7
8
    /**
9
     * Stores a list of key/value config.
10
     *
11
     * @var array
12
     */
13
    protected $config = [];
14
15 25
    public function __construct(array $config = [])
16
    {
17 25
        foreach($config as $key => $item) {
18 19
            if(!($item instanceof ConfigItemInterface)) {
19 14
                $item = new ConfigItem($item);
20 14
            }
21
22 19
            $this->config[$key] = $item;
23 25
        }
24 25
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 17
    public function set($key, $item)
30
    {
31 17
        if(!($item instanceof ConfigItemInterface)) {
32 2
            $item = new ConfigItem($item);
33 2
        }
34
35 17
        if(!$this->exists($key)) {
36 17
            $this->config[$key] = $item;
37 17
        }
38
39 17
        $existing = $this->config[$key];
40 17
        $existing->set($item->getValue(), $item->getMetaData());
41 17
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 22
    public function get($key)
47
    {
48 22
        if(!$this->exists($key)) {
49 1
            return null;
50
        }
51
52 22
        return $this->config[$key];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 22
    public function exists($key)
59
    {
60 22
        return array_key_exists($key, $this->config);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function clear($key)
67
    {
68 1
        if($this->exists($key)) {
69 1
            unset($this->config[$key]);
70 1
        }
71 1
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 7
    public function keys()
77
    {
78 7
        return array_keys($this->config);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 21
    public function all()
85
    {
86 21
        return $this->config;
87
    }
88
}
89