Passed
Push — master ( f50aa1...d726cb )
by Nico
57:47 queued 28:23
created

SettingsCore   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 68.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 125
ccs 35
cts 51
cp 0.6863
rs 10
wmc 20

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigValue() 0 14 3
A __construct() 0 5 1
A getBooleanConfigValue() 0 10 2
A exists() 0 6 1
A getIntegerConfigValue() 0 10 2
A getStringConfigValue() 0 10 2
A getPath() 0 6 2
A getConfig() 0 3 1
A getArrayConfigValue() 0 5 1
A getSettingPath() 0 3 1
A getConfigArray() 0 18 4
1
<?php
2
3
namespace Stu\Module\Config\Model;
4
5
use Noodlehaus\ConfigInterface;
6
use Stu\Module\Config\StuConfigException;
7
8
class SettingsCore implements SettingsCoreInterface
9
{
10 17
    public function __construct(
11
        private ?SettingsInterface $parentSetting,
12
        private string $configPath,
13
        private ConfigInterface $config
14
    ) {
15 17
    }
16
17 16
    public function getPath(): string
18
    {
19 16
        if ($this->parentSetting === null) {
20 14
            return $this->configPath;
21
        }
22 2
        return sprintf('%s.%s', $this->parentSetting->getPath(), $this->configPath);
23
    }
24
25 15
    public function getConfig(): ConfigInterface
26
    {
27 15
        return $this->config;
28
    }
29
30 4
    public function getIntegerConfigValue(string $setting, ?int $default = null): int
31
    {
32 4
        $path = $this->getSettingPath($setting);
33 4
        $value = $this->getConfigValue($path, $default);
34
35 3
        if (!is_numeric($value)) {
36 1
            throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid integer.', $value, $path));
37
        }
38
39 2
        return (int)$value;
40
    }
41
42 7
    public function getStringConfigValue(string $setting, ?string $default = null): string
43
    {
44 7
        $path = $this->getSettingPath($setting);
45 7
        $value = $this->getConfigValue($path, $default);
46
47 7
        if (!is_string($value)) {
48 1
            throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid string.', $value, $path));
49
        }
50
51 6
        return $value;
52
    }
53
54 7
    public function getBooleanConfigValue(string $setting, ?bool $default = null): bool
55
    {
56 7
        $path = $this->getSettingPath($setting);
57 7
        $value = $this->getConfigValue($path, $default);
58
59 7
        if (!is_bool($value)) {
60 1
            throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid boolean.', $value, $path));
61
        }
62
63 6
        return $value;
64
    }
65
66
    /**
67
     * @param string[]|null $default
68
     *
69
     * @return array<string>
70
     */
71
    public function getArrayConfigValue(string $setting, ?array $default = null): array
72
    {
73
        $path = $this->getSettingPath($setting);
74
75
        return $this->getConfigArray($path, $default);
76
    }
77
78
    public function exists(string $setting): bool
79
    {
80
        $path = $this->getSettingPath($setting);
81
        $value = $this->getConfig()->get($path);
82
83
        return $value !== null;
84
    }
85
86
    /**
87
     * @return int|string|bool
88
     */
89 14
    private function getConfigValue(string $path, int|null|string|bool $default = null)
90
    {
91 14
        $value = $this->getConfig()->get($path);
92
93 14
        if ($value !== null) {
94 10
            return $value;
95
        }
96
97
        //throw exception if setting mandatory
98 4
        if ($default === null) {
99 1
            throw new StuConfigException(sprintf('There is no corresponding config setting on path "%s"', $path));
100
        }
101
102 3
        return $default;
103
    }
104
105
    /**
106
     * @param string[]|null $default
107
     *
108
     * @return array<string>
109
     */
110
    private function getConfigArray(string $path, ?array $default = null): array
111
    {
112
        $value = $this->getConfig()->get($path);
113
114
        if ($value !== null) {
115
            if (!is_array($value)) {
116
                throw new StuConfigException(sprintf('The value "%s" with path "%s" is no valid array.', $value, $path));
117
            }
118
119
            return $value;
120
        }
121
122
        //throw exception if setting mandatory
123
        if ($default === null) {
124
            throw new StuConfigException(sprintf('There is no corresponding config setting on path "%s"', $path));
125
        }
126
127
        return $default;
128
    }
129
130 14
    private function getSettingPath(string $setting): string
131
    {
132 14
        return sprintf('%s.%s', $this->getPath(), $setting);
133
    }
134
}
135