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

ConfigFactory::resetCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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