Passed
Pull Request — feature/95-gacela-file-in-diff... (#181)
by Jesús
03:37
created

ConfigFactory::getGacelaPhpPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
ccs 7
cts 7
cp 1
crap 3
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 string $appRootDir;
22
23
    private SetupGacelaInterface $setup;
24
25 52
    public function __construct(string $appRootDir, SetupGacelaInterface $setup)
26
    {
27 52
        $this->appRootDir = $appRootDir;
28 52
        $this->setup = $setup;
29
    }
30
31 48
    public function createConfigLoader(): ConfigLoader
32
    {
33 48
        return new ConfigLoader(
34 48
            $this->createGacelaFileConfig(),
35 48
            $this->createPathFinder(),
36 48
            $this->createPathNormalizer(),
37
        );
38
    }
39
40 52
    public function createGacelaFileConfig(): GacelaConfigFileInterface
41
    {
42 52
        $gacelaSetups = [];
43 52
        $fileIo = $this->createFileIo();
44
45 52
        $gacelaPhpDefaultPath = $this->getGacelaPhpDefaultPath();
46 52
        if ($fileIo->existsFile($gacelaPhpDefaultPath)) {
47 16
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpDefaultPath, $this->setup, $fileIo);
48 16
            $gacelaSetups[] = $factoryFromGacelaPhp->createGacelaFileConfig();
49
        }
50
51 52
        $gacelaPhpPath = $this->getGacelaPhpPathFromEnv();
52 52
        if ($fileIo->existsFile($gacelaPhpPath)) {
53 2
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
54 2
            $gacelaSetups[] = $factoryFromGacelaPhp->createGacelaFileConfig();
55
        }
56
57 52
        return array_reduce(
58
            $gacelaSetups,
59 52
            static fn (GacelaConfigFileInterface $carry, GacelaConfigFileInterface $item): GacelaConfigFileInterface => $carry->combine($item),
60 52
            (new GacelaConfigFromBootstrapFactory($this->setup))->createGacelaFileConfig()
61
        );
62
    }
63
64 52
    private function createFileIo(): FileIoInterface
65
    {
66 52
        return new FileIo();
67
    }
68
69 52
    private function getGacelaPhpDefaultPath(): string
70
    {
71 52
        return sprintf(
72
            '%s/%s%s',
73 52
            $this->appRootDir,
74
            self::GACELA_PHP_CONFIG_FILENAME,
75
            self::GACELA_PHP_CONFIG_EXTENSION
76
        );
77
    }
78
79 52
    private function getGacelaPhpPathFromEnv(): string
80
    {
81 52
        return sprintf(
82
            '%s/%s-%s%s',
83 52
            $this->appRootDir,
84
            self::GACELA_PHP_CONFIG_FILENAME,
85 52
            $this->env(),
86
            self::GACELA_PHP_CONFIG_EXTENSION
87
        );
88
    }
89
90 52
    private function env(): string
91
    {
92 52
        return getenv('APP_ENV') ?: '';
93
    }
94
95 48
    private function createPathFinder(): PathFinderInterface
96
    {
97 48
        return new PathFinder();
98
    }
99
100 48
    private function createPathNormalizer(): PathNormalizerInterface
101
    {
102 48
        return new AbsolutePathNormalizer([
103 48
            AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir),
104 48
            AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()),
105
        ]);
106
    }
107
}
108