Passed
Push — feature/remove-deprecated-setu... ( 0b42cc )
by Chema
09:26
created

GacelaConfigUsingGacelaPhpFileFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 58
c 1
b 0
f 0
ccs 25
cts 26
cp 0.9615
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A createGacelaFileConfig() 0 25 3
A createMappingInterfacesBuilder() 0 3 1
A createConfigBuilder() 0 3 1
A createSuffixTypesBuilder() 0 3 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 22
    public function __construct(
30
        string $gacelaPhpPath,
31
        SetupGacelaInterface $setup,
32
        FileIoInterface $fileIo
33
    ) {
34 22
        $this->gacelaPhpPath = $gacelaPhpPath;
35 22
        $this->setup = $setup;
36 22
        $this->fileIo = $fileIo;
37
    }
38
39 22
    public function createGacelaFileConfig(): GacelaConfigFileInterface
40
    {
41
        /** @var callable(GacelaConfig):void $configFn */
42 22
        $configFn = $this->fileIo->include($this->gacelaPhpPath);
43 22
        if (!is_callable($configFn)) {
44 2
            throw new RuntimeException('`gacela.php` file should return a `callable(GacelaConfig)`');
45
        }
46
47 20
        $gacelaConfig = new GacelaConfig($this->setup->externalServices());
48 20
        $configFn($gacelaConfig);
49 20
        $setupGacela = SetupGacela::fromGacelaConfig($gacelaConfig);
50
51
        /** @var object $setupGacela */
52 20
        if (!is_subclass_of($setupGacela, SetupGacelaInterface::class)) {
53
            throw new RuntimeException('`gacela.php` file should return a `callable(GacelaConfig)`');
54
        }
55
56 20
        $configBuilder = $this->createConfigBuilder($setupGacela);
57 20
        $mappingInterfacesBuilder = $this->createMappingInterfacesBuilder($setupGacela);
58 20
        $suffixTypesBuilder = $this->createSuffixTypesBuilder($setupGacela);
59
60 20
        return (new GacelaConfigFile())
61 20
            ->setConfigItems($configBuilder->build())
62 20
            ->setMappingInterfaces($mappingInterfacesBuilder->build())
63 20
            ->setSuffixTypes($suffixTypesBuilder->build());
64
    }
65
66 20
    private function createConfigBuilder(SetupGacelaInterface $setupGacela): ConfigBuilder
67
    {
68 20
        return $setupGacela->buildConfig(new ConfigBuilder());
69
    }
70
71 20
    private function createMappingInterfacesBuilder(SetupGacelaInterface $setupGacela): MappingInterfacesBuilder
72
    {
73 20
        return $setupGacela->buildMappingInterfaces(new MappingInterfacesBuilder(), $this->setup->externalServices());
74
    }
75
76 20
    private function createSuffixTypesBuilder(SetupGacelaInterface $setupGacela): SuffixTypesBuilder
77
    {
78 20
        return $setupGacela->buildSuffixTypes(new SuffixTypesBuilder());
79
    }
80
}
81