Passed
Push — master ( a07882...2b8759 )
by Thierry
02:21
created

ConfigManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 107
rs 10
c 1
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 11 2
A read() 0 25 5
A getConfig() 0 3 1
A __construct() 0 4 1
A newConfig() 0 11 2
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\Exception\SetupException;
18
use Jaxon\Utils\Config\Config;
19
use Jaxon\Utils\Config\Exception\DataDepth;
20
use Jaxon\Utils\Config\Exception\FileAccess;
21
use Jaxon\Utils\Config\Exception\FileContent;
22
use Jaxon\Utils\Config\Exception\FileExtension;
23
use Jaxon\Utils\Config\Exception\YamlExtension;
24
use Jaxon\Utils\Config\Reader;
25
use Jaxon\Utils\Translation\Translator;
26
27
class ConfigManager extends Reader
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ConfigManager
Loading history...
28
{
29
    /**
30
     * @var Translator
31
     */
32
    protected $xTranslator;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
33
34
    /**
35
     * The constructor
36
     *
37
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
38
     * @param Translator $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
39
     */
40
    public function __construct(Config $xConfig, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
41
    {
42
        parent::__construct($xConfig);
43
        $this->xTranslator = $xTranslator;
44
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
45
46
    /**
47
     * Read a config file
48
     *
49
     * @param string $sConfigFile
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
50
     *
51
     * @return array
52
     * @throws SetupException
53
     */
54
    public function read(string $sConfigFile): array
55
    {
56
        try
57
        {
58
            return parent::read($sConfigFile);
59
        }
60
        catch(YamlExtension $e)
61
        {
62
            $sMessage = $this->xTranslator->trans('errors.yaml.install');
63
            throw new SetupException($sMessage);
64
        }
65
        catch(FileExtension $e)
66
        {
67
            $sMessage = $this->xTranslator->trans('errors.file.extension', ['path' => $sConfigFile]);
68
            throw new SetupException($sMessage);
69
        }
70
        catch(FileAccess $e)
71
        {
72
            $sMessage = $this->xTranslator->trans('errors.file.access', ['path' => $sConfigFile]);
73
            throw new SetupException($sMessage);
74
        }
75
        catch(FileContent $e)
76
        {
77
            $sMessage = $this->xTranslator->trans('errors.file.content', ['path' => $sConfigFile]);
78
            throw new SetupException($sMessage);
79
        }
80
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
81
82
    /**
83
     * Set the config options of the library
84
     *
85
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
86
     *
87
     * @return void
88
     * @throws SetupException
89
     */
90
    public function setOptions(array $aOptions): void
91
    {
92
        try
93
        {
94
            $this->xConfig->setOptions($aOptions);
95
        }
96
        catch(DataDepth $e)
97
        {
98
            $sMessage = $this->xTranslator->trans('errors.data.depth',
99
                ['key' => $e->sPrefix, 'depth' => $e->nDepth]);
100
            throw new SetupException($sMessage);
101
        }
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Get the library config
106
     *
107
     * @return Config
108
     */
109
    public function getConfig(): Config
110
    {
111
        return $this->xConfig;
112
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
113
114
    /**
115
     * Create a new the config object
116
     *
117
     * @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...
118
     * @param string $sKeys    The prefix of key of the config options
119
     *
120
     * @return Config
121
     * @throws SetupException
122
     */
123
    public function newConfig(array $aOptions = [], string $sKeys = ''): Config
124
    {
125
        try
126
        {
127
            return new Config($aOptions, $sKeys);
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; 0 found
Loading history...
136
}
137