Completed
Push — master ( ab7ebf...740609 )
by Grégoire
02:23
created

ConfigurationArray::getConfiguration()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 26
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 42
ccs 25
cts 25
cp 1
crap 6
rs 8.8817
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Configuration;
6
7
use Closure;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Configuration\Configuration\Exception\InvalidConfigurationKey;
10
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
11
use Doctrine\Migrations\Tools\BooleanStringFormatter;
12
use function assert;
13
use function call_user_func;
14
use function is_array;
15
use function is_bool;
16
use function is_callable;
17
18
final class ConfigurationArray implements ConfigurationLoader
19
{
20
    /** @var array<string,mixed> */
21
    private $configurations;
22
23
    /**
24
     * @param array<string,mixed> $configurations
25
     */
26 33
    public function __construct(array $configurations)
27
    {
28 33
        $this->configurations = $configurations;
29 33
    }
30
31 33
    public function getConfiguration() : Configuration
32
    {
33
        $configMap = [
34
            'migrations_paths' => static function ($paths, Configuration $configuration) : void {
35 14
                foreach ($paths as $namespace => $path) {
36 14
                    $configuration->addMigrationsDirectory($namespace, $path);
37
                }
38 33
            },
39
            'migrations' => static function ($migrations, Configuration $configuration) : void {
40 9
                foreach ($migrations as $className) {
41 9
                    $configuration->addMigrationClass($className);
42
                }
43 33
            },
44
            'table_storage' => [
45 33
                'table_name' => 'setTableName',
46 33
                'version_column_name' => 'setVersionColumnName',
47
                'version_column_length' => static function ($value, TableMetadataStorageConfiguration $configuration) : void {
48 9
                    $configuration->setVersionColumnLength((int) $value);
49 33
                },
50 33
                'executed_at_column_name' => 'setExecutedAtColumnName',
51 33
                'execution_time_column_name' => 'setExecutionTimeColumnName',
52
            ],
53
54 33
            'organize_migrations' => 'setMigrationOrganization',
55 33
            'name' => 'setName',
56 33
            'custom_template' => 'setCustomTemplate',
57
            'all_or_nothing' => static function ($value, Configuration $configuration) : void {
58 9
                $configuration->setAllOrNothing(is_bool($value) ? $value : BooleanStringFormatter::toBoolean($value, false));
59 33
            },
60
            'check_database_platform' =>  static function ($value, Configuration $configuration) : void {
61 7
                $configuration->setCheckDatabasePlatform(is_bool($value) ? $value :BooleanStringFormatter::toBoolean($value, false));
62 33
            },
63
        ];
64
65 33
        $object = new Configuration();
66 33
        self::applyConfigs($configMap, $object, $this->configurations);
67
68 26
        if ($object->getMetadataStorageConfiguration() === null) {
69 17
            $object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
70
        }
71
72 26
        return $object;
73
    }
74
75
    /**
76
     * @param mixed[]                                         $configMap
77
     * @param Configuration|TableMetadataStorageConfiguration $object
78
     * @param array<string|int,mixed>                         $data
79
     */
80 33
    private static function applyConfigs(array $configMap, $object, array $data) : void
81
    {
82 33
        foreach ($data as $configurationKey => $configurationValue) {
83 33
            if (! isset($configMap[$configurationKey])) {
84 4
                throw InvalidConfigurationKey::new((string) $configurationKey);
85
            }
86
87 29
            if (is_array($configMap[$configurationKey])) {
88 9
                if ($configurationKey !== 'table_storage') {
89
                    throw InvalidConfigurationKey::new((string) $configurationKey);
90
                }
91
92 9
                $storageConfig = new TableMetadataStorageConfiguration();
93 9
                assert($object instanceof Configuration);
94 9
                $object->setMetadataStorageConfiguration($storageConfig);
95 9
                self::applyConfigs($configMap[$configurationKey], $storageConfig, $configurationValue);
96
            } else {
97 29
                $callable = $configMap[$configurationKey] instanceof Closure
98 14
                    ? $configMap[$configurationKey]
99 29
                    : [$object, $configMap[$configurationKey]];
100 29
                assert(is_callable($callable));
101
                call_user_func(
102 29
                    $callable,
103
                    $configurationValue,
104
                    $object,
105
                    $data
106
                );
107
            }
108
        }
109 26
    }
110
}
111