Completed
Pull Request — master (#278)
by Asmir
17:19 queued 14:39
created

Configuration::getOrganizeMigrationsModes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
rs 9.9332
cc 3
nc 3
nop 0
crap 3.0067
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
6
7
use ReflectionClass;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
use function constant;
11
use function count;
12
use function in_array;
13
use function is_string;
14
use function method_exists;
15
use function strlen;
16
use function strpos;
17
use function strtoupper;
18
use function substr;
19
20
/**
21
 * DoctrineMigrationsExtension configuration structure.
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * Generates the configuration tree.
27
     *
28
     * @return TreeBuilder The config tree builder
29
     */
30 6
    public function getConfigTreeBuilder() : TreeBuilder
31
    {
32 6
        $treeBuilder = new TreeBuilder('doctrine_migrations');
33
34 6
        if (method_exists($treeBuilder, 'getRootNode')) {
35 6
            $rootNode = $treeBuilder->getRootNode();
36
        } else {
37
            // BC layer for symfony/config 4.1 and older
38
            $rootNode = $treeBuilder->root('doctrine_migrations', 'array');
0 ignored issues
show
Bug introduced by
The method root() does not exist on Symfony\Component\Config...ion\Builder\TreeBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            /** @scrutinizer ignore-call */ 
39
            $rootNode = $treeBuilder->root('doctrine_migrations', 'array');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        }
40
41 6
        $organizeMigrationModes = $this->getOrganizeMigrationsModes();
42
43
        $rootNode
44 6
            ->children()
45 6
                ->scalarNode('name')->defaultValue('Application Migrations')->end()
46 6
                ->arrayNode('migrations_paths')
47
48 6
                    ->info('A list of pairs namespace/path where to look for migrations.')
49 6
                    ->requiresAtLeastOneElement()
50 6
                    ->useAttributeAsKey('name')
51 6
                    ->defaultValue(['%kernel.root_dir%/DoctrineMigrations' => 'Application\Migrations'])
52 6
                    ->prototype('scalar')->end()
53 6
                    ->validate()
54
                        ->ifTrue(static function ($v) {
55 1
                            return count($v) === 0;
56 6
                        })
57 6
                        ->thenInvalid('At least one migrations path must be specified.')
58 6
                    ->end()
59 6
                 ->end()
60
61 6
                ->arrayNode('storage')
62 6
                    ->addDefaultsIfNotSet()
63 6
                    ->info('Storage to use for migration status metadata.')
64 6
                    ->children()
65 6
                        ->scalarNode('id')
66 6
                            ->info('Custom metadata storage service ID.')
67 6
                            ->defaultValue(null)
68 6
                        ->end()
69 6
                        ->arrayNode('table_storage')
70 6
                            ->addDefaultsIfNotSet()
71 6
                            ->info('The default metadata storage, implemented as table in the database.')
72 6
                            ->children()
73 6
                                ->scalarNode('table_name')->defaultValue(null)->cannotBeEmpty()->end()
74 6
                                ->scalarNode('version_column_name')->defaultValue(null)->end()
75 6
                                ->scalarNode('version_column_length')->defaultValue(null)->end()
76 6
                                ->scalarNode('executed_at_column_name')->defaultValue(null)->end()
77 6
                                ->scalarNode('execution_time_column_name')->defaultValue(null)->end()
78 6
                            ->end()
79 6
                        ->end()
80 6
                    ->end()
81
82 6
                ->end()
83 6
                ->arrayNode('migrations')
84 6
                    ->info('A list of migrations to load in addition the the one discovered via "migrations_paths".')
85 6
                    ->prototype('scalar')->end()
86 6
                    ->defaultValue([])
87 6
                ->end()
88 6
                ->scalarNode('sorter')
89 6
                    ->info('Alternative migrations sorting algorithm')
90 6
                    ->defaultValue(null)
91 6
                ->end()
92 6
                ->scalarNode('connection')
93 6
                    ->info('Connection name to use for the migrations database.')
94 6
                    ->defaultValue(null)
95 6
                ->end()
96 6
                ->scalarNode('em')
97 6
                    ->info('Entity manager name to use for the migrations database (available when doctrine/orm is installed).')
98 6
                    ->defaultValue(null)
99 6
                ->end()
100 6
                ->scalarNode('all_or_nothing')
101 6
                    ->info('Run all migrations in a transaction.')
102 6
                    ->defaultValue(false)
103 6
                ->end()
104 6
                ->scalarNode('check_database_platform')->defaultValue(true)->end()
105 6
                ->scalarNode('custom_template')
106 6
                    ->info('Custom template path for generated migration classes.')
107 6
                    ->defaultValue(null)
108 6
                ->end()
109 6
                ->scalarNode('organize_migrations')
110 6
                    ->defaultValue(false)
111 6
                    ->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
112 6
                    ->validate()
113
                        ->ifTrue(static function ($v) use ($organizeMigrationModes) {
114 1
                            if ($v === false) {
115
                                return false;
116
                            }
117
118 1
                            if (is_string($v) && in_array(strtoupper($v), $organizeMigrationModes, true)) {
119 1
                                return false;
120
                            }
121
122
                            return true;
123 6
                        })
124 6
                        ->thenInvalid('Invalid organize migrations mode value %s')
125 6
                    ->end()
126 6
                    ->validate()
127 6
                        ->ifString()
128
                            ->then(static function ($v) {
129 1
                                return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
130 6
                            })
131 6
                    ->end()
132 6
                ->end()
133 6
            ->end();
134
135 6
        return $treeBuilder;
136
    }
137
138
139
    /**
140
     * Find organize migrations modes for their names
141
     *
142
     * @return string[]
143
     */
144 6
    private function getOrganizeMigrationsModes() : array
145
    {
146 6
        $constPrefix = 'VERSIONS_ORGANIZATION_';
147 6
        $prefixLen   = strlen($constPrefix);
148 6
        $refClass    = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
149 6
        $constsArray = $refClass->getConstants();
150 6
        $namesArray  = [];
151
152 6
        foreach ($constsArray as $key => $value) {
153 6
            if (strpos($key, $constPrefix) !== 0) {
154
                continue;
155
            }
156
157 6
            $namesArray[] = substr($key, $prefixLen);
158
        }
159
160 6
        return $namesArray;
161
    }
162
}
163