Passed
Push — master ( 435ddd...eaf173 )
by Enjoys
01:53
created

Config::getConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 4
rs 10
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
declare(strict_types=1);
28
29
namespace Enjoys\Config;
30
31
use Enjoys\Config\Parse;
32
use Exception;
33
34
/**
35
 * Description of Config
36
37
 * @author Enjoys
38
 */
39
class Config implements \Psr\Log\LoggerAwareInterface
40
{
41
    use \Psr\Log\LoggerAwareTrait;
42
43
    public const YAML = Parse\YAML::class;
44
    public const INI = Parse\INI::class;
45
    public const JSON = Parse\Json::class;
46
47
48
    /**
49
     *
50
     * @var mixed
51
     */
52
    private $config = [];
53
54 4
    public function __construct(?\Psr\Log\LoggerInterface $logger = null)
55
    {
56 4
        $this->logger = $logger ?? new \Psr\Log\NullLogger();
57 4
    }
58
59
    /**
60
     *
61
     * @param array|string $config
62
     * @param array $options
63
     * @param string $parseClass
64
     * @return void
65
     * @throws Exception
66
     */
67 4
    public function addConfig($config, array $options = [], string $parseClass = self::INI): void
68
    {
69 4
        if (!class_exists($parseClass)) {
70 1
            throw new \Exception(sprintf('Not found parse class: %s', $parseClass));
71
        }
72
73 3
        if (is_array($config)) {
74 2
            foreach ($config as $namespace => $_config) {
75 2
                $this->parse($parseClass, $_config, $options, $namespace);
76
            }
77
        }
78
        
79 3
        if(is_string($config)){
80 1
            $this->parse($parseClass, $config, $options);
81
        }
82
83
 
84 3
    }
85
    
86 3
    private function parse(string $parseClass, string $config, $options, ?string $namespace = null)
87
    {
88
       /** @var  ParseInterface $parser */
89 3
        $parser = new $parseClass($config);
90 3
        $parser->setOptions($options);
91 3
        $parser->setLogger($this->logger);
92 3
        $result = $parser->parse();
93
94 3
        if (is_array($result)) {
95 3
            if ($namespace === null) {
96 1
                $this->config = array_merge($this->config, $result);
97
            } else {
98 2
                if (!array_key_exists($namespace, $this->config)) {
99 2
                    $this->config[$namespace] = [];
100
                }
101 2
                $this->config[$namespace] = array_merge($this->config[$namespace], $result);
102
            }
103
        }
104 3
    }
105
106
    /**
107
     *
108
     * @param string $key
109
     * @param mixed $default
110
     * @return mixed
111
     */
112 3
    public function getConfig(string $key = null, $default = null)
113
    {
114 3
        if ($key === null) {
115 3
            return $this->config;
116
        }
117
118 1
        if (is_array($this->config) && array_key_exists($key, $this->config)) {
119 1
            return $this->config[$key];
120
        }
121
122
//        if (is_object($this->config) && property_exists($this->config, $key)) {
123
//            return $this->config->$key;
124
//        }
125
126 1
        return $default;
127
    }
128
}
129