Config   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 0

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 103
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A has() 0 10 3
A getDefault() 0 8 2
A set() 0 6 1
A setFallback() 0 6 1
1
<?php
2
3
namespace League\Flysystem;
4
5
class Config
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $settings = [];
11
12
    /**
13
     * @var Config
14
     */
15
    protected $fallback;
16
17
    /**
18
     * Constructor.
19
     *
20
     * @param array $settings
21
     */
22 390
    public function __construct(array $settings = [])
23
    {
24 390
        $this->settings = $settings;
25 390
    }
26
27
    /**
28
     * Get a setting.
29
     *
30
     * @param string $key
31
     * @param mixed  $default
32
     *
33
     * @return mixed config setting or default when not found
34
     */
35 228
    public function get($key, $default = null)
36
    {
37 228
        if ( ! array_key_exists($key, $this->settings)) {
38 210
            return $this->getDefault($key, $default);
39
        }
40
41 24
        return $this->settings[$key];
42
    }
43
44
    /**
45
     * Check if an item exists by key.
46
     *
47
     * @param string $key
48
     *
49
     * @return bool
50
     */
51 6
    public function has($key)
52
    {
53 6
        if (array_key_exists($key, $this->settings)) {
54 3
            return true;
55
        }
56
57 6
        return $this->fallback instanceof Config
58 6
            ? $this->fallback->has($key)
59 6
            : false;
60
    }
61
62
    /**
63
     * Try to retrieve a default setting from a config fallback.
64
     *
65
     * @param string $key
66
     * @param mixed  $default
67
     *
68
     * @return mixed config setting or default when not found
69
     */
70 210
    protected function getDefault($key, $default)
71
    {
72 210
        if ( ! $this->fallback) {
73 210
            return $default;
74
        }
75
76 21
        return $this->fallback->get($key, $default);
77
    }
78
79
    /**
80
     * Set a setting.
81
     *
82
     * @param string $key
83
     * @param mixed  $value
84
     *
85
     * @return $this
86
     */
87 9
    public function set($key, $value)
88
    {
89 9
        $this->settings[$key] = $value;
90
91 9
        return $this;
92
    }
93
94
    /**
95
     * Set the fallback.
96
     *
97
     * @param Config $fallback
98
     *
99
     * @return $this
100
     */
101 69
    public function setFallback(Config $fallback)
102
    {
103 69
        $this->fallback = $fallback;
104
105 69
        return $this;
106
    }
107
}
108