Passed
Push — develop ( 667b67...a1f6a6 )
by Brent
03:21
created

Config::defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 3
cts 3
cp 1
crap 1
rs 9.6666
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 Iterator;
9
use Stitcher\Exception\InvalidConfiguration;
10
use Stitcher\File;
11
use Symfony\Component\Finder\Finder;
12
13
class Config
14
{
15
    protected static $env;
16
    protected static $loadedConfiguration = [];
17
    protected static $plugins = [];
18
19 14
    public static function init()
20
    {
21 14
        self::$env = new Dotenv(File::path());
22
23
        try {
24 14
            self::$env->load();
25
        } catch (InvalidPathException $e) {
26
            throw InvalidConfiguration::dotEnvNotFound(File::path());
27
        }
28
29 14
        $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
30
31 14
        $loadedConfiguration = self::load($configurationFiles);
0 ignored issues
show
Bug introduced by
It seems like $configurationFiles defined by \Symfony\Component\Finde...'*.php')->getIterator() on line 29 can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>; however, Pageon\Config::load() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
33 14
        self::registerPlugins($loadedConfiguration);
34
35 14
        self::registerConfiguration($loadedConfiguration);
36 14
    }
37
38 6
    public static function get(string $key)
39
    {
40 6
        return self::$loadedConfiguration[$key] ?? null;
41
    }
42
43 8
    public static function all(): array
44
    {
45 8
        return self::$loadedConfiguration;
46
    }
47
48 8
    public static function plugins(): array
49
    {
50 8
        return self::$plugins;
51
    }
52
53 14
    protected static function defaults(): array
54
    {
55
        return [
56 14
            'rootDirectory' => File::path(),
57 14
            'templateRenderer' => 'twig',
58
            'staticFiles' => [],
59
            'cacheStaticFiles' => false,
60
        ];
61
    }
62
63 14
    protected static function load(Iterator $configurationFiles): array
64
    {
65 14
        $loadedConfiguration = [];
66
67 14
        foreach ($configurationFiles as $configurationFile) {
68 14
            $loadedFileConfiguration = require $configurationFile;
69
70 14
            if (! is_array($loadedFileConfiguration)) {
71
                continue;
72
            }
73
74 14
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
75
        }
76
77 14
        return $loadedConfiguration;
78
    }
79
80 14
    protected static function registerPlugins(array $loadedConfiguration): void
81
    {
82 14
        self::$plugins = $loadedConfiguration['plugins'] ?? [];
83 14
    }
84
85 14
    protected static function registerConfiguration(array $loadedConfiguration): void
86
    {
87 14
        self::$loadedConfiguration = array_merge(
88 14
            self::defaults(),
89 14
            $loadedConfiguration,
90 14
            Arr::dot($loadedConfiguration)
91
        );
92 14
    }
93
}
94