ConfigCollection   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 121
ccs 42
cts 42
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B set() 0 20 5
A get() 0 9 2
A exists() 0 5 1
A delete() 0 7 2
A deleteAll() 0 6 1
A getMetadata() 0 8 3
A getHistory() 0 8 3
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
    /**
16
     * @var array
17
     */
18
    protected $metadata = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected $history = [];
24
25
    /**
26
     * @var boolean
27
     */
28
    protected $trackMetadata = false;
29
30 24
    public function __construct($trackMetadata = false)
31
    {
32 24
        $this->trackMetadata = $trackMetadata;
33 24
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 21
    public function set($key, $value, $metadata = [])
39
    {
40 21
        $key = strtolower($key);
41 1
        if($this->trackMetadata) {
42 1
            if(isset($this->metadata[$key]) && isset($this->config[$key])) {
43 1
                if(!isset($this->history[$key])) {
44 1
                    $this->history[$key] = [];
45
                }
46 1
47 1
                array_unshift($this->history[$key], [
48 1
                    'value' => $this->config[$key],
49 1
                    'metadata' => $this->metadata[$key]
50 1
                ]);
51
            }
52 1
53 1
            $this->metadata[$key] = $metadata;
54
        }
55 21
56 21
        $this->config[$key] = $value;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61 21
     */
62
    public function get($key)
63 21
    {
64 12
        $key = strtolower($key);
65
        if(!$this->exists($key)) {
66
            return null;
67 20
        }
68
69
        return $this->config[$key];
70
    }
71
72
    /**
73 22
     * {@inheritdoc}
74
     */
75 22
    public function exists($key)
76
    {
77
        $key = strtolower($key);
78
        return isset($this->config[$key]);
79
    }
80
81 1
    /**
82
     * {@inheritdoc}
83 1
     */
84 1
    public function delete($key)
85 1
    {
86 1
        $key = strtolower($key);
87
        if($this->exists($key)) {
88
            unset($this->config[$key]);
89
        }
90
    }
91 1
92
    /**
93 1
     * {@inheritdoc}
94 1
     */
95 1
    public function deleteAll()
96 1
    {
97
        $this->config = [];
98
        $this->metadata = [];
99
        $this->history = [];
100
    }
101 2
102
    /**
103 2
     * {@inheritdoc}
104 1
     */
105
    public function getMetadata()
106
    {
107 1
        if(!$this->trackMetadata || !is_array($this->metadata)) {
108
            return [];
109
        }
110
111
        return $this->metadata;
112
    }
113 2
114
    /**
115 2
     * {@inheritdoc}
116 1
     */
117
    public function getHistory()
118
    {
119 1
        if(!$this->trackMetadata || !is_array($this->history)) {
120
            return [];
121
        }
122
123
        return $this->history;
124
    }
125
}
126