Settings::setDefault()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
namespace PSB\Core\Util;
3
4
5
use PSB\Core\Exception\RuntimeException;
6
use PSB\Core\Exception\OutOfBoundsException;
7
8
class Settings implements ReadOnlySettingsInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $defaults = [];
14
15
    /**
16
     * @var array
17
     */
18
    private $overrides = [];
19
20
    /**
21
     * @var bool
22
     */
23
    private $locked = false;
24
25
    /**
26
     * Returns the value for the $key. Throws an exception is value is not found in overrides.
27
     *
28
     * @param string $key
29
     *
30
     * @throws OutOfBoundsException
31
     * @return mixed
32
     */
33 3 View Code Duplication
    public function get($key)
34
    {
35 3
        if (isset($this->overrides[$key])) {
36 1
            return $this->overrides[$key];
37
        }
38
39 2
        if (isset($this->defaults[$key])) {
40 1
            return $this->defaults[$key];
41
        }
42
43 1
        throw new OutOfBoundsException("The given key '$key' was not present in the settings.");
44
    }
45
46
    /**
47
     * Returns the value for the $key searching through overrides and defaults. Returns null if not found.
48
     *
49
     * @param string $key
50
     *
51
     * @return mixed
52
     */
53 4 View Code Duplication
    public function tryGet($key)
54
    {
55 4
        if (isset($this->overrides[$key])) {
56 1
            return $this->overrides[$key];
57
        }
58
59 3
        if (isset($this->defaults[$key])) {
60 2
            return $this->defaults[$key];
61
        }
62
63 2
        return null;
64
    }
65
66
    /**
67
     * Returns true if there is an override or a default value for $key
68
     *
69
     * @param string $key
70
     *
71
     * @return bool
72
     */
73 3
    public function has($key)
74
    {
75 3
        if (isset($this->overrides[$key]) || isset($this->defaults[$key])) {
76 2
            return true;
77
        }
78
79 1
        return false;
80
    }
81
82
    /**
83
     * @param string $key
84
     * @param mixed  $value
85
     */
86 5
    public function set($key, $value)
87
    {
88 5
        $this->assertWriteable();
89 4
        $this->overrides[$key] = $value;
90 4
    }
91
92
    /**
93
     * @param string $key
94
     * @param mixed  $value
95
     */
96 5
    public function setDefault($key, $value)
97
    {
98 5
        $this->assertWriteable();
99 4
        $this->defaults[$key] = $value;
100 4
    }
101
102 9
    private function assertWriteable()
103
    {
104 9
        if ($this->locked) {
105 2
            throw new RuntimeException("Settings container has been write private.");
106
        }
107 7
    }
108
109 2
    public function preventChanges()
110
    {
111 2
        $this->locked = true;
112
    }
113
}