Passed
Push — develop ( 80559e...1e38ed )
by Brent
02:12
created

Config::init()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.128

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 1
dl 0
loc 28
ccs 12
cts 15
cp 0.8
crap 4.128
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Exception\InvalidPathException;
7
use Illuminate\Support\Arr;
8
use Stitcher\Exception\InvalidConfiguration;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
class Config
13
{
14
    protected static $env;
15
    protected static $loadedConfig = [];
16
17 6
    public static function init(string $basePath)
18
    {
19 6
        $basePath = rtrim($basePath, '/');
20 6
        self::$env = new Dotenv($basePath);
21
22
        try {
23 6
            self::$env->load();
24
        } catch (InvalidPathException $e) {
25
            throw InvalidConfiguration::dotEnvNotFound($basePath);
26
        }
27
28 6
        $configFiles = Finder::create()->files()->in($basePath . '/config')->name('*.php');
29
30 6
        $unparsedConfig = [];
31
32
        /** @var SplFileInfo $configFile */
33 6
        foreach ($configFiles as $configFile) {
34 6
            $fileConfig = require $configFile;
35
36 6
            if (!is_array($fileConfig)) {
37
                continue;
38
            }
39
40 6
            $unparsedConfig = array_merge($unparsedConfig, $fileConfig);
41
        }
42
43 6
        self::$loadedConfig = Arr::dot($unparsedConfig);
44 6
    }
45
46 4
    public static function get(string $key)
47
    {
48 4
        return self::$loadedConfig[$key] ?? null;
49
    }
50
51 1
    public static function all(): array
52
    {
53 1
        return self::$loadedConfig;
54
    }
55
}
56