Completed
Push — master ( 070a7f...5725b1 )
by Sébastien
04:16
created

SafeConfigReader   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 26
eloc 71
dl 0
loc 203
ccs 75
cts 85
cp 0.8824
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getNullableString() 0 13 3
A getNullableInt() 0 12 3
A getValueOrDefault() 0 3 2
A ensureNotNull() 0 12 2
A __construct() 0 4 1
A ensureKeyExists() 0 12 2
A getNullableBool() 0 12 3
A getArray() 0 6 1
A keyExists() 0 3 1
A getInt() 0 6 1
A throwInvalidConfigException() 0 8 2
A getNullableArray() 0 12 3
A getBool() 0 6 1
A getString() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Util;
6
7
use Soluble\MediaTools\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 28
    public function __construct(array $config, ?string $configKey = null)
21
    {
22 28
        $this->config    = $config;
23 28
        $this->configKey = $configKey;
24 28
    }
25
26
    /**
27
     * @return int|null
28
     *
29
     * @throws InvalidConfigException
30
     */
31 26
    public function getNullableInt(string $key, ?int $default = null): ?int
32
    {
33 26
        $value = $this->getValueOrDefault($key, $default);
34 26
        if (!($value === null) && !is_int($value)) {
35 1
            $this->throwInvalidConfigException(sprintf(
36 1
                'Param \'%s\' must be int, \'%s\' given',
37 1
                $key,
38 1
                gettype($value)
39 1
            ), $key);
40
        }
41
42 25
        return $value;
43
    }
44
45
    /**
46
     * Check strict int.
47
     *
48
     * @throws InvalidConfigException
49
     */
50 4
    public function getInt(string $key, ?int $default = null): int
51
    {
52 4
        $value = $this->getNullableInt($key, $default);
53 3
        $this->ensureNotNull($value, $key);
54
55 2
        return (int) $value;
56
    }
57
58
    /**
59
     * Check strict array.
60
     *
61
     * @throws InvalidConfigException
62
     */
63 25
    public function getArray(string $key, ?array $default = null): array
64
    {
65 25
        $value = $this->getNullableArray($key, $default);
66 24
        $this->ensureNotNull($value, $key);
67
68 23
        return (array) $value;
69
    }
70
71
    /**
72
     * @return array|null
73
     *
74
     * @throws InvalidConfigException
75
     */
76 26
    public function getNullableArray(string $key, ?array $default = null): ?array
77
    {
78 26
        $value = $this->getValueOrDefault($key, $default);
79 26
        if (!($value === null) && !is_array($value)) {
80 1
            $this->throwInvalidConfigException(sprintf(
81 1
                'Param \'%s\' must be array, \'%s\' given',
82 1
                $key,
83 1
                gettype($value)
84 1
            ), $key);
85
        }
86
87 25
        return $value;
88
    }
89
90
    /**
91
     * Check strict string.
92
     *
93
     * @throws InvalidConfigException
94
     */
95 26
    public function getString(string $key, ?string $default = null): string
96
    {
97 26
        $value = $this->getNullableString($key, $default);
98 24
        $this->ensureNotNull($value, $key);
99
100 23
        return (string) $value;
101
    }
102
103
    /**
104
     * @throws InvalidConfigException
105
     */
106 28
    public function getNullableString(string $key, ?string $default = null): ?string
107
    {
108 28
        $value = $this->getValueOrDefault($key, $default);
109
110 28
        if (!($value === null) && !is_string($value)) {
111 3
            $this->throwInvalidConfigException(sprintf(
112 3
                'Param \'%s\' must be string, \'%s\' given',
113 3
                $key,
114 3
                gettype($value)
115 3
            ), $key);
116
        }
117
118 25
        return $value;
119
    }
120
121
    /**
122
     * Check strict bool.
123
     *
124
     * @throws InvalidConfigException
125
     */
126 4
    public function getBool(string $key, ?bool $default = null): bool
127
    {
128 4
        $value = $this->getNullableBool($key, $default);
129 3
        $this->ensureNotNull($value, $key);
130
131 2
        return (bool) $value;
132
    }
133
134
    /**
135
     * @throws InvalidConfigException
136
     */
137 5
    public function getNullableBool(string $key, ?bool $default = null): ?bool
138
    {
139 5
        $value = $this->getValueOrDefault($key, $default);
140 5
        if (!($value === null) && !is_bool($value)) {
141 1
            $this->throwInvalidConfigException(sprintf(
142 1
                'Param \'%s\' must be bool, \'%s\' given',
143 1
                $key,
144 1
                gettype($value)
145 1
            ), $key);
146
        }
147
148 4
        return $value;
149
    }
150
151
    /**
152
     * @param mixed|null $default
153
     *
154
     * @return mixed|null
155
     */
156 28
    protected function getValueOrDefault(string $key, $default)
157
    {
158 28
        return $this->keyExists($key) ? $this->config[$key] : $default;
159
    }
160
161 28
    public function keyExists(string $key): bool
162
    {
163 28
        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 24
    protected function ensureNotNull($value, string $key): void
190
    {
191 24
        if ($value !== null) {
192 23
            return;
193
        }
194
195 1
        $this->throwInvalidConfigException(
196 1
            sprintf(
197 1
                'Param \'%s\' cannot be null.',
198 1
                $key
199
            ),
200 1
            $key
201
        );
202
    }
203
204 4
    protected function throwInvalidConfigException(string $msg, string $key): void
205
    {
206 4
        throw new InvalidConfigException(
207 4
            sprintf(
208 4
                '%s (check your config entry %s[\'%s\'])',
209 4
                $msg,
210 4
                $this->configKey === null ? '' : '[' . $this->configKey . ']',
211 4
                $key
212
            )
213
        );
214
    }
215
}
216