Passed
Pull Request — master (#54)
by Jesús
02:44
created

GacelaConfigFileFactory::createGacelaFileConfig()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.1647

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
nc 5
nop 0
dl 0
loc 32
rs 9.4555
c 1
b 0
f 0
ccs 13
cts 16
cp 0.8125
crap 5.1647
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 RuntimeException;
10
use function is_array;
11
use function is_callable;
12
13
final class GacelaConfigFileFactory implements GacelaConfigFileFactoryInterface
14
{
15
    private string $applicationRootDir;
16
    private array $globalServices;
17
    private string $gacelaPhpConfigFilename;
18
19
    /**
20
     * @param array<string, mixed> $globalServices
21
     */
22 17
    public function __construct(
23
        string $applicationRootDir,
24
        array $globalServices,
25
        string $gacelaPhpConfigFilename
26
    ) {
27 17
        $this->applicationRootDir = $applicationRootDir;
28 17
        $this->globalServices = $globalServices;
29 17
        $this->gacelaPhpConfigFilename = $gacelaPhpConfigFilename;
30 17
    }
31
32 17
    public function createGacelaFileConfig(): GacelaConfigFile
33
    {
34 17
        $gacelaPhpPath = $this->applicationRootDir . '/' . $this->gacelaPhpConfigFilename;
35
36 17
        if (!is_file($gacelaPhpPath)) {
37 12
            return GacelaConfigFile::withDefaults();
38
        }
39
40
        /** @var array|callable|mixed $configGacela */
41 5
        $configGacela = include $gacelaPhpPath;
42
43 5
        if (is_callable($configGacela)) {
44
            /** @var AbstractConfigGacela $configGacelaClass */
45 4
            $configGacelaClass = $configGacela($this->globalServices);
46 4
            if (!is_subclass_of($configGacelaClass, AbstractConfigGacela::class)) {
47
                throw new RuntimeException('Your anonymous class must extends AbstractConfigGacela');
48
            }
49
50
            /** @psalm-suppress ArgumentTypeCoercion */
51 4
            return GacelaConfigFile::fromArray([
52 4
                'config' => $configGacelaClass->config(),
53 4
                'mapping-interfaces' => $configGacelaClass->mappingInterfaces(),
54
            ]);
55
        }
56
57 1
        if (is_array($configGacela)) {
58 1
            trigger_error('Use a function that returns an anonymous class that extends AbstractConfigGacela. Check the documentation for more info. Array is deprecated.', E_USER_DEPRECATED);
59
            /** @psalm-suppress MixedArgumentTypeCoercion */
60
            return GacelaConfigFile::fromArray($configGacela);
61
        }
62
63
        throw new RuntimeException('Create a function that returns an anonymous class that extends AbstractConfigGacela');
64
    }
65
}
66