Passed
Push — master ( fa1416...da5f2f )
by Jesús
04:02 queued 12s
created

createDefaultGacelaPhpConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Config;
6
7
use Gacela\Framework\AbstractConfigGacela;
8
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile;
9
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem;
10
use RuntimeException;
11
use function is_callable;
12
13
final class GacelaConfigFileFactory implements GacelaConfigFileFactoryInterface
14
{
15
    private string $applicationRootDir;
16
17
    private string $gacelaPhpConfigFilename;
18
19
    /** @var array<string,mixed> */
20
    private array $globalServices;
21
22
    private ConfigGacelaMapper $configGacelaMapper;
23
24
    /**
25
     * @param array<string,mixed> $globalServices
26
     */
27 22
    public function __construct(
28
        string $applicationRootDir,
29
        string $gacelaPhpConfigFilename,
30
        array $globalServices,
31
        ConfigGacelaMapper $configGacelaMapper
32
    ) {
33 22
        $this->applicationRootDir = $applicationRootDir;
34 22
        $this->gacelaPhpConfigFilename = $gacelaPhpConfigFilename;
35 22
        $this->globalServices = $globalServices;
36 22
        $this->configGacelaMapper = $configGacelaMapper;
37 22
    }
38
39 22
    public function createGacelaFileConfig(): GacelaConfigFile
40
    {
41 22
        $gacelaPhpPath = $this->applicationRootDir . '/' . $this->gacelaPhpConfigFilename;
42
43 22
        if (!is_file($gacelaPhpPath)) {
44 15
            return $this->createDefaultGacelaPhpConfig();
45
        }
46
47 7
        $configGacela = include $gacelaPhpPath;
48 7
        if (!is_callable($configGacela)) {
49
            throw new RuntimeException('Create a function that returns an anonymous class that extends AbstractConfigGacela');
50
        }
51
52
        /** @var AbstractConfigGacela $configGacelaClass */
53 7
        $configGacelaClass = $configGacela();
54 7
        if (!is_subclass_of($configGacelaClass, AbstractConfigGacela::class)) {
55
            throw new RuntimeException('Your anonymous class must extends AbstractConfigGacela');
56
        }
57
58 7
        $configItems = $this->configGacelaMapper->mapConfigItems($configGacelaClass->config());
59 7
        $configReaders = $configGacelaClass->configReaders();
60 7
        $mappingInterfaces = $configGacelaClass->mappingInterfaces($this->globalServices);
61
62 7
        return $this->createWithDefaultIfEmpty($configItems, $configReaders, $mappingInterfaces);
63
    }
64
65 15
    private function createDefaultGacelaPhpConfig(): GacelaConfigFile
66
    {
67
        /** @var array{
68
         *     config?: array<array>|array{type:string,path:string,path_local:string},
69
         *     config-readers?: array<string,ConfigReaderInterface>,
70
         *     mapping-interfaces?: array<class-string,class-string|callable>,
71
         * } $configFromGlobalServices
72
         */
73 15
        $configFromGlobalServices = $this->globalServices;
74 15
        $configItems = $this->configGacelaMapper->mapConfigItems($configFromGlobalServices['config'] ?? []);
75 15
        $configReaders = $configFromGlobalServices['config-readers'] ?? [];
76 15
        $mappingInterfaces = $configFromGlobalServices['mapping-interfaces'] ?? [];
77
78 15
        return $this->createWithDefaultIfEmpty($configItems, $configReaders, $mappingInterfaces);
79
    }
80
81
    /**
82
     * @param array<string,GacelaConfigItem> $configItems
83
     * @param array<string,ConfigReaderInterface> $configReaders
84
     * @param array<class-string,class-string|callable> $mappingInterfaces
85
     */
86 22
    private function createWithDefaultIfEmpty(
87
        array $configItems,
88
        array $configReaders,
89
        array $mappingInterfaces
90
    ): GacelaConfigFile {
91 22
        $gacelaConfigFile = GacelaConfigFile::withDefaults();
92
93 22
        if (!empty($configItems)) {
94 3
            $gacelaConfigFile->setConfigItems($configItems);
95
        }
96 22
        if (!empty($configReaders)) {
97 2
            $gacelaConfigFile->setConfigReaders($configReaders);
98
        }
99 22
        if (!empty($mappingInterfaces)) {
100 6
            $gacelaConfigFile->setMappingInterfaces($mappingInterfaces);
101
        }
102
103 22
        return $gacelaConfigFile;
104
    }
105
}
106