Test Failed
Branch add-cache (2af103)
by Michael
02:53
created

ConfigCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 14 2
A get() 0 8 2
A exists() 0 4 1
A delete() 0 6 2
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 boolean
17
     */
18
    protected $trackMetadata = false;
19
20
    public function __construct($trackMetadata = false)
21
    {
22
        $this->trackMetadata = $trackMetadata;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function set($key, ConfigItemInterface $item)
29
    {
30
        if(!$this->exists($key)) {
31
            $this->config[$key] = $item;
32
        }
33
34
        // Get the existing item so we can set the new value on it
35
        $existing = $this->config[$key];
36
37
        // Ensure that that tracking is correct for items belonging to this collection
38
        $existing->trackMetadata($this->trackMetadata);
39
40
        $existing->set($item->getValue(), $item->getMetadata());
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function get($key)
47
    {
48
        if(!$this->exists($key)) {
49
            return null;
50
        }
51
52
        return $this->config[$key];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function exists($key)
59
    {
60
        return array_key_exists($key, $this->config);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function delete($key)
67
    {
68
        if($this->exists($key)) {
69
            unset($this->config[$key]);
70
        }
71
    }
72
}
73