Passed
Push — master ( 229462...d74785 )
by Andreas
02:20 queued 14s
created

ArrayLoader::load()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 7

Importance

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