Test Failed
Branch add-cache (cab8ca)
by Michael
02:14
created

ConfigCollection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 107
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

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