Completed
Push — master ( b7f544...05e48a )
by Mike
05:04
created

XerviceConfig   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 88.57%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 102
ccs 31
cts 35
cp 0.8857
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A parseAdditionalFiles() 0 6 3
A parseFileIfExist() 0 3 1
A getRootPath() 0 3 2
A getInstance() 0 6 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 8
    public function __construct(ConfigBusinessFactory $factory)
37
    {
38 8
        $this->factory = $factory;
39 8
        $this->parser = $this->factory->createParser();
40 8
        $this->init();
41 8
    }
42
43
    /**
44
     * @return \Xervice\Config\Business\XerviceConfig
45
     */
46
    public static function getInstance(): XerviceConfig
47
    {
48
        if (self::$instance === null) {
49
            self::$instance = new self(new ConfigBusinessFactory());
50
        }
51
        return self::$instance;
52
    }
53
54
    /**
55
     * @return \Xervice\Config\Business\Container\ConfigContainer
56
     */
57 8
    public function getConfig(): ConfigContainer
58
    {
59 8
        return $this->factory->getConfigContainer();
60
    }
61
62
    /**
63
     * @param string $file
64
     */
65 8
    private function parseFileIfExist(string $file): void
66
    {
67 8
        $this->parser->parseFile($file);
68 8
    }
69
70 8
    private function init(): void
71
    {
72 8
        $environment = $this->factory->createEnvironment();
73 8
        $rootPath = $this->getRootPath();
74 8
        $configDir = $this->getConfigPath($rootPath);
75
76 8
        $this->parseFileIfExist($configDir . '/config_default.php');
77 8
        $this->parseFileIfExist($configDir . '/config_' . $environment->getEnvironment() . '.php');
78 8
        $this->parseFileIfExist(
79 8
            $configDir . '/config_' . $environment->getEnvironment() . '_' . $environment->getScope() . '.php'
80
        );
81 8
        $this->parseFileIfExist($configDir . '/config_local.php');
82
83 8
        $this->parseAdditionalFiles();
84
85 8
        $this->factory->getConfigContainer()->set(self::APPLICATION_PATH, $rootPath);
86 8
    }
87
88
    /**
89
     * @return string
90
     */
91 8
    private function getRootPath(): string
92
    {
93 8
        return getenv('APPLICATION_PATH') ?: getcwd();
94
    }
95
96
    /**
97
     * @param $rootPath
98
     *
99
     * @return string
100
     */
101 8
    private function getConfigPath($rootPath): string
102
    {
103 8
        return getenv('CONFIG_PATH') ?: $rootPath . '/config/';
104
}
105
106 8
    private function parseAdditionalFiles(): void
107
    {
108 8
        $additionalFiles = (array)$this->getConfig()->get(self::ADDITIONAL_CONFIG_FILES, []);
109 8
        if ($additionalFiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $additionalFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110 7
            foreach ($additionalFiles as $file) {
111 7
                $this->parseFileIfExist($file);
112
            }
113
        }
114 8
    }
115
}
116