Completed
Pull Request — master (#586)
by Greg
03:26
created

Config   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 0
loc 224
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A has() 0 4 1
A get() 0 7 2
A set() 0 5 1
A import() 0 8 2
A export() 0 4 1
A applyConfiguration() 0 11 3
A getGlobalOptionDefaultValues() 0 10 1
A getDefault() 0 4 2
A setDefault() 0 5 1
A isSimulated() 0 4 1
A setSimulated() 0 4 1
A isInteractive() 0 4 1
A setInteractive() 0 4 1
A isDecorated() 0 4 1
A setDecorated() 0 4 1
A setProgressBarAutoDisplayInterval() 0 4 1
1
<?php
2
namespace Robo\Config;
3
4
use Dflydev\DotAccessData\Data;
5
6
class Config
7
{
8
    const PROGRESS_BAR_AUTO_DISPLAY_INTERVAL = 'progress-delay';
9
    const DEFAULT_PROGRESS_DELAY = 2;
10
    const SIMULATE = 'simulate';
11
    const INTERACTIVE = 'interactive';
12
    const DECORATED = 'decorated';
13
14
    /**
15
     * @var Data
16
     */
17
    protected $config;
18
19
    /**
20
     * @var array
21
     */
22
    protected $defaults;
23
24
    /**
25
     * Create a new configuration object, and initialize it with
26
     * the provided nested array containing configuration data.
27
     */
28
    public function __construct(array $data = null)
29
    {
30
        $this->config = new Data($data);
31
        $this->defaults = $this->getGlobalOptionDefaultValues();
32
    }
33
34
    /**
35
     * Determine if a non-default config value exists.
36
     */
37
    public function has($key)
38
    {
39
        return ($this->config->has($key));
40
    }
41
42
    /**
43
     * Fetch a configuration value
44
     *
45
     * @param string $key Which config item to look up
46
     * @param string|null $defaultOverride Override usual default value with a different default. Deprecated; provide defaults to the config processor instead.
47
     *
48
     * @return mixed
49
     */
50
    public function get($key, $defaultOverride = null)
51
    {
52
        if ($this->has($key)) {
53
            return $this->config->get($key);
54
        }
55
        return $this->getDefault($key, $defaultOverride);
56
    }
57
58
    /**
59
     * Set a config value
60
     *
61
     * @param string $key
62
     * @param mixed $value
63
     *
64
     * @return $this
65
     */
66
    public function set($key, $value)
67
    {
68
        $this->config->set($key, $value);
69
        return $this;
70
    }
71
72
    /**
73
     * Import configuration from the provided nexted array, replacing whatever
74
     * was here previously. No processing is done on the provided data.
75
     *
76
     * @param array $data
77
     * @return Config
78
     */
79
    public function import($data)
80
    {
81
        $this->config = new Data($data);
82
        if (!empty($data)) {
83
            $this->config->import($data, true);
84
        }
85
        return $this;
86
    }
87
88
    /**
89
     * Export all configuration as a nested array.
90
     */
91
    public function export()
92
    {
93
        return $this->config->export();
94
    }
95
96
    /**
97
     * Given an object that contains configuration methods, inject any
98
     * configuration found in the configuration file.
99
     *
100
     * The proper use for this method is to call setter methods of the
101
     * provided object. Using configuration to call methods that do work
102
     * is an abuse of this mechanism.
103
     *
104
     * TODO: We could use reflection to test to see if the return type
105
     * of the provided object is a reference to the object itself. All
106
     * setter methods should do this. This test is insufficient to guarentee
107
     * that the method is valid, but it would be a good start.
108
     */
109
    public function applyConfiguration($object, $configurationKey)
110
    {
111
        if ($this->has($configurationKey)) {
112
            $settings = $this->get($configurationKey);
113
            foreach ($settings as $setterMethod => $args) {
114
                // TODO: Should it be possible to make $args a nested array
115
                // to make this code call the setter method multiple times?
116
                call_user_func_array([$object, $setterMethod], (array)$args);
117
            }
118
        }
119
    }
120
121
    /**
122
     * Return an associative array containing all of the global configuration
123
     * options and their default values.
124
     *
125
     * @return array
126
     */
127
    public function getGlobalOptionDefaultValues()
128
    {
129
        $globalOptions =
130
        [
131
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
132
            self::SIMULATE => false,
133
        ];
134
135
        return $globalOptions;
136
    }
137
138
    /**
139
     * Return the default value for a given configuration item.
140
     *
141
     * @param string $key
142
     * @param mixed $defaultOverride
143
     *
144
     * @return mixed
145
     */
146
    public function getDefault($key, $defaultOverride = null)
147
    {
148
        return isset($this->defaults[$key]) ? $this->defaults[$key] : $defaultOverride;
149
    }
150
151
    /**
152
     * Set the default value for a configuration setting. This allows us to
153
     * set defaults either before or after more specific configuration values
154
     * are loaded. Keeping defaults separate from current settings also
155
     * allows us to determine when a setting has been overridden.
156
     *
157
     * @param string $key
158
     * @param string $value
159
     */
160
    public function setDefault($key, $value)
161
    {
162
        $this->defaults[$key] = $value;
163
        return $this;
164
    }
165
166
    /**
167
     * @return bool
168
     */
169
    public function isSimulated()
170
    {
171
        return $this->get(self::SIMULATE);
172
    }
173
174
    /**
175
     * @param bool $simulated
176
     *
177
     * @return $this
178
     */
179
    public function setSimulated($simulated = true)
180
    {
181
        return $this->set(self::SIMULATE, $simulated);
182
    }
183
184
    /**
185
     * @return bool
186
     */
187
    public function isInteractive()
188
    {
189
        return $this->get(self::INTERACTIVE);
190
    }
191
192
    /**
193
     * @param bool $simulated
0 ignored issues
show
Bug introduced by
There is no parameter named $simulated. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
194
     *
195
     * @return $this
196
     */
197
    public function setInteractive($interactive = true)
198
    {
199
        return $this->set(self::INTERACTIVE, $interactive);
200
    }
201
202
    /**
203
     * @return bool
204
     */
205
    public function isDecorated()
206
    {
207
        return $this->get(self::DECORATED);
208
    }
209
210
    /**
211
     * @param bool $decorated
212
     *
213
     * @return $this
214
     */
215
    public function setDecorated($decorated = true)
216
    {
217
        return $this->set(self::DECORATED, $decorated);
218
    }
219
220
    /**
221
     * @param int $interval
222
     *
223
     * @return $this
224
     */
225
    public function setProgressBarAutoDisplayInterval($interval)
226
    {
227
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
228
    }
229
}
230