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

Config::extend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Robo;
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
    public function import(array $data)
70
    {
71
        $this->config->import($data, true);
72
        return $this;
73
    }
74
75
    /**
76
     * Extend this configuration by merging the provided nested array.
77
     */
78
    public function extend(array $data)
79
    {
80
        $this->config->import($data, false);
81
        return $this;
82
    }
83
84
    /**
85
     * Export all configuration as a nested array.
86
     */
87
    public function export()
88
    {
89
        return $this->config->export();
90
    }
91
92
93
    /**
94
     * Return an associative array containing all of the global configuration
95
     * options and their default values.
96
     *
97
     * @return array
98
     */
99
    public function getGlobalOptionDefaultValues()
100
    {
101
        $globalOptions =
102
        [
103
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
104
            self::SIMULATE => false,
105
        ];
106
107
        return $globalOptions;
108
    }
109
110
    /**
111
     * Return the default value for a given configuration item.
112
     *
113
     * @param string $key
114
     * @param mixed $defaultOverride
115
     *
116
     * @return mixed
117
     */
118
    public function getDefault($key, $defaultOverride = null)
119
    {
120
        $globalOptions = $this->getGlobalOptionDefaultValues();
121
        return isset($globalOptions[$key]) ? $globalOptions[$key] : $defaultOverride;
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function isSimulated()
128
    {
129
        return $this->get(self::SIMULATE);
130
    }
131
132
    /**
133
     * @param bool $simulated
134
     *
135
     * @return $this
136
     */
137
    public function setSimulated($simulated = true)
138
    {
139
        return $this->set(self::SIMULATE, $simulated);
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function isDecorated()
146
    {
147
        return $this->get(self::DECORATED);
148
    }
149
150
    /**
151
     * @param bool $decorated
152
     *
153
     * @return $this
154
     */
155
    public function setDecorated($decorated = true)
156
    {
157
        return $this->set(self::DECORATED, $decorated);
158
    }
159
160
    /**
161
     * @param int $interval
162
     *
163
     * @return $this
164
     */
165
    public function setProgressBarAutoDisplayInterval($interval)
166
    {
167
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
168
    }
169
}
170