Completed
Pull Request — master (#91)
by
unknown
06:19
created

MigrationCommand::execute()   C

Complexity

Conditions 16
Paths 22

Size

Total Lines 75
Code Lines 39

Duplication

Lines 12
Ratio 16 %

Code Coverage

Tests 23
CRAP Score 48

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 75
ccs 23
cts 46
cp 0.5
rs 5.3027
cc 16
eloc 39
nc 22
nop 2
crap 48

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
namespace Kaliop\eZMigrationBundle\Command;
4
use Symfony\Component\Console\Input\InputInterface;
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
9
/**
10
 * Command to execute the available migration definitions.
11
 */
12
class MigrationCommand extends AbstractCommand
13
{
14
    /**
15
     * Set up the command.
16
     *
17
     * Define the name, options and help text.
18
     */
19 27
    protected function configure()
20
    {
21 27
        parent::configure();
22
23 27
        $this
24 27
            ->setName('kaliop:migration:migration')
25 27
            ->setDescription('Manually delete migrations from the database table.')
26 27
            ->addOption('delete', null, InputOption::VALUE_NONE, "Delete the specified migration.")
27 27
            ->addOption('add', null, InputOption::VALUE_NONE, "Add the specified migration definition.")
28 27
            ->addOption('skip', null, InputOption::VALUE_NONE, "Mark the specified migration as skipped.")
29 27
            ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question.")
30 27
            ->addArgument('migration', InputArgument::REQUIRED, 'The version to add/skip (filename with full path) or delete (plain migration name).', null)
31 27
            ->setHelp(
32
                <<<EOT
33
The <info>kaliop:migration:migration</info> command allows you to manually delete migrations versions from the migration table:
34
35
    <info>./ezpublish/console kaliop:migration:migration --delete migration_name</info>
36
37
As well as manually adding migrations to the migration table:
38
39
    <info>./ezpublish/console kaliop:migration:migration --add /path/to/migration_definition</info>
40
EOT
41 27
            );
42 27
    }
43
44
    /**
45
     * Execute the command.
46
     *
47
     * @param InputInterface $input
48
     * @param OutputInterface $output
49
     * @return null|int null or 0 if everything went fine, or an error code
50
     */
51 26
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 26
        if (! $input->getOption('add') && ! $input->getOption('delete') && ! $input->getOption('skip')) {
54
            throw new \InvalidArgumentException('You must specify whether you want to --add, --delete or --skip the specified migration.');
55
        }
56
57 26
        $migrationService = $this->getMigrationService();
58 26
        $migrationNameOrPath = $input->getArgument('migration');
59
60
        // ask user for confirmation to make changes
61 26 View Code Duplication
        if ($input->isInteractive() && !$input->getOption('no-interaction')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $dialog = $this->getHelperSet()->get('dialog');
63
            if (!$dialog->askConfirmation(
64
                $output,
65
                '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>',
66
                false
67
            )
68
            ) {
69
                $output->writeln('<error>Migration change cancelled!</error>');
70
                return 0;
71
            }
72
        }
73
74 26
        if ($input->getOption('add')) {
75
            // will throw if a file is passed and it is not found, but not if an empty dir is passed
76 26
            $migrationDefinitionCollection = $migrationService->getMigrationsDefinitions(array($migrationNameOrPath));
77
78 26
            if (!count($migrationDefinitionCollection))
79 26
            {
80
                throw new \InvalidArgumentException(sprintf('The path "%s" does not correspond to any migration definition.', $migrationNameOrPath));
81
            }
82
83 26
            foreach($migrationDefinitionCollection as $migrationDefinition) {
84 26
                $migrationName = basename($migrationDefinition->path);
85
86 26
                $migration = $migrationService->getMigration($migrationNameOrPath);
87 26
                if ($migration != null) {
88
                    throw new \InvalidArgumentException(sprintf('The migration "%s" does already exist in the migrations table.', $migrationName));
89
                }
90
91 26
                $migrationService->addMigration($migrationDefinition);
92 26
                $output->writeln('<info>Added migration' . $migrationDefinition->path . '</info>');
93 26
            }
94
95 26
            return;
96
        }
97
98 26
        if ($input->getOption('delete')) {
99 26
            $migration = $migrationService->getMigration($migrationNameOrPath);
100 26
            if ($migration == null) {
101 26
                throw new \InvalidArgumentException(sprintf('The migration "%s" does not exist in the migrations table.', $migrationNameOrPath));
102
            }
103
104 26
            $migrationService->deleteMigration($migration);
105
106 26
            return;
107
        }
108
109
        if ($input->getOption('skip')) {
110
            // will throw if a file is passed and it is not found, but not if an empty dir is passed
111
            $migrationDefinitionCollection = $migrationService->getMigrationsDefinitions(array($migrationNameOrPath));
112
113
            if (!count($migrationDefinitionCollection))
114
            {
115
                throw new \InvalidArgumentException(sprintf('The path "%s" does not correspond to any migration definition.', $migrationNameOrPath));
116
            }
117
118
            foreach($migrationDefinitionCollection as $migrationDefinition) {
119
                $migrationService->skipMigration($migrationDefinition);
120
                $output->writeln('<info>Migration' . $migrationDefinition->path . ' marked as skipped</info>');
121
            }
122
123
            return;
124
        }
125
    }
126
}
127