Passed
Push — master ( 2b04c5...3174fa )
by Jesús
01:25 queued 12s
created

Config::getFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\Config\ConfigFactory;
8
use Gacela\Framework\Config\ConfigInit;
9
use Gacela\Framework\Config\ConfigReader\EnvConfigReader;
10
use Gacela\Framework\Config\ConfigReader\PhpConfigReader;
11
use Gacela\Framework\Config\ConfigReaderInterface;
12
use Gacela\Framework\Exception\ConfigException;
13
14
final class Config
15
{
16
    private static ?self $instance = null;
17
18
    private string $applicationRootDir = '';
19
20
    private array $config = [];
21
22
    /** @var array<string, ConfigReaderInterface> */
23
    private array $configReaders;
24
25
    private ?ConfigFactory $configFactory = null;
26
27
    /**
28
     * @param array<string, ConfigReaderInterface> $configReaders
29
     */
30 5
    private function __construct(array $configReaders)
31
    {
32 5
        $this->configReaders = $configReaders;
33 5
    }
34
35 17
    public static function getInstance(): self
36
    {
37 17
        if (self::$instance === null) {
38 5
            self::$instance = new self([
39 5
                'php' => new PhpConfigReader(),
40 5
                'env' => new EnvConfigReader(),
41
            ]);
42
        }
43
44 17
        return self::$instance;
45
    }
46
47 3
    public static function resetInstance(): void
48
    {
49 3
        self::$instance = null;
50 3
    }
51
52
    /**
53
     * @param array<string, ConfigReaderInterface> $configReaders
54
     */
55 1
    public static function setConfigReaders(array $configReaders = []): void
56
    {
57 1
        self::$instance = new self($configReaders);
58 1
    }
59
60
    /**
61
     * @param null|mixed $default
62
     *
63
     * @throws ConfigException
64
     *
65
     * @return mixed
66
     */
67 9
    public function get(string $key, $default = null)
68
    {
69 9
        if (empty($this->config)) {
70 3
            $this->init($this->getApplicationRootDir());
71
        }
72
73 9
        if ($default !== null && !$this->hasValue($key)) {
74 1
            return $default;
75
        }
76
77 8
        if (!$this->hasValue($key)) {
78 1
            throw ConfigException::keyNotFound($key, self::class);
79
        }
80
81 7
        return $this->config[$key];
82
    }
83
84
    /**
85
     * @throws ConfigException
86
     */
87 10
    public function init(string $applicationRootDir): void
88
    {
89 10
        $this->setApplicationRootDir($applicationRootDir);
90
91 10
        $this->config = (new ConfigInit(
92 10
            $this->getApplicationRootDir(),
93 10
            $this->getFactory()->createGacelaConfigFileFactory(),
94 10
            $this->getFactory()->createPathFinder(),
95 10
            $this->configReaders
96 10
        ))->readAll();
97 10
    }
98
99 10
    public function setApplicationRootDir(string $dir): void
100
    {
101 10
        $this->applicationRootDir = $dir;
102 10
    }
103
104 17
    public function getApplicationRootDir(): string
105
    {
106 17
        if (empty($this->applicationRootDir)) {
107 4
            $this->applicationRootDir = getcwd() ?: '';
108
        }
109
110 17
        return $this->applicationRootDir;
111
    }
112
113 9
    private function hasValue(string $key): bool
114
    {
115 9
        return isset($this->config[$key]);
116
    }
117
118 10
    private function getFactory(): ConfigFactory
119
    {
120 10
        if (null === $this->configFactory) {
121 5
            $this->configFactory = new ConfigFactory();
122
        }
123
124 10
        return $this->configFactory;
125
    }
126
}
127