Passed
Push — master ( eaf173...40d83e )
by Enjoys
02:10
created

Config::addConfig()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 14
nop 3
dl 0
loc 28
ccs 17
cts 17
cp 1
crap 7
rs 8.8333
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
     * @var mixed
50
     */
51
    private $config = [];
52
53 6
    public function __construct(?\Psr\Log\LoggerInterface $logger = null)
54
    {
55 6
        $this->logger = $logger ?? new \Psr\Log\NullLogger();
56 6
    }
57
58
    /**
59
     *
60
     * @param array|string $params
61
     * @param array $options
62
     * @param string $parseClass
63
     * @return void
64
     * @throws Exception
65
     */
66 6
    public function addConfig($params, array $options = [], string $parseClass = self::INI): void
67
    {
68 6
        $params = (array) $params;
69
70 6
        if (!class_exists($parseClass)) {
71 1
            throw new \Exception(sprintf('Not found parse class: %s', $parseClass));
72
        }
73
        /** @var  ParseInterface $parser */
74 5
        $parser = new $parseClass();
75 5
        $parser->setOptions($options);
76 5
        $parser->setLogger($this->logger);
77
78 5
        foreach ($params as $namespace => $config) {
79 5
            if (is_int($namespace)) {
80 1
                $namespace = null;
81
            }
82
83 5
            if (is_string($namespace)) {
84 4
                $namespace = \trim($namespace);
85
            }
86
87 5
            if (is_array($config)) {
88 2
                foreach ($config as $_config) {
89 2
                    $this->parse($parser, $_config, $namespace);
90
                }
91 2
                continue;
92
            }
93 3
            $this->parse($parser, $config, $namespace);
94
        }
95 5
    }
96
97 5
    private function parse(ParseInterface $parser, string $config, ?string $namespace = null): void
98
    {
99 5
        $parser->addConfigSource($config);
100
101 5
        $result = $parser->parse();
102
103 5
        if (is_array($result)) {
104 5
            if ($namespace === null) {
105 1
                $this->config = array_merge($this->config, $result);
106
            } else {
107 4
                if (!array_key_exists($namespace, $this->config)) {
108 4
                    $this->config[$namespace] = [];
109
                }
110 4
                $this->config[$namespace] = array_merge($this->config[$namespace], $result);
111
            }
112
        }
113 5
    }
114
115
    /**
116
     *
117
     * @param string $key
118
     * @param mixed $default
119
     * @return mixed
120
     */
121 5
    public function getConfig(string $key = null, $default = null)
122
    {
123 5
        if ($key === null) {
124 5
            return $this->config;
125
        }
126
127 1
        if (is_array($this->config) && array_key_exists($key, $this->config)) {
128 1
            return $this->config[$key];
129
        }
130
131
//        if (is_object($this->config) && property_exists($this->config, $key)) {
132
//            return $this->config->$key;
133
//        }
134
135 1
        return $default;
136
    }
137
}
138