Passed
Push — feature/94-combine-gacela-file... ( e03dc3...2b5490 )
by Chema
04:26
created

ConfigFactory::createGacelaFileConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 18
rs 9.9332
ccs 11
cts 11
cp 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\AbstractFactory;
8
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigFromBootstrapFactory;
9
use Gacela\Framework\Config\GacelaFileConfig\Factory\GacelaConfigUsingGacelaPhpFileFactory;
10
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
11
use Gacela\Framework\Config\PathNormalizer\AbsolutePathNormalizer;
12
use Gacela\Framework\Config\PathNormalizer\WithoutSuffixAbsolutePathStrategy;
13
use Gacela\Framework\Config\PathNormalizer\WithSuffixAbsolutePathStrategy;
14
use Gacela\Framework\Setup\SetupGacelaInterface;
15
16
final class ConfigFactory extends AbstractFactory
17
{
18
    private const GACELA_PHP_CONFIG_FILENAME = 'gacela.php';
19
20
    private string $appRootDir;
21
22
    private SetupGacelaInterface $setup;
23
24 32
    public function __construct(string $appRootDir, SetupGacelaInterface $setup)
25
    {
26 32
        $this->appRootDir = $appRootDir;
27 32
        $this->setup = $setup;
28
    }
29
30 28
    public function createConfigLoader(): ConfigLoader
31
    {
32 28
        return new ConfigLoader(
33 28
            $this->createGacelaFileConfig(),
34 28
            $this->createPathFinder(),
35 28
            $this->createPathNormalizer(),
36
        );
37
    }
38
39 32
    public function createGacelaFileConfig(): GacelaConfigFileInterface
40
    {
41 32
        $gacelaPhpPath = $this->appRootDir . '/' . self::GACELA_PHP_CONFIG_FILENAME;
42 32
        $fileIo = $this->createFileIo();
43
44 32
        if ($fileIo->existsFile($gacelaPhpPath)) {
45 13
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
46 13
            $gacelaSetupFromGacelaPhp = $factoryFromGacelaPhp->createGacelaFileConfig();
47
        }
48
49 32
        $factoryFromBootstrap = new GacelaConfigFromBootstrapFactory($this->setup);
50 32
        $gacelaSetupFromBootstrap = $factoryFromBootstrap->createGacelaFileConfig();
51
52 32
        if (isset($gacelaSetupFromGacelaPhp) && $gacelaSetupFromGacelaPhp instanceof GacelaConfigFileInterface) {
53 13
            return $gacelaSetupFromBootstrap->combine($gacelaSetupFromGacelaPhp);
54
        }
55
56 19
        return $gacelaSetupFromBootstrap;
57
    }
58
59 32
    private function createFileIo(): FileIoInterface
60
    {
61 32
        return new FileIo();
62
    }
63
64 28
    private function createPathFinder(): PathFinderInterface
65
    {
66 28
        return new PathFinder();
67
    }
68
69 28
    private function createPathNormalizer(): PathNormalizerInterface
70
    {
71 28
        return new AbsolutePathNormalizer([
72 28
            AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir),
73 28
            AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()),
74
        ]);
75
    }
76
77 28
    private function env(): string
78
    {
79 28
        return getenv('APP_ENV') ?: '';
80
    }
81
}
82