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