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

ConfigFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 26
c 3
b 0
f 0
dl 0
loc 64
rs 10
ccs 29
cts 29
cp 1
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createFileIo() 0 3 1
A createConfigLoader() 0 6 1
A createPathNormalizer() 0 5 1
A createGacelaFileConfig() 0 18 4
A __construct() 0 4 1
A env() 0 3 2
A createPathFinder() 0 3 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\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