Passed
Push — master ( a069d9...d155c7 )
by Michael
02:21
created

ConfigCollection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 117
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 117
ccs 42
cts 42
cp 1
rs 10

8 Methods

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