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

ConfigCollection::set()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 3
crap 5
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