Passed
Push — master ( 13320f...91e8dd )
by Chema
03:27
created

createMappingInterfacesBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config\GacelaFileConfig\Factory;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\Bootstrap\SetupGacela;
9
use Gacela\Framework\Bootstrap\SetupGacelaInterface;
10
use Gacela\Framework\Config\FileIoInterface;
11
use Gacela\Framework\Config\GacelaConfigBuilder\ConfigBuilder;
12
use Gacela\Framework\Config\GacelaConfigBuilder\MappingInterfacesBuilder;
13
use Gacela\Framework\Config\GacelaConfigBuilder\SuffixTypesBuilder;
14
use Gacela\Framework\Config\GacelaConfigFileFactoryInterface;
15
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile;
16
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface;
17
use RuntimeException;
18
19
use function is_callable;
20
21
final class GacelaConfigUsingGacelaPhpFileFactory implements GacelaConfigFileFactoryInterface
22
{
23
    private string $gacelaPhpPath;
24
25
    private SetupGacelaInterface $setup;
26
27
    private FileIoInterface $fileIo;
28
29 21
    public function __construct(
30
        string $gacelaPhpPath,
31
        SetupGacelaInterface $setup,
32
        FileIoInterface $fileIo
33
    ) {
34 21
        $this->gacelaPhpPath = $gacelaPhpPath;
35 21
        $this->setup = $setup;
36 21
        $this->fileIo = $fileIo;
37
    }
38
39 21
    public function createGacelaFileConfig(): GacelaConfigFileInterface
40
    {
41 21
        $gacelaConfig = $this->createGacelaConfig();
42 20
        $setupGacela = SetupGacela::fromGacelaConfig($gacelaConfig);
43
44 20
        $configBuilder = $this->createConfigBuilder($setupGacela);
45 20
        $mappingInterfacesBuilder = $this->createMappingInterfacesBuilder($setupGacela);
46 20
        $suffixTypesBuilder = $this->createSuffixTypesBuilder($setupGacela);
47
48 20
        return (new GacelaConfigFile())
49 20
            ->setConfigItems($configBuilder->build())
50 20
            ->setMappingInterfaces($mappingInterfacesBuilder->build())
51 20
            ->setSuffixTypes($suffixTypesBuilder->build());
52
    }
53
54 21
    private function createGacelaConfig(): GacelaConfig
55
    {
56 21
        $gacelaConfig = new GacelaConfig($this->setup->externalServices());
57
58
        /** @var callable(GacelaConfig):void|null $configFn */
59 21
        $configFn = $this->fileIo->include($this->gacelaPhpPath);
60
61 21
        if (!is_callable($configFn)) {
62 1
            throw new RuntimeException('`gacela.php` file should return a `callable(GacelaConfig)`');
63
        }
64
65 20
        $configFn($gacelaConfig);
66
67 20
        return $gacelaConfig;
68
    }
69
70 20
    private function createConfigBuilder(SetupGacelaInterface $setupGacela): ConfigBuilder
71
    {
72 20
        return $setupGacela->buildConfig(new ConfigBuilder());
73
    }
74
75 20
    private function createMappingInterfacesBuilder(SetupGacelaInterface $setupGacela): MappingInterfacesBuilder
76
    {
77 20
        return $setupGacela->buildMappingInterfaces(new MappingInterfacesBuilder(), $this->setup->externalServices());
78
    }
79
80 20
    private function createSuffixTypesBuilder(SetupGacelaInterface $setupGacela): SuffixTypesBuilder
81
    {
82 20
        return $setupGacela->buildSuffixTypes(new SuffixTypesBuilder());
83
    }
84
}
85