ConfigFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\AbstractConfig;
8
use Gacela\Framework\AbstractFactory;
9
use Gacela\Framework\Bootstrap\SetupGacelaInterface;
10
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigFromBootstrapFactory;
11
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigUsingGacelaPhpFileFactory;
12
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
13
use Gacela\Framework\Config\PathNormalizer\AbsolutePathNormalizer;
14
use Gacela\Framework\Config\PathNormalizer\WithoutSuffixAbsolutePathStrategy;
15
use Gacela\Framework\Config\PathNormalizer\WithSuffixAbsolutePathStrategy;
16
17
use function sprintf;
18
19
/**
20
 * @extends AbstractFactory<AbstractConfig>
21
 */
22
final class ConfigFactory extends AbstractFactory
23
{
24
    private const GACELA_PHP_CONFIG_FILENAME = 'gacela';
25
26
    private const GACELA_PHP_CONFIG_EXTENSION = '.php';
27
28
    private static ?GacelaConfigFileInterface $gacelaFileConfig = null;
29
30
    public function __construct(
31
        private readonly string $appRootDir,
32
        private readonly SetupGacelaInterface $setup,
33
    ) {
34
    }
35
36
    public static function resetCache(): void
37
    {
38
        self::$gacelaFileConfig = null;
39
    }
40
41
    public function createConfigLoader(): ConfigLoader
42
    {
43
        return new ConfigLoader(
44
            $this->createGacelaFileConfig(),
45
            $this->createPathFinder(),
46
            $this->createPathNormalizer(),
47
        );
48
    }
49
50
    public function createGacelaFileConfig(): GacelaConfigFileInterface
51
    {
52
        if (self::$gacelaFileConfig instanceof GacelaConfigFileInterface) {
53
            return self::$gacelaFileConfig;
54
        }
55
56
        $gacelaConfigFiles = [];
57
        $fileIo = $this->createFileIo();
58
59
        $gacelaPhpDefaultPath = $this->getGacelaPhpDefaultPath();
60
        if ($fileIo->existsFile($gacelaPhpDefaultPath)) {
61
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory(
62
                $gacelaPhpDefaultPath,
63
                $this->setup,
64
                $fileIo,
65
            );
66
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
67
        }
68
69
        $gacelaPhpPath = $this->getGacelaPhpPathFromEnv();
70
        if ($fileIo->existsFile($gacelaPhpPath)) {
71
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
72
            $gacelaConfigFiles[] = $factoryFromGacelaPhp->createGacelaFileConfig();
73
        }
74
75
        self::$gacelaFileConfig = array_reduce(
76
            $gacelaConfigFiles,
77
            static fn (GacelaConfigFileInterface $carry, GacelaConfigFileInterface $item): GacelaConfigFileInterface => $carry->combine($item),
78
            (new GacelaConfigFromBootstrapFactory($this->setup))->createGacelaFileConfig(),
79
        );
80
81
        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...
82
    }
83
84
    private function createFileIo(): FileIoInterface
85
    {
86
        return new FileIo();
87
    }
88
89
    private function getGacelaPhpDefaultPath(): string
90
    {
91
        return sprintf(
92
            '%s/%s%s',
93
            $this->appRootDir,
94
            self::GACELA_PHP_CONFIG_FILENAME,
95
            self::GACELA_PHP_CONFIG_EXTENSION,
96
        );
97
    }
98
99
    private function getGacelaPhpPathFromEnv(): string
100
    {
101
        return sprintf(
102
            '%s/%s-%s%s',
103
            $this->appRootDir,
104
            self::GACELA_PHP_CONFIG_FILENAME,
105
            $this->env(),
106
            self::GACELA_PHP_CONFIG_EXTENSION,
107
        );
108
    }
109
110
    private function env(): string
111
    {
112
        return getenv('APP_ENV') ?: '';
113
    }
114
115
    private function createPathFinder(): PathFinderInterface
116
    {
117
        return new PathFinder();
118
    }
119
120
    private function createPathNormalizer(): PathNormalizerInterface
121
    {
122
        return new AbsolutePathNormalizer([
123
            AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir),
124
            AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()),
125
        ]);
126
    }
127
}
128