XerviceConfig   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 125
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getInstance() 0 6 2
A get() 0 3 1
A parseAdditionalFiles() 0 7 3
A set() 0 3 1
A parseFileIfExist() 0 3 1
A getRootPath() 0 3 2
A getConfig() 0 3 1
A getConfigPath() 0 3 2
A init() 0 16 1
1
<?php
2
declare(strict_types=1);
3
4
5
namespace Xervice\Config\Business;
6
7
8
use Xervice\Config\Business\Container\ConfigContainer;
9
10
class XerviceConfig
11
{
12
    public const APPLICATION_PATH = 'APPLICATION_PATH';
13
14
    public const ADDITIONAL_CONFIG_FILES = 'ADDITIONAL_CONFIG_FILES';
15
16
    /**
17
     * @var \Xervice\Config\Business\XerviceConfig
18
     */
19
    private static $instance;
20
21
    /**
22
     * @var \Xervice\Config\Business\ConfigBusinessFactory
23
     */
24
    private $factory;
25
26
    /**
27
     * @var \Xervice\Config\Business\Parser\Parser
28
     */
29
    private $parser;
30
31
    /**
32
     * XerviceConfig constructor.
33
     *
34
     * @param \Xervice\Config\Business\ConfigBusinessFactory $factory
35
     */
36 14
    public function __construct(ConfigBusinessFactory $factory)
37
    {
38 14
        $this->factory = $factory;
39 14
        $this->parser = $this->factory->createParser();
40 14
        $this->init();
41 14
    }
42
43
    /**
44
     * @return \Xervice\Config\Business\XerviceConfig
45
     */
46 6
    public static function getInstance(): XerviceConfig
47
    {
48 6
        if (self::$instance === null) {
49 1
            self::$instance = new self(new ConfigBusinessFactory());
50
        }
51 6
        return self::$instance;
52
    }
53
54
    /**
55
     * @param string $key
56
     * @param mixed $default
57
     *
58
     * @return mixed
59
     */
60 6
    public static function get(string $key, $default = null)
61
    {
62 6
        return self::getInstance()->getConfig()->get($key, $default);
63
    }
64
65
    /**
66
     * @param string $key
67
     * @param mixed $value
68
     *
69
     * @return \Xervice\Config\Business\Container\ConfigContainer
70
     */
71 6
    public static function set(string $key, $value)
72
    {
73 6
        return self::getInstance()->getConfig()->set($key, $value);
74
    }
75
76
    /**
77
     * @return \Xervice\Config\Business\Container\ConfigContainer
78
     */
79 14
    public function getConfig(): ConfigContainer
80
    {
81 14
        return $this->factory->getConfigContainer();
82
    }
83
84
    /**
85
     * @param string $file
86
     */
87 14
    private function parseFileIfExist(string $file): void
88
    {
89 14
        $this->parser->parseFile($file);
90 14
    }
91
92 14
    private function init(): void
93
    {
94 14
        $environment = $this->factory->createEnvironment();
95 14
        $rootPath = $this->getRootPath();
96 14
        $configDir = $this->getConfigPath($rootPath);
97
98 14
        $this->parseFileIfExist($configDir . '/config_default.php');
99 14
        $this->parseFileIfExist($configDir . '/config_' . $environment->getEnvironment() . '.php');
100 14
        $this->parseFileIfExist(
101 14
            $configDir . '/config_' . $environment->getEnvironment() . '_' . $environment->getScope() . '.php'
102
        );
103 14
        $this->parseFileIfExist($configDir . '/config_local.php');
104
105 14
        $this->parseAdditionalFiles();
106
107 14
        $this->factory->getConfigContainer()->set(self::APPLICATION_PATH, $rootPath);
108 14
    }
109
110
    /**
111
     * @return string
112
     */
113 14
    private function getRootPath(): string
114
    {
115 14
        return getenv('APPLICATION_PATH') ?: getcwd();
116
    }
117
118
    /**
119
     * @param string $rootPath
120
     *
121
     * @return string
122
     */
123 14
    private function getConfigPath(string $rootPath): string
124
    {
125 14
        return getenv('CONFIG_PATH') ?: $rootPath . '/config/';
126
}
127
128 14
    private function parseAdditionalFiles(): void
129
    {
130 14
        $additionalFiles = $this->getConfig()->get(self::ADDITIONAL_CONFIG_FILES, []);
131
132 14
        if (\is_array($additionalFiles)) {
133 14
            foreach ($additionalFiles as $file) {
134 13
                $this->parseFileIfExist($file);
135
            }
136
        }
137 14
    }
138
}
139