Completed
Pull Request — master (#31)
by Greg
01:30
created

SiteAliasWithConfig   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 3
dl 0
loc 145
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getConfig() 0 4 1
A name() 0 4 1
A setName() 0 4 1
A has() 0 7 3
A get() 0 14 4
A set() 0 4 1
A import() 0 4 1
A replace() 0 4 1
A combine() 0 4 1
A export() 0 4 1
A hasDefault() 0 4 1
A getDefault() 0 4 1
A setDefault() 0 4 1
A exportConfig() 0 4 1
A changesProhibited() 0 4 1
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
use Consolidation\Config\ConfigInterface;
5
use Consolidation\SiteAlias\AliasRecord;
6
use Consolidation\SiteAlias\AliasRecordInterface;
7
use Consolidation\SiteAlias\AliasRecordTrait;
8
9
/**
10
 * SiteAliasWithConfig delegates to a site alias, and
11
 * also combines it with two config stores:
12
 *
13
 *   - Runtime config (set on commandline): Options that override site alias contents
14
 *   - Default config (set from config files): Default options
15
 */
16
class SiteAliasWithConfig implements AliasRecordInterface
17
{
18
    use AliasRecordTrait;
19
20
    protected $runtimeConfig;
21
    protected $siteAlias;
22
    protected $defaultConfig;
23
24
    public function __construct(ConfigInterface $runtimeConfig, AliasRecordInterface $siteAlias, ConfigInterface $defaultConfig)
25
    {
26
        $this->runtimeConfig = $runtimeConfig;
27
        $this->siteAlias = $siteAlias;
28
        $this->defaultConfig = $defaultConfig;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function getConfig(ConfigInterface $config, $key, $default = null)
35
    {
36
        throw new \Exception('getConfig is deprecated, and not supported in SiteAliasWithConfig (which replaces it). Use "get".');
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function name()
43
    {
44
        return $this->siteAlias()->name();
0 ignored issues
show
Bug introduced by
The method siteAlias() does not seem to exist on object<Consolidation\Sit...as\SiteAliasWithConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function setName($name)
51
    {
52
        $this->changesProhibited();
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function has($key)
59
    {
60
        return
61
            $this->runtimeConfig->has($key) ||
62
            $this->siteAlias->has($key) ||
63
            $this->defaultConfig->has($key);
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function get($key, $defaultFallback = null)
70
    {
71
        if ($this->runtimeConfig->has($key)) {
72
            return $this->runtimeConfig->get($key);
73
        }
74
        if ($this->siteAlias->has($key)) {
75
            return $this->siteAlias->get($key);
76
        }
77
        if ($this->defaultConfig->has($key)) {
78
            return $this->defaultConfig->get($key);
79
        }
80
81
        return $defaultFallback;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function set($key, $value)
88
    {
89
        $this->changesProhibited();
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95
    public function import($data)
96
    {
97
        $this->changesProhibited();
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function replace($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105
        $this->changesProhibited();
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function combine($data)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        $this->changesProhibited();
114
    }
115
116
    /**
117
     * Export all configuration as a nested array.
118
     */
119
    public function export()
120
    {
121
        throw new \Exception('SiteAliasWithConfig::export() not supported.');
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function hasDefault($key)
128
    {
129
        return false;
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    public function getDefault($key, $defaultFallback = null)
136
    {
137
        return $defaultFallback;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function setDefault($key, $value)
144
    {
145
        $this->changesProhibited();
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function exportConfig()
152
    {
153
        throw new \Exception('SiteAliasWithConfig::exportConfig() not supported.');
154
    }
155
156
    protected function changesProhibited()
157
    {
158
        throw new \Exception('Changing a SiteAliasWithConfig is not permitted.');
159
    }
160
}
161