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

Configuration   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 96.23%

Importance

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

2 Methods

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