Completed
Pull Request — master (#552)
by Greg
03:27
created

Config   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 205
rs 10
c 0
b 0
f 0

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