Passed
Push — master ( 72f8f4...a05bce )
by Jesús
07:47 queued 11s
created

GacelaConfigFileFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
c 2
b 0
f 0
dl 0
loc 84
ccs 29
cts 31
cp 0.9355
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createWithDefaultIfEmpty() 0 14 3
A createDefaultGacelaPhpConfig() 0 13 1
A createGacelaFileConfig() 0 23 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
        $mappingInterfaces = $configGacelaClass->mappingInterfaces($this->globalServices);
60
61 7
        return $this->createWithDefaultIfEmpty($configItems, $mappingInterfaces);
62
    }
63
64 17
    private function createDefaultGacelaPhpConfig(): GacelaConfigFile
65
    {
66
        /**
67
         * @var array{
68
         *     config?: list<array{path?:string, path_local?:string, reader?:ConfigReaderInterface}>|array{path?:string, path_local?:string, reader?:ConfigReaderInterface},
69
         *     mapping-interfaces?: array<class-string,class-string|callable>,
70
         * } $configFromGlobalServices
71
         */
72 17
        $configFromGlobalServices = $this->globalServices;
73 17
        $configItems = $this->configGacelaMapper->mapConfigItems($configFromGlobalServices['config'] ?? []);
74 17
        $mappingInterfaces = $configFromGlobalServices['mapping-interfaces'] ?? [];
75
76 17
        return $this->createWithDefaultIfEmpty($configItems, $mappingInterfaces);
77
    }
78
79
    /**
80
     * @param list<GacelaConfigItem> $configItems
81
     * @param array<class-string,class-string|callable> $mappingInterfaces
82
     */
83 24
    private function createWithDefaultIfEmpty(
84
        array $configItems,
85
        array $mappingInterfaces
86
    ): GacelaConfigFile {
87 24
        $gacelaConfigFile = GacelaConfigFile::withDefaults();
88
89 24
        if (!empty($configItems)) {
90 3
            $gacelaConfigFile->setConfigItems($configItems);
91
        }
92 24
        if (!empty($mappingInterfaces)) {
93 6
            $gacelaConfigFile->setMappingInterfaces($mappingInterfaces);
94
        }
95
96 24
        return $gacelaConfigFile;
97
    }
98
}
99