Passed
Push — fix/gacela-file-config-cache ( dec2fa )
by Chema
03:34
created

ConfigFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
c 5
b 0
f 0
dl 0
loc 102
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
        private SetupGacelaInterface $setup,
26
    ) {
27
    }
28
29
    public static function resetCache(): void
30
    {
31
        self::$gacelaFileConfig = null;
32
    }
33
34
    public function createConfigLoader(): ConfigLoader
35
    {
36
        return new ConfigLoader(
37
            $this->createGacelaFileConfig(),
38
            $this->createPathFinder(),
39
            $this->createPathNormalizer(),
40
        );
41
    }
42
43
    public function createGacelaFileConfig(): GacelaConfigFileInterface
44
    {
45
        if (self::$gacelaFileConfig !== null) {
46
            return self::$gacelaFileConfig;
47
        }
48
49
        $gacelaConfigFiles = [];
50
        $fileIo = $this->createFileIo();
51
52
        $gacelaPhpDefaultPath = $this->getGacelaPhpDefaultPath();
53
        if ($fileIo->existsFile($gacelaPhpDefaultPath)) {
54
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory(
55
                $gacelaPhpDefaultPath,
56
                $this->setup,
57
                $fileIo,
58
            );
59
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
60
        }
61
62
        $gacelaPhpPath = $this->getGacelaPhpPathFromEnv();
63
        if ($fileIo->existsFile($gacelaPhpPath)) {
64
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
65
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
66
        }
67
68
        self::$gacelaFileConfig = array_reduce(
69
            $gacelaConfigFiles,
70
            static fn (GacelaConfigFileInterface $carry, GacelaConfigFileInterface $item) => $carry->combine($item),
71
            (new GacelaConfigFromBootstrapFactory($this->setup))->createGacelaFileConfig(),
72
        );
73
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
        return new FileIo();
80
    }
81
82
    private function getGacelaPhpDefaultPath(): string
83
    {
84
        return sprintf(
85
            '%s/%s%s',
86
            $this->appRootDir,
87
            self::GACELA_PHP_CONFIG_FILENAME,
88
            self::GACELA_PHP_CONFIG_EXTENSION,
89
        );
90
    }
91
92
    private function getGacelaPhpPathFromEnv(): string
93
    {
94
        return sprintf(
95
            '%s/%s-%s%s',
96
            $this->appRootDir,
97
            self::GACELA_PHP_CONFIG_FILENAME,
98
            $this->env(),
99
            self::GACELA_PHP_CONFIG_EXTENSION,
100
        );
101
    }
102
103
    private function env(): string
104
    {
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