Passed
Push — master ( 27d45a...b61896 )
by Thierry
02:21
created

ConfigManager::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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\Jaxon;
18
use Jaxon\Exception\SetupException;
19
use Jaxon\Utils\Config\Config;
20
use Jaxon\Utils\Config\ConfigReader;
21
use Jaxon\Utils\Translation\Translator;
22
use Jaxon\Utils\Config\Exception\DataDepth;
23
use Jaxon\Utils\Config\Exception\FileAccess;
24
use Jaxon\Utils\Config\Exception\FileContent;
25
use Jaxon\Utils\Config\Exception\FileExtension;
26
use Jaxon\Utils\Config\Exception\YamlExtension;
27
28
class ConfigManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ConfigManager
Loading history...
29
{
30
    /**
31
     * @var Config
32
     */
33
    protected $xConfig;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * @var ConfigReader
37
     */
38
    protected $xConfigReader;
39
40
    /**
41
     * @var Translator
42
     */
43
    protected $xTranslator;
44
45
    /**
46
     * @var array The default config options
47
     */
48
    protected $aConfig =  [
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "="; 2 found
Loading history...
49
        'core' => [
50
            'version'               => Jaxon::VERSION,
51
            'language'              => 'en',
52
            'encoding'              => 'utf-8',
53
            'decode_utf8'           => false,
54
            'prefix' => [
55
                'function'          => 'jaxon_',
56
                'class'             => 'Jaxon',
57
            ],
58
            'request' => [
59
                // 'uri'            => '',
60
                'mode'              => 'asynchronous',
61
                'method'            => 'POST', // W3C: Method is case sensitive
62
            ],
63
            'response' => [
64
                'send'              => true,
65
                'merge.ap'          => true,
66
                'merge.js'          => true,
67
            ],
68
            'debug' => [
69
                'on'                => false,
70
                'verbose'           => false,
71
            ],
72
            'process' => [
73
                'exit'              => true,
74
                'clean'             => false,
75
                'timeout'           => 6000,
76
            ],
77
            'error' => [
78
                'handle'            => false,
79
                'log_file'          => '',
80
            ],
81
            'jquery' => [
82
                'no_conflict'       => false,
83
            ],
84
            'upload' => [
85
                'enabled'           => true,
86
            ],
87
        ],
88
        'js' => [
89
            'lib' => [
90
                'output_id'         => 0,
91
                'queue_size'        => 0,
92
                'load_timeout'      => 2000,
93
                'show_status'       => false,
94
                'show_cursor'       => true,
95
            ],
96
            'app' => [
97
                'dir'               => '',
98
                'minify'            => true,
99
                'options'           => '',
100
            ],
101
        ],
102
    ];
103
104
    /**
105
     * The constructor
106
     *
107
     * @param ConfigReader $xConfigReader
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
109
     */
110
    public function __construct(ConfigReader $xConfigReader, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
111
    {
112
        $this->xConfigReader = $xConfigReader;
113
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
114
        try
115
        {
116
            $this->xConfig = new Config($this->aConfig);
117
        }
118
        catch(DataDepth $e){} // This exception cannot actually be raised.
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Read a config file
123
     *
124
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     *
126
     * @return array
127
     * @throws SetupException
128
     */
129
    public function read(string $sConfigFile): array
130
    {
131
        try
132
        {
133
            return $this->xConfigReader->read($sConfigFile);
134
        }
135
        catch(YamlExtension $e)
136
        {
137
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
138
            throw new SetupException($sMessage);
139
        }
140
        catch(FileExtension $e)
141
        {
142
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
143
            throw new SetupException($sMessage);
144
        }
145
        catch(FileAccess $e)
146
        {
147
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
148
            throw new SetupException($sMessage);
149
        }
150
        catch(FileContent $e)
151
        {
152
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
153
            throw new SetupException($sMessage);
154
        }
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Read options from a config file and set the library config
159
     *
160
     * @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...
161
     * @param string $sConfigSection The section of the config file to be loaded
162
     *
163
     * @return void
164
     * @throws SetupException
165
     */
166
    public function load(string $sConfigFile, string $sConfigSection = '')
167
    {
168
        try
169
        {
170
            $this->xConfigReader->load($this->xConfig, $sConfigFile, $sConfigSection);
171
            // Set the library language any time the config is changed.
172
            $this->xTranslator->setLocale($this->xConfig->getOption('core.language'));
0 ignored issues
show
Bug introduced by
It seems like $this->xConfig->getOption('core.language') can also be of type null; however, parameter $sLocale of Jaxon\Utils\Translation\Translator::setLocale() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
            $this->xTranslator->setLocale(/** @scrutinizer ignore-type */ $this->xConfig->getOption('core.language'));
Loading history...
173
        }
174
        catch(YamlExtension $e)
175
        {
176
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
177
            throw new SetupException($sMessage);
178
        }
179
        catch(FileExtension $e)
180
        {
181
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
182
            throw new SetupException($sMessage);
183
        }
184
        catch(FileAccess $e)
185
        {
186
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
187
            throw new SetupException($sMessage);
188
        }
189
        catch(FileContent $e)
190
        {
191
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
192
            throw new SetupException($sMessage);
193
        }
194
        catch(DataDepth $e)
195
        {
196
            $sMessage = $this->xTranslator->trans('errors.data.depth',
197
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
198
            throw new SetupException($sMessage);
199
        }
200
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
201
202
    /**
203
     * Set the config options of the library
204
     *
205
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
206
     *
207
     * @return void
208
     * @throws SetupException
209
     */
210
    public function setOptions(array $aOptions): void
211
    {
212
        try
213
        {
214
            $this->xConfig->setOptions($aOptions);
215
            // Set the library language any time the config is changed.
216
            $this->xTranslator->setLocale($this->xConfig->getOption('core.language'));
0 ignored issues
show
Bug introduced by
It seems like $this->xConfig->getOption('core.language') can also be of type null; however, parameter $sLocale of Jaxon\Utils\Translation\Translator::setLocale() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
            $this->xTranslator->setLocale(/** @scrutinizer ignore-type */ $this->xConfig->getOption('core.language'));
Loading history...
217
        }
218
        catch(DataDepth $e)
219
        {
220
            $sMessage = $this->xTranslator->trans('errors.data.depth',
221
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
222
            throw new SetupException($sMessage);
223
        }
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Set the value of a config option
228
     *
229
     * @param string $sName The option name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
230
     * @param mixed $xValue The option value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
231
     *
232
     * @return void
233
     */
234
    public function setOption(string $sName, $xValue)
235
    {
236
        $this->xConfig->setOption($sName, $xValue);
237
        // Set the library language any time the config is changed.
238
        $this->xTranslator->setLocale($this->xConfig->getOption('core.language'));
0 ignored issues
show
Bug introduced by
It seems like $this->xConfig->getOption('core.language') can also be of type null; however, parameter $sLocale of Jaxon\Utils\Translation\Translator::setLocale() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

238
        $this->xTranslator->setLocale(/** @scrutinizer ignore-type */ $this->xConfig->getOption('core.language'));
Loading history...
239
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
240
241
    /**
242
     * Get the value of a config option
243
     *
244
     * @param string $sName The option name
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
245
     * @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...
246
     *
247
     * @return mixed
248
     */
249
    public function getOption(string $sName, $xDefault = null)
250
    {
251
        return $this->xConfig->getOption($sName, $xDefault);
252
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
253
254
    /**
255
     * Check the presence of a config option
256
     *
257
     * @param string $sName The option name
258
     *
259
     * @return bool
260
     */
261
    public function hasOption(string $sName): bool
262
    {
263
        return $this->xConfig->hasOption($sName);
264
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
265
266
    /**
267
     * Get the names of the options matching a given prefix
268
     *
269
     * @param string $sPrefix The prefix to match
270
     *
271
     * @return array
272
     */
273
    public function getOptionNames(string $sPrefix): array
274
    {
275
        return $this->xConfig->getOptionNames($sPrefix);
276
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
277
278
    /**
279
     * Create a new the config object
280
     *
281
     * @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...
282
     * @param string $sKeys    The prefix of key of the config options
283
     *
284
     * @return Config
285
     * @throws SetupException
286
     */
287
    public function newConfig(array $aOptions = [], string $sKeys = ''): Config
288
    {
289
        try
290
        {
291
            return new Config($aOptions, $sKeys);
292
        }
293
        catch(DataDepth $e)
294
        {
295
            $sMessage = $this->xTranslator->trans('errors.data.depth',
296
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
297
            throw new SetupException($sMessage);
298
        }
299
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
300
}
301