Passed
Push — feature/95-gacela-file-in-diff... ( 9d47f9...5a015a )
by Chema
03:24
created

ConfigFactory::getGacelaPhpPathFromEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
ccs 4
cts 4
cp 1
crap 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\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
        $gacelaPhpPath = $this->getGacelaPhpPath();
43 52
        $fileIo = $this->createFileIo();
44
45 52
        if ($fileIo->existsFile($gacelaPhpPath)) {
46 16
            $factoryFromGacelaPhp = new GacelaConfigUsingGacelaPhpFileFactory($gacelaPhpPath, $this->setup, $fileIo);
47 16
            $gacelaSetupFromGacelaPhp = $factoryFromGacelaPhp->createGacelaFileConfig();
48
        }
49
50 52
        $factoryFromBootstrap = new GacelaConfigFromBootstrapFactory($this->setup);
51 52
        $gacelaSetupFromBootstrap = $factoryFromBootstrap->createGacelaFileConfig();
52
53 52
        if (isset($gacelaSetupFromGacelaPhp) && $gacelaSetupFromGacelaPhp instanceof GacelaConfigFileInterface) {
54 16
            return $gacelaSetupFromBootstrap->combine($gacelaSetupFromGacelaPhp);
55
        }
56
57 36
        return $gacelaSetupFromBootstrap;
58
    }
59
60 48
    private function createPathFinder(): PathFinderInterface
61
    {
62 48
        return new PathFinder();
63
    }
64
65 48
    private function createPathNormalizer(): PathNormalizerInterface
66
    {
67 48
        return new AbsolutePathNormalizer([
68 48
            AbsolutePathNormalizer::WITHOUT_SUFFIX => new WithoutSuffixAbsolutePathStrategy($this->appRootDir),
69 48
            AbsolutePathNormalizer::WITH_SUFFIX => new WithSuffixAbsolutePathStrategy($this->appRootDir, $this->env()),
70
        ]);
71
    }
72
73 52
    private function getGacelaPhpPath(): string
74
    {
75 52
        if ($this->env() === '') {
76 47
            return $this->getGacelaPhpDefaultPath();
77
        }
78
79 5
        $gacelaPhpPathFromEnv = $this->getGacelaPhpPathFromEnv();
80 5
        if ($this->createFileIo()->existsFile($gacelaPhpPathFromEnv)) {
81 2
            return $gacelaPhpPathFromEnv;
82
        }
83
84 3
        return $this->getGacelaPhpDefaultPath();
85
    }
86
87 50
    private function getGacelaPhpDefaultPath(): string
88
    {
89 50
        return sprintf(
90
            '%s/%s%s',
91 50
            $this->appRootDir,
92
            self::GACELA_PHP_CONFIG_FILENAME,
93
            self::GACELA_PHP_CONFIG_EXTENSION
94
        );
95
    }
96
97 5
    private function getGacelaPhpPathFromEnv(): string
98
    {
99 5
        return sprintf(
100
            '%s/%s-%s%s',
101 5
            $this->appRootDir,
102
            self::GACELA_PHP_CONFIG_FILENAME,
103 5
            $this->env(),
104
            self::GACELA_PHP_CONFIG_EXTENSION
105
        );
106
    }
107
108 52
    private function env(): string
109
    {
110 52
        return getenv('APP_ENV') ?: '';
111
    }
112
113 52
    private function createFileIo(): FileIoInterface
114
    {
115 52
        return new FileIo();
116
    }
117
}
118