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

Config::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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
    public function __construct()
19
    {
20
        $this->config = new Data();
21
    }
22
23
    /**
24
     * Determine if a non-default config value exists.
25
     */
26
    public function has($key)
27
    {
28
        return ($this->config->has($key));
29
    }
30
31
    /**
32
     * Fetch a configuration value
33
     *
34
     * @param string $key Which config item to look up
35
     * @param string|null $defaultOverride Override usual default value with a different default
36
     *
37
     * @return mixed
38
     */
39
    public function get($key, $defaultOverride = null)
40
    {
41
        if ($this->has($key)) {
42
            return $this->config->get($key);
43
        }
44
        return $this->getDefault($key, $defaultOverride);
45
    }
46
47
    /**
48
     * Set a config value
49
     *
50
     * @param string $key
51
     * @param mixed $value
52
     *
53
     * @return $this
54
     */
55
    public function set($key, $value)
56
    {
57
        $this->config->set($key, $value);
58
        return $this;
59
    }
60
61
    /**
62
     * Return an associative array containing all of the global configuration
63
     * options and their default values.
64
     *
65
     * @return array
66
     */
67
    public function getGlobalOptionDefaultValues()
68
    {
69
        $globalOptions =
70
        [
71
            self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL => self::DEFAULT_PROGRESS_DELAY,
72
            self::SIMULATE => false,
73
        ];
74
75
        return $globalOptions;
76
    }
77
78
    /**
79
     * Return the default value for a given configuration item.
80
     *
81
     * @param string $key
82
     * @param mixed $defaultOverride
83
     *
84
     * @return mixed
85
     */
86
    public function getDefault($key, $defaultOverride = null)
87
    {
88
        $globalOptions = $this->getGlobalOptionDefaultValues();
89
        return isset($globalOptions[$key]) ? $globalOptions[$key] : $defaultOverride;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isSimulated()
96
    {
97
        return $this->get(self::SIMULATE);
98
    }
99
100
    /**
101
     * @param bool $simulated
102
     *
103
     * @return $this
104
     */
105
    public function setSimulated($simulated = true)
106
    {
107
        return $this->set(self::SIMULATE, $simulated);
108
    }
109
110
    /**
111
     * @return bool
112
     */
113
    public function isDecorated()
114
    {
115
        return $this->get(self::DECORATED);
116
    }
117
118
    /**
119
     * @param bool $decorated
120
     *
121
     * @return $this
122
     */
123
    public function setDecorated($decorated = true)
124
    {
125
        return $this->set(self::DECORATED, $decorated);
126
    }
127
128
    /**
129
     * @param int $interval
130
     *
131
     * @return $this
132
     */
133
    public function setProgressBarAutoDisplayInterval($interval)
134
    {
135
        return $this->set(self::PROGRESS_BAR_AUTO_DISPLAY_INTERVAL, $interval);
136
    }
137
}
138