Passed
Branch master (ea3a4f)
by Enjoys
02:00
created

Config   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 44
c 2
b 0
f 0
dl 0
loc 109
ccs 43
cts 43
cp 1
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 3 1
A getConfig() 0 11 3
A __construct() 0 3 1
B addConfig() 0 28 7
A parse() 0 25 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Config;
6
7
use Psr\Log\LoggerAwareInterface;
8
use Psr\Log\LoggerAwareTrait;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
12
13
/**
14
 * Description of Config
15
 * @author Enjoys
16
 */
17
final class Config
18
{
19
20
    public const YAML = Parse\YAML::class;
21
    public const INI = Parse\INI::class;
22
    public const JSON = Parse\Json::class;
23
24
25
    private array $config = [];
26
27
    private LoggerInterface $logger;
28
29 7
    public function __construct(?LoggerInterface $logger = null)
30
    {
31 7
        $this->logger = $logger ?? new NullLogger();
32 7
    }
33
34
    /**
35
     *
36
     * @param array|string $params
37
     * @param array $options
38
     * @param string $parseClass
39
     * @param bool $replace
40
     * @return void
41
     * @throws \Exception
42
     */
43 7
    public function addConfig($params, array $options = [], string $parseClass = self::INI, bool $replace = true): void
44
    {
45 7
        $params = (array)$params;
46
47 7
        if (!class_exists($parseClass)) {
48 1
            throw new \Exception(sprintf('Not found parse class: %s', $parseClass));
49
        }
50
        /** @var  ParseInterface $parser */
51 6
        $parser = new $parseClass();
52 6
        $parser->setOptions($options);
0 ignored issues
show
Bug introduced by
The method setOptions() does not exist on Enjoys\Config\ParseInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Enjoys\Config\ParseInterface. ( Ignorable by Annotation )

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

52
        $parser->/** @scrutinizer ignore-call */ 
53
                 setOptions($options);
Loading history...
53 6
        $parser->setLogger($this->logger);
54
55 6
        foreach ($params as $namespace => $config) {
56 6
            if (is_int($namespace)) {
57 1
                $namespace = null;
58
            }
59
60 6
            if (is_string($namespace)) {
61 5
                $namespace = \trim($namespace);
62
            }
63
64 6
            if (is_array($config)) {
65 2
                foreach ($config as $_config) {
66 2
                    $this->parse($parser, $_config, $namespace, $replace);
67
                }
68 2
                continue;
69
            }
70 4
            $this->parse($parser, $config, $namespace, $replace);
71
        }
72 6
    }
73
74 6
    private function parse(
75
        ParseInterface $parser,
76
        string $config,
77
        ?string $namespace = null,
78
        bool $replace = true
79
    ): void {
80 6
        $parser->addConfigSource($config);
81
82 6
        $result = $parser->parse();
83
84 6
        if (is_array($result)) {
85 6
            if ($namespace === null) {
86 1
                if ($replace === true) {
87 1
                    $this->config = array_merge($this->config, $result);
88
                } else {
89 1
                    $this->config = array_merge($result, $this->config);
90
                }
91
            } else {
92 5
                if (!array_key_exists($namespace, $this->config)) {
93 5
                    $this->config[$namespace] = [];
94
                }
95 5
                if ($replace === true) {
96 5
                    $this->config[$namespace] = array_merge((array)$this->config[$namespace], $result);
97
                } else {
98 1
                    $this->config[$namespace] = array_merge($result, (array)$this->config[$namespace]);
99
                }
100
            }
101
        }
102 6
    }
103
104
    /**
105
     *
106
     * @param string $key
107
     * @param mixed $default
108
     * @return mixed
109
     */
110 6
    public function getConfig(string $key = null, $default = null)
111
    {
112 6
        if ($key === null) {
113 6
            return $this->config;
114
        }
115
116 1
        if (array_key_exists($key, $this->config)) {
117 1
            return $this->config[$key];
118
        }
119
120 1
        return $default;
121
    }
122
123 1
    public function setLogger(LoggerInterface $logger): void
124
    {
125 1
        $this->logger = $logger;
126 1
    }
127
}
128