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

ConfigCollection::set()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 15
cts 15
cp 1
rs 8.8571
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
    /**
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