Passed
Push — master ( 5b7bca...888408 )
by Thierry
02:13
created

ConfigManager::load()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 6
nop 2
dl 0
loc 31
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ConfigManager.php - Jaxon config reader
5
 *
6
 * Extends the config reader in the jaxon-utils package, and provides exception handlers.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
14
15
namespace Jaxon\Config;
16
17
use Jaxon\Utils\Config\Config;
18
use Jaxon\Utils\Config\Reader;
19
use Jaxon\Utils\Translation\Translator;
20
use Jaxon\Exception\SetupException;
21
use Jaxon\Utils\Config\Exception\DataDepth;
22
use Jaxon\Utils\Config\Exception\FileAccess;
23
use Jaxon\Utils\Config\Exception\FileContent;
24
use Jaxon\Utils\Config\Exception\FileExtension;
25
use Jaxon\Utils\Config\Exception\YamlExtension;
26
27
class ConfigManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ConfigManager
Loading history...
28
{
29
    /**
30
     * @var Config
31
     */
32
    protected $xConfig;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
33
34
    /**
35
     * @var Reader
36
     */
37
    protected $xReader;
38
39
    /**
40
     * @var Translator
41
     */
42
    protected $xTranslator;
43
44
    /**
45
     * The constructor
46
     *
47
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
48
     * @param Reader $xReader
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
49
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     */
51
    public function __construct(Config $xConfig, Reader $xReader, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
52
    {
53
        $this->xConfig = $xConfig;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
        $this->xReader = $xReader;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
        $this->xTranslator = $xTranslator;
56
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
57
58
    /**
59
     * Read a config file
60
     *
61
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     *
63
     * @return array
64
     * @throws SetupException
65
     */
66
    public function read(string $sConfigFile): array
67
    {
68
        try
69
        {
70
            return $this->xReader->read($sConfigFile);
71
        }
72
        catch(YamlExtension $e)
73
        {
74
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
75
            throw new SetupException($sMessage);
76
        }
77
        catch(FileExtension $e)
78
        {
79
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
80
            throw new SetupException($sMessage);
81
        }
82
        catch(FileAccess $e)
83
        {
84
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
85
            throw new SetupException($sMessage);
86
        }
87
        catch(FileContent $e)
88
        {
89
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
90
            throw new SetupException($sMessage);
91
        }
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Read options from a config file and set the library config
96
     *
97
     * @param string $sConfigFile The full path to the config file
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
98
     * @param string $sConfigSection The section of the config file to be loaded
99
     *
100
     * @return void
101
     * @throws SetupException
102
     */
103
    public function load(string $sConfigFile, string $sConfigSection = '')
104
    {
105
        try
106
        {
107
            $this->xReader->load($this->xConfig, $sConfigFile, $sConfigSection);
108
        }
109
        catch(YamlExtension $e)
110
        {
111
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
112
            throw new SetupException($sMessage);
113
        }
114
        catch(FileExtension $e)
115
        {
116
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
117
            throw new SetupException($sMessage);
118
        }
119
        catch(FileAccess $e)
120
        {
121
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
122
            throw new SetupException($sMessage);
123
        }
124
        catch(FileContent $e)
125
        {
126
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
127
            throw new SetupException($sMessage);
128
        }
129
        catch(DataDepth $e)
130
        {
131
            $sMessage = $this->xTranslator->trans('errors.data.depth',
132
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
133
            throw new SetupException($sMessage);
134
        }
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
138
     * Set the config options of the library
139
     *
140
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
141
     *
142
     * @return void
143
     * @throws SetupException
144
     */
145
    public function setOptions(array $aOptions): void
146
    {
147
        try
148
        {
149
            $this->xConfig->setOptions($aOptions);
150
        }
151
        catch(DataDepth $e)
152
        {
153
            $sMessage = $this->xTranslator->trans('errors.data.depth',
154
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
155
            throw new SetupException($sMessage);
156
        }
157
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
158
159
    /**
160
     * Set the value of a config option
161
     *
162
     * @param string $sName The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
163
     * @param mixed $xValue The option value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
164
     *
165
     * @return void
166
     */
167
    public function setOption(string $sName, $xValue)
168
    {
169
        $this->xConfig->setOption($sName, $xValue);
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
171
172
    /**
173
     * Get the value of a config option
174
     *
175
     * @param string $sName The option name
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
176
     * @param mixed $xDefault The default value, to be returned if the option is not defined
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
177
     *
178
     * @return mixed
179
     */
180
    public function getOption(string $sName, $xDefault = null)
181
    {
182
        return $this->xConfig->getOption($sName, $xDefault);
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
184
185
    /**
186
     * Check the presence of a config option
187
     *
188
     * @param string $sName The option name
189
     *
190
     * @return bool
191
     */
192
    public function hasOption(string $sName): bool
193
    {
194
        return $this->xConfig->hasOption($sName);
195
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
196
197
    /**
198
     * Get the library config
199
     *
200
     * @return Config
201
     */
202
    public function getConfig(): Config
203
    {
204
        return $this->xConfig;
205
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
206
207
    /**
208
     * Create a new the config object
209
     *
210
     * @param array $aOptions    The options array
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
211
     * @param string $sKeys    The prefix of key of the config options
212
     *
213
     * @return Config
214
     * @throws SetupException
215
     */
216
    public function newConfig(array $aOptions = [], string $sKeys = ''): Config
217
    {
218
        try
219
        {
220
            return new Config($aOptions, $sKeys);
221
        }
222
        catch(DataDepth $e)
223
        {
224
            $sMessage = $this->xTranslator->trans('errors.data.depth',
225
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
226
            throw new SetupException($sMessage);
227
        }
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
229
}
230