Config::defaults()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
ccs 5
cts 5
cp 1
crap 1
rs 9.7998
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 15
    public static function init(): void
20
    {
21 15
        self::$env = new Dotenv(File::path());
22
23
        try {
24 15
            self::$env->load();
25
        } catch (InvalidPathException $e) {
26
            throw InvalidConfiguration::dotEnvNotFound(File::path());
27
        }
28
29 15
        $loadedConfiguration = [];
30
31 15 View Code Duplication
        if (is_dir(File::path('config'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32 15
            $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
33
34 15
            $loadedConfiguration = array_merge($loadedConfiguration, self::load($configurationFiles));
0 ignored issues
show
Bug introduced by
It seems like $configurationFiles defined by \Symfony\Component\Finde...'*.php')->getIterator() on line 32 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...
35
        }
36
37 15 View Code Duplication
        if (file_exists(File::path('src/config.php'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            $sourceConfigurationFile = Finder::create()->files()->in(File::path('src'))->name('config.php')->getIterator();
39
40
            $loadedConfiguration = array_merge($loadedConfiguration, self::load($sourceConfigurationFile));
0 ignored issues
show
Bug introduced by
It seems like $sourceConfigurationFile defined by \Symfony\Component\Finde...ig.php')->getIterator() on line 38 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...
41
        }
42
43 15
        self::registerPlugins($loadedConfiguration);
44
45 15
        self::registerConfiguration($loadedConfiguration);
46 15
    }
47
48 7
    public static function get(string $key)
49
    {
50 7
        return self::$loadedConfiguration[$key] ?? null;
51
    }
52
53 9
    public static function all(): array
54
    {
55 9
        return self::$loadedConfiguration;
56
    }
57
58 9
    public static function plugins(): array
59
    {
60 9
        return self::$plugins;
61
    }
62
63 15
    protected static function defaults(): array
64
    {
65
        return [
66 15
            'rootDirectory' => File::path(),
67 15
            'resourcesPath' => File::path('resources'),
68 15
            'templateRenderer' => 'twig',
69
            'staticFiles' => [],
70
            'cacheStaticFiles' => false,
71
            'cacheImages' => true,
72 15
            'siteUrl' => '',
73
            'errorPages' => [],
74
            'minify' => false,
75
        ];
76
    }
77
78 15
    protected static function load(Iterator $configurationFiles): array
79
    {
80 15
        $loadedConfiguration = [];
81
82 15
        foreach ($configurationFiles as $configurationFile) {
83 15
            $loadedFileConfiguration = require $configurationFile;
84
85 15
            if (! \is_array($loadedFileConfiguration)) {
86
                continue;
87
            }
88
89 15
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
90
        }
91
92 15
        return $loadedConfiguration;
93
    }
94
95 15
    protected static function registerPlugins(array $loadedConfiguration): void
96
    {
97 15
        self::$plugins = $loadedConfiguration['plugins'] ?? [];
98 15
    }
99
100 15
    protected static function registerConfiguration(array $loadedConfiguration): void
101
    {
102 15
        self::$loadedConfiguration = array_merge(
103 15
            self::defaults(),
104 15
            $loadedConfiguration,
105 15
            Arr::dot($loadedConfiguration)
106
        );
107 15
    }
108
}
109