Passed
Pull Request — master (#249)
by Chema
02:50
created

ConfigFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
c 5
b 0
f 0
dl 0
loc 102
ccs 39
cts 39
cp 1
rs 10
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createConfigLoader() 0 6 1
A createPathNormalizer() 0 5 1
A createFileIo() 0 3 1
A createGacelaFileConfig() 0 32 4
A __construct() 0 4 1
A env() 0 3 2
A getGacelaPhpDefaultPath() 0 7 1
A resetCache() 0 3 1
A createPathFinder() 0 3 1
A getGacelaPhpPathFromEnv() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\AbstractFactory;
8
use Gacela\Framework\Bootstrap\SetupGacelaInterface;
9
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigFromBootstrapFactory;
10
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigUsingGacelaPhpFileFactory;
11
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
12
use Gacela\Framework\Config\PathNormalizer\AbsolutePathNormalizer;
13
use Gacela\Framework\Config\PathNormalizer\WithoutSuffixAbsolutePathStrategy;
14
use Gacela\Framework\Config\PathNormalizer\WithSuffixAbsolutePathStrategy;
15
16
final class ConfigFactory extends AbstractFactory
17
{
18
    private const GACELA_PHP_CONFIG_FILENAME = 'gacela';
19
    private const GACELA_PHP_CONFIG_EXTENSION = '.php';
20
21
    private static ?GacelaConfigFileInterface $gacelaFileConfig = null;
22
23
    public function __construct(
24
        private string $appRootDir,
25 83
        private SetupGacelaInterface $setup,
26
    ) {
27 83
    }
28 83
29
    public static function resetCache(): void
30
    {
31 79
        self::$gacelaFileConfig = null;
32
    }
33 79
34 79
    public function createConfigLoader(): ConfigLoader
35 79
    {
36 79
        return new ConfigLoader(
37
            $this->createGacelaFileConfig(),
38
            $this->createPathFinder(),
39
            $this->createPathNormalizer(),
40 83
        );
41
    }
42 83
43 83
    public function createGacelaFileConfig(): GacelaConfigFileInterface
44
    {
45 83
        if (self::$gacelaFileConfig !== null) {
46 83
            return self::$gacelaFileConfig;
47 16
        }
48 16
49
        $gacelaConfigFiles = [];
50
        $fileIo = $this->createFileIo();
51 83
52 83
        $gacelaPhpDefaultPath = $this->getGacelaPhpDefaultPath();
53 2
        if ($fileIo->existsFile($gacelaPhpDefaultPath)) {
54 2
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory(
55
                $gacelaPhpDefaultPath,
56
                $this->setup,
57 83
                $fileIo,
58
            );
59 83
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
60 83
        }
61
62
        $gacelaPhpPath = $this->getGacelaPhpPathFromEnv();
63
        if ($fileIo->existsFile($gacelaPhpPath)) {
64 83
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
65
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
66 83
        }
67
68
        self::$gacelaFileConfig = array_reduce(
69 83
            $gacelaConfigFiles,
70
            static fn (GacelaConfigFileInterface $carry, GacelaConfigFileInterface $item) => $carry->combine($item),
71 83
            (new GacelaConfigFromBootstrapFactory($this->setup))->createGacelaFileConfig(),
72
        );
73 83
74
        return self::$gacelaFileConfig;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::gacelaFileConfig returns the type null which is incompatible with the type-hinted return Gacela\Framework\Config\...celaConfigFileInterface.
Loading history...
75
    }
76
77
    private function createFileIo(): FileIoInterface
78
    {
79 83
        return new FileIo();
80
    }
81 83
82
    private function getGacelaPhpDefaultPath(): string
83 83
    {
84
        return sprintf(
85 83
            '%s/%s%s',
86
            $this->appRootDir,
87
            self::GACELA_PHP_CONFIG_FILENAME,
88
            self::GACELA_PHP_CONFIG_EXTENSION,
89
        );
90 83
    }
91
92 83
    private function getGacelaPhpPathFromEnv(): string
93
    {
94
        return sprintf(
95 79
            '%s/%s-%s%s',
96
            $this->appRootDir,
97 79
            self::GACELA_PHP_CONFIG_FILENAME,
98
            $this->env(),
99
            self::GACELA_PHP_CONFIG_EXTENSION,
100 79
        );
101
    }
102 79
103 79
    private function env(): string
104 79
    {
105
        return getenv('APP_ENV') ?: '';
106
    }
107
108
    private function createPathFinder(): PathFinderInterface
109
    {
110
        return new PathFinder();
111
    }
112
113
    private function createPathNormalizer(): PathNormalizerInterface
114
    {
115
        return new AbsolutePathNormalizer([
116
            AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir),
117
            AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()),
118
        ]);
119
    }
120
}
121