Passed
Push — master ( bf86e6...5ea322 )
by Sébastien
07:21
created

SafeConfigReader::getBool()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Common\Config;
6
7
use Soluble\MediaTools\Common\Exception\InvalidConfigException;
8
9
class SafeConfigReader
10
{
11
    /** @var null|string */
12
    protected $configKey;
13
14
    /** @var array<string, mixed> */
15
    protected $config;
16
17
    /**
18
     * @param array<string, mixed> $config
19
     */
20
    public function __construct(array $config, ?string $configKey = null)
21
    {
22
        $this->config    = $config;
23
        $this->configKey = $configKey;
24
    }
25
26
    /**
27
     * @return int|null
28
     *
29
     * @throws InvalidConfigException
30
     */
31
    public function getNullableInt(string $key, ?int $default = null): ?int
32
    {
33
        $value = $this->getValueOrDefault($key, $default);
34
        if (!($value === null) && !is_int($value)) {
35
            $this->throwInvalidConfigException(sprintf(
36
                'Param \'%s\' must be int, \'%s\' given',
37
                $key,
38
                gettype($value)
39
            ), $key);
40
        }
41
42
        return $value;
43
    }
44
45
    /**
46
     * Check strict int.
47
     *
48
     * @throws InvalidConfigException
49
     */
50
    public function getInt(string $key, ?int $default = null): int
51
    {
52
        $value = $this->getNullableInt($key, $default);
53
        $this->ensureNotNull($value, $key);
54
55
        return (int) $value;
56
    }
57
58
    /**
59
     * Check strict array.
60
     *
61
     * @throws InvalidConfigException
62
     */
63
    public function getArray(string $key, ?array $default = null): array
64
    {
65
        $value = $this->getNullableArray($key, $default);
66
        $this->ensureNotNull($value, $key);
67
68
        return (array) $value;
69
    }
70
71
    /**
72
     * @return array|null
73
     *
74
     * @throws InvalidConfigException
75
     */
76
    public function getNullableArray(string $key, ?array $default = null): ?array
77
    {
78
        $value = $this->getValueOrDefault($key, $default);
79
        if (!($value === null) && !is_array($value)) {
80
            $this->throwInvalidConfigException(sprintf(
81
                'Param \'%s\' must be array, \'%s\' given',
82
                $key,
83
                gettype($value)
84
            ), $key);
85
        }
86
87
        return $value;
88
    }
89
90
    /**
91
     * Check strict string.
92
     *
93
     * @throws InvalidConfigException
94
     */
95
    public function getString(string $key, ?string $default = null): string
96
    {
97
        $value = $this->getNullableString($key, $default);
98
        $this->ensureNotNull($value, $key);
99
100
        return (string) $value;
101
    }
102
103
    /**
104
     * @throws InvalidConfigException
105
     */
106
    public function getNullableString(string $key, ?string $default = null): ?string
107
    {
108
        $value = $this->getValueOrDefault($key, $default);
109
110
        if (!($value === null) && !is_string($value)) {
111
            $this->throwInvalidConfigException(sprintf(
112
                'Param \'%s\' must be string, \'%s\' given',
113
                $key,
114
                gettype($value)
115
            ), $key);
116
        }
117
118
        return $value;
119
    }
120
121
    /**
122
     * Check strict bool.
123
     *
124
     * @throws InvalidConfigException
125
     */
126
    public function getBool(string $key, ?bool $default = null): bool
127
    {
128
        $value = $this->getNullableBool($key, $default);
129
        $this->ensureNotNull($value, $key);
130
131
        return (bool) $value;
132
    }
133
134
    /**
135
     * @throws InvalidConfigException
136
     */
137
    public function getNullableBool(string $key, ?bool $default = null): ?bool
138
    {
139
        $value = $this->getValueOrDefault($key, $default);
140
        if (!($value === null) && !is_bool($value)) {
141
            $this->throwInvalidConfigException(sprintf(
142
                'Param \'%s\' must be bool, \'%s\' given',
143
                $key,
144
                gettype($value)
145
            ), $key);
146
        }
147
148
        return $value;
149
    }
150
151
    /**
152
     * @param mixed|null $default
153
     *
154
     * @return mixed|null
155
     */
156
    protected function getValueOrDefault(string $key, $default)
157
    {
158
        return $this->keyExists($key) ? $this->config[$key] : $default;
159
    }
160
161
    public function keyExists(string $key): bool
162
    {
163
        return array_key_exists($key, $this->config);
164
    }
165
166
    /**
167
     * @throws InvalidConfigException
168
     */
169
    public function ensureKeyExists(string $key): void
170
    {
171
        if ($this->keyExists($key)) {
172
            return;
173
        }
174
175
        $this->throwInvalidConfigException(
176
            sprintf(
177
                'Required param [\'%s\'] is missing.',
178
                $key
179
            ),
180
            $key
181
        );
182
    }
183
184
    /**
185
     * @param mixed $value
186
     *
187
     * @throws InvalidConfigException
188
     */
189
    protected function ensureNotNull($value, string $key): void
190
    {
191
        if ($value !== null) {
192
            return;
193
        }
194
195
        $this->throwInvalidConfigException(
196
            sprintf(
197
                'Param \'%s\' cannot be null.',
198
                $key
199
            ),
200
            $key
201
        );
202
    }
203
204
    protected function throwInvalidConfigException(string $msg, string $key): void
205
    {
206
        throw new InvalidConfigException(
207
            sprintf(
208
                '%s (check your config entry %s[\'%s\'])',
209
                $msg,
210
                $this->configKey === null ? '' : '[' . $this->configKey . ']',
211
                $key
212
            )
213
        );
214
    }
215
}
216