Passed
Push — master ( cd6749...435ddd )
by Enjoys
01:48
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 3
    public function __construct(?\Psr\Log\LoggerInterface $logger = null)
55
    {
56 3
        $this->logger = $logger ?? new \Psr\Log\NullLogger();
57 3
    }
58
59
    /**
60
     *
61
     * @param array|string $config
62
     * @param array $options
63
     * @param string $parseClass
64
     * @return void
65
     * @throws Exception
66
     */
67 3
    public function addConfig($config, array $options = [], string $parseClass = self::INI): void
68
    {
69 3
        if (!class_exists($parseClass)) {
70 1
            throw new \Exception(sprintf('Not found parse class: %s', $parseClass));
71
        }
72
73 2
        if (is_array($config) && !empty($config)) {
74 1
            $namespace = array_key_first($config);
75 1
            $config = $config[$namespace];
76
        }
77
78
        /** @var  ParseInterface $parser */
79 2
        $parser = new $parseClass($config);
80 2
        $parser->setOptions($options);
81 2
        $parser->setLogger($this->logger);
82 2
        $result = $parser->parse();
83
84 2
        if (is_array($result)) {
85 2
            if (!isset($namespace)) {
86 1
                $this->config = array_merge($this->config, $result);
87
            } else {
88 1
                if (!array_key_exists($namespace, $this->config)) {
89 1
                    $this->config[$namespace] = [];
90
                }
91 1
                $this->config[$namespace] = array_merge($this->config[$namespace], $result);
92
            }
93
        }
94 2
    }
95
96
    /**
97
     *
98
     * @param string $key
99
     * @param mixed $default
100
     * @return mixed
101
     */
102 2
    public function getConfig(string $key = null, $default = null)
103
    {
104 2
        if ($key === null) {
105 2
            return $this->config;
106
        }
107
108 1
        if (is_array($this->config) && array_key_exists($key, $this->config)) {
109 1
            return $this->config[$key];
110
        }
111
112
//        if (is_object($this->config) && property_exists($this->config, $key)) {
113
//            return $this->config->$key;
114
//        }
115
116 1
        return $default;
117
    }
118
}
119