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

Config::getDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
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
     * Create a new configuration object, and initialize it with
20
     * the provided nested array containing configuration data.
21
     */
22
    public function __construct(array $data = null)
23
    {
24
        $this->config = new Data($data);
25
    }
26
27
    /**
28
     * Determine if a non-default config value exists.
29
     */
30
    public function has($key)
31
    {
32
        return ($this->config->has($key));
33
    }
34
35
    /**
36
     * Fetch a configuration value
37
     *
38
     * @param string $key Which config item to look up
39
     * @param string|null $defaultOverride Override usual default value with a different default
40
     *
41
     * @return mixed
42
     */
43
    public function get($key, $defaultOverride = null)
44
    {
45
        if ($this->has($key)) {
46
            return $this->config->get($key);
47
        }
48
        return $this->getDefault($key, $defaultOverride);
49
    }
50
51
    /**
52
     * Set a config value
53
     *
54
     * @param string $key
55
     * @param mixed $value
56
     *
57
     * @return $this
58
     */
59
    public function set($key, $value)
60
    {
61
        $this->config->set($key, $value);
62
        return $this;
63
    }
64
65
    /**
66
     * Import configuration from the provided nexted array, replacing whatever
67
     * was here previously.
68
     *
69
     * @param array|ConfigLoaderInterface $data
70
     */
71
    public function import($data, $overwrite = true)
72
    {
73
        if ($data instanceof ConfigLoaderInterface) {
74
            $data = $data->export();
75
        }
76
        $this->config->import($data, $overwrite);
77
        return $this;
78
    }
79
80
    /**
81
     * Extend this configuration by merging the provided nested array.
82
     *
83
     * @param array|ConfigLoaderInterface $data
84
     */
85
    public function extend($data)
86
    {
87
        $this->import($data, false);
88
        return $this;
89
    }
90
91
    /**
92
     * Export all configuration as a nested array.
93
     */
94
    public function export()
95
    {
96
        return $this->config->export();
97
    }
98
99
100
    /**
101
     * Return an associative array containing all of the global configuration
102
     * options and their default values.
103
     *
104
     * @return array
105
     */
106
    public function getGlobalOptionDefaultValues()
107
    {
108
        $globalOptions =
109
        [
110
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
111
            self::SIMULATE => false,
112
        ];
113
114
        return $globalOptions;
115
    }
116
117
    /**
118
     * Return the default value for a given configuration item.
119
     *
120
     * @param string $key
121
     * @param mixed $defaultOverride
122
     *
123
     * @return mixed
124
     */
125
    public function getDefault($key, $defaultOverride = null)
126
    {
127
        $globalOptions = $this->getGlobalOptionDefaultValues();
128
        return isset($globalOptions[$key]) ? $globalOptions[$key] : $defaultOverride;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function isSimulated()
135
    {
136
        return $this->get(self::SIMULATE);
137
    }
138
139
    /**
140
     * @param bool $simulated
141
     *
142
     * @return $this
143
     */
144
    public function setSimulated($simulated = true)
145
    {
146
        return $this->set(self::SIMULATE, $simulated);
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function isDecorated()
153
    {
154
        return $this->get(self::DECORATED);
155
    }
156
157
    /**
158
     * @param bool $decorated
159
     *
160
     * @return $this
161
     */
162
    public function setDecorated($decorated = true)
163
    {
164
        return $this->set(self::DECORATED, $decorated);
165
    }
166
167
    /**
168
     * @param int $interval
169
     *
170
     * @return $this
171
     */
172
    public function setProgressBarAutoDisplayInterval($interval)
173
    {
174
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
175
    }
176
}
177