Passed
Push — master ( 581f43...7abc8c )
by Michael
02:10
created

ConfigItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 83
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A set() 0 11 2
A getValue() 0 4 1
A getMetadata() 0 4 1
A getHistory() 0 4 1
A trackMetadata() 0 4 1
A __clone() 0 4 1
1
<?php
2
3
namespace micmania1\config;
4
5
class ConfigItem implements ConfigItemInterface
6
{
7
    /**
8
     * @var mixed
9
     */
10
    protected $value;
11
12
    /**
13
     * @var array
14
     */
15
    protected $metaData = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $history = [];
21
22
    /**
23
     * @var boolean
24
     */
25
    protected $trackMetadata = false ;
26
27 25
    public function __construct($value, array $metaData = [], $trackMetadata = false)
28
    {
29 25
        $this->value = $value;
30 25
        $this->trackMetadata = (boolean) $trackMetadata;
31 25
        if($this->trackMetadata) {
32 2
            $this->metaData = $metaData;
33 2
        }
34 25
    }
35
36 23
    public function set($value, $metaData = [])
37
    {
38 23
        if($this->trackMetadata) {
39
            // Clone will clear the history to prevent recursion
40 2
            $previous = clone $this;
41 2
            array_unshift($this->history, $previous);
42 2
            $this->metaData = $metaData;
43 2
        }
44
45 23
        $this->value = $value;
46 23
    }
47
48
    /**
49
     * @return mixed
50
     */
51 25
    public function getValue()
52
    {
53 25
        return $this->value;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 25
    public function getMetadata()
60
    {
61 25
        return $this->metaData;
62
    }
63
64
    /**
65
     * Fetch the item history ordered in descending order by data
66
     *
67
     * @return CollectionItemInterface[]
68
     */
69 1
    public function getHistory()
70
    {
71 1
        return $this->history;
72
    }
73
74 22
    public function trackMetadata($track)
75
    {
76 22
        $this->trackMetadata = $track;
77 22
    }
78
79
    /**
80
     * History is an array of historical records of itself so we clear it to prevent
81
     * recursion.
82
     */
83 2
    public function __clone()
84
    {
85 2
        $this->history = [];
86 2
    }
87
}
88