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

ConfigItem::trackMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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