Passed
Push — attributes ( b566ac...704868 )
by Peter
03:02
created

Config::mustGet()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 15
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AbterPhp\Framework\Config;
7
8
use Opulence\Framework\Configuration\Config as OpulenceConfig;
9
10
class Config extends OpulenceConfig
11
{
12
13
    /**
14
     * Gets a setting, but throws an exception if it's empty
15
     *
16
     * @param string $category The category of setting to get
17
     * @param string $setting  The name of the setting to get
18
     * @param mixed  $default  The default value if one does not exist
19
     *
20
     * @return string|int|float|bool The value of the setting
21
     */
22
    public static function mustGet(string $category, string $setting, $default = null)
23
    {
24
        $value = parent::get($category, $setting, $default);
25
26
        if ($value === null) {
27
            throw new \RuntimeException(sprintf("missing config: %s:%s", $category, $setting));
28
        }
29
30
        if (!is_scalar($value)) {
31
            throw new \RuntimeException(
32
                sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value))
33
            );
34
        }
35
36
        return $value;
37
    }
38
39
    /**
40
     * Gets a setting, but throws an exception if it's empty
41
     *
42
     * @param string $category The category of setting to get
43
     * @param string $setting  The name of the setting to get
44
     * @param mixed  $default  The default value if one does not exist
45
     *
46
     * @return string|int|float|bool The value of the setting
47
     */
48
    public static function reset(string $category, string $setting, $default = null)
49
    {
50
        $value = parent::get($category, $setting, $default);
51
52
        if ($value === null) {
53
            throw new \RuntimeException(sprintf("missing config: %s:%s", $category, $setting));
54
        }
55
56
        if (!is_scalar($value)) {
57
            throw new \RuntimeException(
58
                sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value))
59
            );
60
        }
61
62
        return $value;
63
    }
64
}
65