Completed
Pull Request — master (#278)
by Asmir
22:58 queued 15:13
created

DoctrineMigrationsExtension::load()   F

Complexity

Conditions 19
Paths > 20000

Size

Total Lines 81
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 48
c 1
b 0
f 1
dl 0
loc 81
ccs 0
cts 49
cp 0
rs 0.3499
cc 19
nc 33792
nop 2
crap 380

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
6
7
use Doctrine\Migrations\Configuration\Configuration as MigrationsConfiguration;
8
use Doctrine\Migrations\DependencyFactory;
9
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
16
use function sprintf;
17
18
/**
19
 * DoctrineMigrationsExtension.
20
 */
21
class DoctrineMigrationsExtension extends Extension
22
{
23
    /**
24
     * Responds to the migrations configuration parameter.
25
     *
26
     * @param string[][] $configs
27
     */
28
    public function load(array $configs, ContainerBuilder $container) : void
29
    {
30
        $configuration = new Configuration();
31
32
        $config = $this->processConfiguration($configuration, $configs);
33
34
        foreach ($config as $key => $value) {
35
            $container->setParameter($this->getAlias() . '.' . $key, $value);
36
        }
37
38
        $configurationDefinition = new Definition(MigrationsConfiguration::class);
39
        $container->setDefinition('doctrine.migrations.configuration', $configurationDefinition);
40
41
        if (! empty($config['name'])) {
42
            $configurationDefinition->addMethodCall('setName', [$config['name']]);
43
        }
44
        if (! empty($config['migrations_paths'])) {
45
            foreach ($config['migrations_paths'] as $ns => $path) {
46
                $configurationDefinition->addMethodCall('addMigrationsDirectory', [$ns, $path]);
47
            }
48
        }
49
        if (! empty($config['all_or_nothing'])) {
50
            $configurationDefinition->addMethodCall('setAllOrNothing', [$config['all_or_nothing']]);
51
        }
52
        if (! empty($config['custom_template'])) {
53
            $configurationDefinition->setCustomTemplate('setName', [$config['custom_template']]);
0 ignored issues
show
Bug introduced by
The method setCustomTemplate() does not exist on Symfony\Component\DependencyInjection\Definition. ( Ignorable by Annotation )

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

53
            $configurationDefinition->/** @scrutinizer ignore-call */ 
54
                                      setCustomTemplate('setName', [$config['custom_template']]);

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...
54
        }
55
        if (! empty($config['organize_migrations'])) {
56
            $configurationDefinition->addMethodCall('setMigrationOrganization', [$config['organize_migrations']]);
57
        }
58
59
        if (! empty($config['storage']['table_storage'])) {
60
            $sConf = $config['storage']['table_storage'];
61
62
            $storageDefinition = new Definition(TableMetadataStorageConfiguration::class);
63
            $container->setDefinition('doctrine.migrations.storage.table_storage', $storageDefinition);
64
            $container->setAlias('doctrine.migrations.metadata_storage', 'doctrine.migrations.storage.table_storage');
65
66
            if (! empty($sConf['table_name'])) {
67
                $configurationDefinition->addMethodCall('setTableName', [$sConf['table_name']]);
68
            }
69
            if (! empty($sConf['version_column_name'])) {
70
                $configurationDefinition->addMethodCall('setVersionColumnName', [$sConf['version_column_name']]);
71
            }
72
            if (! empty($sConf['version_column_length'])) {
73
                $configurationDefinition->addMethodCall('setVersionColumnLength', [$sConf['version_column_length']]);
74
            }
75
            if (! empty($sConf['executed_at_column_name'])) {
76
                $configurationDefinition->addMethodCall('setExecutedAtColumnName', [$sConf['executed_at_column_name']]);
77
            }
78
            if (! empty($sConf['execution_time_column_name'])) {
79
                $configurationDefinition->addMethodCall('setExecutionTimeColumnName', [$sConf['execution_time_column_name']]);
80
            }
81
82
            $configurationDefinition->addMethodCall('setMetadataStorageConfiguration', [new Reference('doctrine.migrations.storage.table_storage')]);
83
        }
84
85
        $dbalConnection = sprintf('doctrine.dbal.%s_connection', $config['connection']);
86
        $emID           = $config['em'] !== null && $config['em'] !== false ? sprintf('@doctrine.orm.%s_entity_manager', $config['em']) : null;
87
88
        $diDefinition = new Definition(DependencyFactory::class, [
89
            new Reference('doctrine.migrations.configuration'),
90
            new Reference($dbalConnection),
91
            $emID !== null ? new Reference($emID) : null,
92
            new Reference('logger'),
93
        ]);
94
95
        $container->setDefinition('doctrine.migrations.di', $diDefinition);
96
97
        if (! empty($config['all_or_nothing'])) {
98
            $configurationDefinition->addMethodCall('setAllOrNothing', [$config['all_or_nothing']]);
99
        }
100
101
        if (! empty($config['sorter'])) {
102
            $configurationDefinition->addMethodCall('setSorter', [new Reference($config['sorter'])]);
103
        }
104
105
        $locator = new FileLocator(__DIR__ . '/../Resources/config/');
106
        $loader  = new XmlFileLoader($container, $locator);
107
108
        $loader->load('services.xml');
109
    }
110
111
    /**
112
     * Returns the base path for the XSD files.
113
     *
114
     * @return string The XSD base path
115
     */
116
    public function getXsdValidationBasePath() : string
117
    {
118
        return __DIR__ . '/../Resources/config/schema';
119
    }
120
121
    public function getNamespace() : string
122
    {
123
        return 'http://symfony.com/schema/dic/doctrine/migrations';
124
    }
125
}
126