Completed
Pull Request — master (#228)
by Jonathan
05:08 queued 02:30
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 4.0035

Importance

Changes 0
Metric Value
cc 4
eloc 33
nc 1
nop 0
dl 0
loc 45
rs 8.5806
c 0
b 0
f 0
ccs 31
cts 33
cp 0.9394
crap 4.0035
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
use function constant;
10
use function in_array;
11
use function is_string;
12
use function strlen;
13
use function strpos;
14
use function strtoupper;
15
use function substr;
16
17
/**
18
 * DoctrineMigrationsExtension configuration structure.
19
 */
20
class Configuration implements ConfigurationInterface
21
{
22
    /**
23
     * Generates the configuration tree.
24
     *
25
     * @return TreeBuilder The config tree builder
26
     */
27 1
    public function getConfigTreeBuilder() : TreeBuilder
28
    {
29 1
        $treeBuilder = new TreeBuilder();
30 1
        $rootNode    = $treeBuilder->root('doctrine_migrations', 'array');
31
32 1
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
33
34
        $rootNode
35 1
            ->children()
36 1
                ->scalarNode('dir_name')->defaultValue('%kernel.root_dir%/DoctrineMigrations')->cannotBeEmpty()->end()
37 1
                ->scalarNode('namespace')->defaultValue('Application\Migrations')->cannotBeEmpty()->end()
38 1
                ->scalarNode('table_name')->defaultValue('migration_versions')->cannotBeEmpty()->end()
39 1
                ->scalarNode('column_name')->defaultValue('version')->end()
40 1
                ->scalarNode('column_length')->defaultValue(255)->end()
41 1
                ->scalarNode('executed_at_column_name')->defaultValue('executed_at')->end()
42 1
                ->scalarNode('all_or_nothing')->defaultValue(false)->end()
43 1
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
44 1
                ->scalarNode('custom_template')->defaultValue(null)->end()
45 1
                ->scalarNode('organize_migrations')->defaultValue(false)
46 1
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
47 1
                    ->validate()
48
                        ->ifTrue(function ($v) use ($organizeMigrationModes) {
49 1
                            if ($v === false) {
50
                                return false;
51
                            }
52
53 1
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes)) {
54 1
                                return false;
55
                            }
56
57
                            return true;
58 1
                        })
59 1
                        ->thenInvalid('Invalid organize migrations mode value %s')
60 1
                    ->end()
61 1
                    ->validate()
62 1
                        ->ifString()
63
                            ->then(function ($v) {
64 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
65 1
                            })
66 1
                        ->end()
67 1
                    ->end()
68 1
            ->end()
69
        ;
70
71 1
        return $treeBuilder;
72
    }
73
74
75
    /**
76
     * Find organize migrations modes for their names
77
     *
78
     * @return string[]
79
     */
80 1
    private function getOrganizeMigrationsModes() : array
81
    {
82 1
        $constPrefix = 'VERSIONS_ORGANIZATION_';
83 1
        $prefixLen   = strlen($constPrefix);
84 1
        $refClass    = new \ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
85 1
        $constsArray = $refClass->getConstants();
86 1
        $namesArray  = [];
87
88 1
        foreach ($constsArray as $key => $value) {
89 1
            if (strpos($key, $constPrefix) !== 0) {
90 1
                continue;
91
            }
92
93 1
            $namesArray[] = substr($key, $prefixLen);
94
        }
95
96 1
        return $namesArray;
97
    }
98
}
99