Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

ResumeCommand::execute()   C

Complexity

Conditions 11
Paths 22

Size

Total Lines 69
Code Lines 43

Duplication

Lines 12
Ratio 17.39 %

Importance

Changes 0
Metric Value
dl 12
loc 69
c 0
b 0
f 0
rs 5.7847
cc 11
eloc 43
nc 22
nop 2

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
5
use Kaliop\eZMigrationBundle\API\Value\Migration;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\InputArgument;
10
11
/**
12
 * Command to resume suspended migrations.
13
 *
14
 * @todo add support for resuming a set based on path
15
 * @todo add support for the separate-process cli switch
16
 */
17
class ResumeCommand extends AbstractCommand
18
{
19
    /**
20
     * Set up the command.
21
     *
22
     * Define the name, options and help text.
23
     */
24
    protected function configure()
25
    {
26
        parent::configure();
27
28
        $this
29
            ->setName('kaliop:migration:resume')
30
            ->setDescription('Restarts any suspended migrations.')
31
            ->addOption('ignore-failures', 'i', InputOption::VALUE_NONE, "Keep resuming migrations even if one fails")
32
            ->addOption('no-interaction', 'n', InputOption::VALUE_NONE, "Do not ask any interactive question.")
33
            ->addOption('no-transactions', 'u', InputOption::VALUE_NONE, "Do not use a repository transaction to wrap each migration. Unsafe, but needed for legacy slot handlers")
34
            ->addOption('migration', 'm', InputOption::VALUE_REQUIRED, 'A single migration to resume (plain migration name).', null)
35
            ->setHelp(<<<EOT
36
The <info>kaliop:migration:resume</info> command allows you to resume any suspended migration
37
EOT
38
            );
39
    }
40
41
    /**
42
     * Execute the command.
43
     *
44
     * @param InputInterface $input
45
     * @param OutputInterface $output
46
     * @return null|int null or 0 if everything went fine, or an error code
47
     * @throws \Exception
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $start = microtime(true);
52
53
        $this->getContainer()->get('ez_migration_bundle.step_executed_listener.tracing')->setOutput($output);
54
55
        $migrationService = $this->getMigrationService();
56
57
        $migrationName = $input->getOption('migration');
58
        if ($migrationName != null) {
59
            $suspendedMigration = $migrationService->getMigration($migrationName);
60
            if (!$suspendedMigration) {
61
                throw new \Exception("Migration '$migrationName' not found");
62
            }
63
            if ($suspendedMigration->status != Migration::STATUS_SUSPENDED) {
64
                throw new \Exception("Migration '$migrationName' is not suspended, can not resume it");
65
            }
66
67
            $suspendedMigrations = array($suspendedMigration);
68
        } else {
69
            $suspendedMigrations = $migrationService->getMigrationsByStatus(Migration::STATUS_SUSPENDED);
70
        };
71
72
        $output->writeln('<info>Found ' . count($suspendedMigrations) . ' suspended migrations</info>');
73
74
        if (!count($suspendedMigrations)) {
75
            $output->writeln('Nothing to do');
76
            return;
77
        }
78
79
        // ask user for confirmation to make changes
80 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...
81
            $dialog = $this->getHelperSet()->get('dialog');
82
            if (!$dialog->askConfirmation(
83
                $output,
84
                '<question>Careful, the database will be modified. Do you want to continue Y/N ?</question>',
85
                false
86
            )
87
            ) {
88
                $output->writeln('<error>Migration resuming cancelled!</error>');
89
                return 0;
90
            }
91
        }
92
93
        $executed = 0;
94
        $failed = 0;
95
96
        foreach($suspendedMigrations as $suspendedMigration) {
97
            $output->writeln("<info>Resuming {$suspendedMigration->name}</info>");
98
99
            try {
100
                $migrationService->resumeMigration($suspendedMigration, !$input->getOption('no-transactions'));
101
102
                $executed++;
103
            } catch (\Exception $e) {
104
                if ($input->getOption('ignore-failures')) {
105
                    $output->writeln("\n<error>Migration failed! Reason: " . $e->getMessage() . "</error>\n");
106
                    $failed++;
107
                    continue;
108
                }
109
                $output->writeln("\n<error>Migration aborted! Reason: " . $e->getMessage() . "</error>");
110
                return 1;
111
            }
112
        }
113
114
        $time = microtime(true) - $start;
115
        $output->writeln("Resumed $executed migrations, failed $failed");
116
        $output->writeln("Time taken: ".sprintf('%.2f', $time)." secs, memory: ".sprintf('%.2f', (memory_get_peak_usage(true) / 1000000)). ' MB');
117
    }
118
}
119