Passed
Pull Request — master (#278)
by Asmir
03:32
created

Configuration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 96.23%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 106
c 2
b 0
f 0
dl 0
loc 149
ccs 102
cts 106
cp 0.9623
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 117 5
A getOrganizeMigrationsModes() 0 17 3
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 array_filter;
11
use function array_keys;
12
use function constant;
13
use function count;
14
use function in_array;
15
use function is_string;
16
use function method_exists;
17
use function strlen;
18
use function strpos;
19
use function strtoupper;
20
use function substr;
21
22
/**
23
 * DoctrineMigrationsExtension configuration structure.
24
 */
25
class Configuration implements ConfigurationInterface
26
{
27
    /**
28
     * Generates the configuration tree.
29
     *
30
     * @return TreeBuilder The config tree builder
31
     */
32 21
    public function getConfigTreeBuilder() : TreeBuilder
33
    {
34 21
        $treeBuilder = new TreeBuilder('doctrine_migrations');
35
36 21
        if (method_exists($treeBuilder, 'getRootNode')) {
37 21
            $rootNode = $treeBuilder->getRootNode();
38
        } else {
39
            // BC layer for symfony/config 4.1 and older
40
            $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

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