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

Config   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 204
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 7 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
        if (!empty($data)) {
81
            $this->config->import($data, true);
82
        }
83
        return $this;
84
    }
85
86
    /**
87
     * Export all configuration as a nested array.
88
     */
89
    public function export()
90
    {
91
        return $this->config->export();
92
    }
93
94
    /**
95
     * Given an object that contains configuration methods, inject any
96
     * configuration found in the configuration file.
97
     *
98
     * The proper use for this method is to call setter methods of the
99
     * provided object. Using configuration to call methods that do work
100
     * is an abuse of this mechanism.
101
     *
102
     * TODO: We could use reflection to test to see if the return type
103
     * of the provided object is a reference to the object itself. All
104
     * setter methods should do this. This test is insufficient to guarentee
105
     * that the method is valid, but it would be a good start.
106
     */
107
    public function applyConfiguration($object, $configurationKey)
108
    {
109
        if ($this->has($configurationKey)) {
110
            $settings = $this->get($configurationKey);
111
            foreach ($settings as $setterMethod => $args) {
112
                // TODO: Should it be possible to make $args a nested array
113
                // to make this code call the setter method multiple times?
114
                call_user_func_array([$object, $setterMethod], (array)$args);
115
            }
116
        }
117
    }
118
119
    /**
120
     * Return an associative array containing all of the global configuration
121
     * options and their default values.
122
     *
123
     * @return array
124
     */
125
    public function getGlobalOptionDefaultValues()
126
    {
127
        $globalOptions =
128
        [
129
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
130
            self::SIMULATE => false,
131
        ];
132
133
        return $globalOptions;
134
    }
135
136
    /**
137
     * Return the default value for a given configuration item.
138
     *
139
     * @param string $key
140
     * @param mixed $defaultOverride
141
     *
142
     * @return mixed
143
     */
144
    public function getDefault($key, $defaultOverride = null)
145
    {
146
        return isset($this->defaults[$key]) ? $this->defaults[$key] : $defaultOverride;
147
    }
148
149
    /**
150
     * Set the default value for a configuration setting. This allows us to
151
     * set defaults either before or after more specific configuration values
152
     * are loaded. Keeping defaults separate from current settings also
153
     * allows us to determine when a setting has been overridden.
154
     *
155
     * @param string $key
156
     * @param string $value
157
     */
158
    public function setDefault($key, $value)
159
    {
160
        $this->defaults[$key] = $value;
161
        return $this;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function isSimulated()
168
    {
169
        return $this->get(self::SIMULATE);
170
    }
171
172
    /**
173
     * @param bool $simulated
174
     *
175
     * @return $this
176
     */
177
    public function setSimulated($simulated = true)
178
    {
179
        return $this->set(self::SIMULATE, $simulated);
180
    }
181
182
    /**
183
     * @return bool
184
     */
185
    public function isDecorated()
186
    {
187
        return $this->get(self::DECORATED);
188
    }
189
190
    /**
191
     * @param bool $decorated
192
     *
193
     * @return $this
194
     */
195
    public function setDecorated($decorated = true)
196
    {
197
        return $this->set(self::DECORATED, $decorated);
198
    }
199
200
    /**
201
     * @param int $interval
202
     *
203
     * @return $this
204
     */
205
    public function setProgressBarAutoDisplayInterval($interval)
206
    {
207
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
208
    }
209
}
210