Failed Conditions
Pull Request — master (#933)
by Asmir
03:12
created

ConfigurationArray   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 87
ccs 42
cts 46
cp 0.913
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B getConfiguration() 0 41 6
B applyConfigs() 0 26 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration\Migration;
6
7
use Closure;
8
use Doctrine\Migrations\Configuration\Configuration;
9
use Doctrine\Migrations\Configuration\Migration\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
            'custom_template' => 'setCustomTemplate',
56
            'all_or_nothing' => static function ($value, Configuration $configuration) : void {
57 9
                $configuration->setAllOrNothing(is_bool($value) ? $value : BooleanStringFormatter::toBoolean($value, false));
58 33
            },
59
            'check_database_platform' =>  static function ($value, Configuration $configuration) : void {
60 7
                $configuration->setCheckDatabasePlatform(is_bool($value) ? $value :BooleanStringFormatter::toBoolean($value, false));
61 33
            },
62
        ];
63
64 33
        $object = new Configuration();
65 33
        self::applyConfigs($configMap, $object, $this->configurations);
66
67 26
        if ($object->getMetadataStorageConfiguration() === null) {
68 17
            $object->setMetadataStorageConfiguration(new TableMetadataStorageConfiguration());
69
        }
70
71 26
        return $object;
72
    }
73
74
    /**
75
     * @param mixed[]                                         $configMap
76
     * @param Configuration|TableMetadataStorageConfiguration $object
77
     * @param array<string|int,mixed>                         $data
78
     */
79 33
    private static function applyConfigs(array $configMap, $object, array $data) : void
80
    {
81 33
        foreach ($data as $configurationKey => $configurationValue) {
82 33
            if (! isset($configMap[$configurationKey])) {
83 4
                throw InvalidConfigurationKey::new((string) $configurationKey);
84
            }
85
86 29
            if (is_array($configMap[$configurationKey])) {
87 9
                if ($configurationKey !== 'table_storage') {
88
                    throw InvalidConfigurationKey::new((string) $configurationKey);
89
                }
90
91 9
                $storageConfig = new TableMetadataStorageConfiguration();
92 9
                assert($object instanceof Configuration);
93 9
                $object->setMetadataStorageConfiguration($storageConfig);
94 9
                self::applyConfigs($configMap[$configurationKey], $storageConfig, $configurationValue);
95
            } else {
96 29
                $callable = $configMap[$configurationKey] instanceof Closure
97 14
                    ? $configMap[$configurationKey]
98 29
                    : [$object, $configMap[$configurationKey]];
99 29
                assert(is_callable($callable));
100
                call_user_func(
101 29
                    $callable,
102
                    $configurationValue,
103
                    $object,
104
                    $data
105
                );
106
            }
107
        }
108 26
    }
109
}
110