Passed
Push — master ( 9630dc...59d7dc )
by Jesús
03:24 queued 18s
created

GacelaConfigFileFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 34
c 2
b 0
f 0
dl 0
loc 91
ccs 33
cts 35
cp 0.9429
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createWithDefaultIfEmpty() 0 18 4
A createDefaultGacelaPhpConfig() 0 14 1
A createGacelaFileConfig() 0 24 4
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 $appRootDir;
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 24
    public function __construct(
28
        string $appRootDir,
29
        string $gacelaPhpConfigFilename,
30
        array $globalServices,
31
        ConfigGacelaMapper $configGacelaMapper
32
    ) {
33 24
        $this->appRootDir = $appRootDir;
34 24
        $this->gacelaPhpConfigFilename = $gacelaPhpConfigFilename;
35 24
        $this->globalServices = $globalServices;
36 24
        $this->configGacelaMapper = $configGacelaMapper;
37 24
    }
38
39 24
    public function createGacelaFileConfig(): GacelaConfigFile
40
    {
41 24
        $gacelaPhpPath = $this->appRootDir . '/' . $this->gacelaPhpConfigFilename;
42
43 24
        if (!is_file($gacelaPhpPath)) {
44 17
            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 17
    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 17
        $configFromGlobalServices = $this->globalServices;
74 17
        $configItems = $this->configGacelaMapper->mapConfigItems($configFromGlobalServices['config'] ?? []);
75 17
        $configReaders = $configFromGlobalServices['config-readers'] ?? [];
76 17
        $mappingInterfaces = $configFromGlobalServices['mapping-interfaces'] ?? [];
77
78 17
        return $this->createWithDefaultIfEmpty($configItems, $configReaders, $mappingInterfaces);
79
    }
80
81
    /**
82
     * @param list<GacelaConfigItem> $configItems
83
     * @param array<string,ConfigReaderInterface> $configReaders
84
     * @param array<class-string,class-string|callable> $mappingInterfaces
85
     */
86 24
    private function createWithDefaultIfEmpty(
87
        array $configItems,
88
        array $configReaders,
89
        array $mappingInterfaces
90
    ): GacelaConfigFile {
91 24
        $gacelaConfigFile = GacelaConfigFile::withDefaults();
92
93 24
        if (!empty($configItems)) {
94 3
            $gacelaConfigFile->setConfigItems($configItems);
95
        }
96 24
        if (!empty($configReaders)) {
97 2
            $gacelaConfigFile->setConfigReaders($configReaders);
98
        }
99 24
        if (!empty($mappingInterfaces)) {
100 6
            $gacelaConfigFile->setMappingInterfaces($mappingInterfaces);
101
        }
102
103 24
        return $gacelaConfigFile;
104
    }
105
}
106