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

Config::set()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 3
nc 1
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. Deprecated; provide defaults to the config processor instead.
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
     * @return Config
71
     */
72
    public function import($data)
73
    {
74
        if (!empty($data)) {
75
            $this->config->import($data, true);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 72 can also be of type object<Robo\Config\ConfigLoaderInterface>; however, Dflydev\DotAccessData\Data::import() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
        }
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
        $processor = new ConfigProcessor();
88
        $processor->add($this->config->export());
89
        $processor->add($data);
90
        return $this->import($processor->export());
91
    }
92
93
    /**
94
     * Export all configuration as a nested array.
95
     */
96
    public function export()
97
    {
98
        return $this->config->export();
99
    }
100
101
    /**
102
     * Return an associative array containing all of the global configuration
103
     * options and their default values.
104
     *
105
     * @return array
106
     */
107
    public function getGlobalOptionDefaultValues()
108
    {
109
        $globalOptions =
110
        [
111
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
112
            self::SIMULATE => false,
113
        ];
114
115
        return $globalOptions;
116
    }
117
118
    /**
119
     * Return the default value for a given configuration item.
120
     *
121
     * @param string $key
122
     * @param mixed $defaultOverride
123
     *
124
     * @return mixed
125
     */
126
    public function getDefault($key, $defaultOverride = null)
127
    {
128
        $globalOptions = $this->getGlobalOptionDefaultValues();
129
        return isset($globalOptions[$key]) ? $globalOptions[$key] : $defaultOverride;
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function isSimulated()
136
    {
137
        return $this->get(self::SIMULATE);
138
    }
139
140
    /**
141
     * @param bool $simulated
142
     *
143
     * @return $this
144
     */
145
    public function setSimulated($simulated = true)
146
    {
147
        return $this->set(self::SIMULATE, $simulated);
148
    }
149
150
    /**
151
     * @return bool
152
     */
153
    public function isDecorated()
154
    {
155
        return $this->get(self::DECORATED);
156
    }
157
158
    /**
159
     * @param bool $decorated
160
     *
161
     * @return $this
162
     */
163
    public function setDecorated($decorated = true)
164
    {
165
        return $this->set(self::DECORATED, $decorated);
166
    }
167
168
    /**
169
     * @param int $interval
170
     *
171
     * @return $this
172
     */
173
    public function setProgressBarAutoDisplayInterval($interval)
174
    {
175
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
176
    }
177
}
178