__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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