Completed
Push — master ( 2b894b...836484 )
by Gaetano
05:34
created

StatusCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Kaliop\eZMigrationBundle\API\Value\Migration;
9
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
10
use Symfony\Component\Console\Helper\Table;
11
12
/**
13
 * Command to display the status of migrations.
14
 *
15
 * @todo add option to skip displaying already executed migrations
16
 */
17
class StatusCommand extends AbstractCommand
18
{
19
    const STATUS_INVALID = -1;
20
21
    protected function configure()
22
    {
23
        $this->setName('kaliop:migration:status')
24
            ->setDescription('View the status of all (or a set of) migrations.')
25
            ->addOption('path', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, "The directory or file to load the migration definitions from")
26
            ->addOption('summary', null, InputOption::VALUE_NONE, "Only print summary information")
27
            ->setHelp(<<<EOT
28
The <info>kaliop:migration:status</info> command displays the status of all available migrations:
29
30
    <info>./ezpublish/console kaliop:migration:status</info>
31
32
You can optionally specify the path to migration versions with <info>--path</info>:
33
34
    <info>./ezpublish/console kaliop:migrations:status --path=/path/to/bundle/version_directory --path=/path/to/bundle/version_directory/single_migration_file</info>
35
EOT
36
            );
37
    }
38
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $migrationsService = $this->getMigrationService();
42
43
        $migrationDefinitions = $migrationsService->getMigrationsDefinitions($input->getOption('path'));
44
        $migrations = $migrationsService->getMigrations();
45
46
        if (!count($migrationDefinitions) && !count($migrations)) {
47
            $output->writeln('<info>No migrations found</info>');
48
            return;
49
        }
50
51
        // create a unique ist of all migrations (coming from db) and definitions (coming from disk)
52
        $index = array();
53
        foreach ($migrationDefinitions as $migrationDefinition) {
54
            $index[$migrationDefinition->name] = array('definition' => $migrationDefinition);
55
        }
56
        foreach ($migrations as $migration) {
57
            if (isset($index[$migration->name])) {
58
                $index[$migration->name]['migration'] = $migration;
59
            } else {
60
                $index[$migration->name] = array('migration' => $migration);
61
62
                // no definition, but a migration is there. Check if the definition sits elsewhere on disk than we expect it to be...
63
                // q: what if we have a loader which does not work with is_file? Could we remove this check?
64
                if ($migration->path != '' && is_file($migration->path)) {
65
                    try {
66
                        $migrationDefinitionCollection = $migrationsService->getMigrationsDefinitions(array($migration->path));
67
                        if (count($migrationDefinitionCollection)) {
68
                            $index[$migration->name]['definition'] = reset($migrationDefinitionCollection);
69
                        }
70
                    } catch (\Exception $e) {
71
                        /// @todo one day we should be able to limit the kind of exceptions we have to catch here...
72
                    }
73
                }
74
            }
75
        }
76
        ksort($index);
77
78
        if (!$input->getOption('summary')) {
79
            if (count($index) > 50000) {
80
                $output->writeln("WARNING: printing the status table might take a while as it contains many rows. Please wait...");
81
            }
82
            $output->writeln("\n <info>==</info> All Migrations\n");
83
        }
84
85
        $summary = array(
86
            self::STATUS_INVALID => array('Invalid', 0),
87
            Migration::STATUS_TODO => array('To do', 0),
88
            Migration::STATUS_STARTED => array('Started', 0),
89
            Migration::STATUS_DONE => array('Done', 0),
90
            Migration::STATUS_SUSPENDED => array('Suspended', 0),
91
            Migration::STATUS_FAILED => array('Failed', 0),
92
            Migration::STATUS_SKIPPED => array('Skipped', 0),
93
            Migration::STATUS_PARTIALLY_DONE => array('Partially done', 0),
94
        );
95
        $data = array();
96
97
        $i = 1;
98
        foreach ($index as $name => $value) {
99
            if (!isset($value['migration'])) {
100
                $migrationDefinition = $migrationsService->parseMigrationDefinition($value['definition']);
101
                $notes = '';
102
                if ($migrationDefinition->status != MigrationDefinition::STATUS_PARSED) {
103
                    $notes = '<error>' . $migrationDefinition->parsingError . '</error>';
104
                    $summary[self::STATUS_INVALID][1]++;
105
                } else {
106
                    $summary[Migration::STATUS_TODO][1]++;
107
                }
108
                $data[] = array(
109
                    $i++,
110
                    $name,
111
                    '<error>not executed</error>',
112
                    '',
113
                    $notes
114
                );
115
            } else {
116
                $migration = $value['migration'];
117
118
                if (!isset($summary[$migration->status])) {
119
                    $summary[$migration->status] = array($migration->status, 0);
120
                }
121
                $summary[$migration->status][1]++;
122
                if ($input->getOption('summary')) {
123
                    continue;
124
                }
125
126 View Code Duplication
                switch ($migration->status) {
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...
127
                    case Migration::STATUS_DONE:
128
                        $status = '<info>executed</info>';
129
                        break;
130
                    case Migration::STATUS_STARTED:
131
                        $status = '<comment>execution started</comment>';
132
                        break;
133
                    case Migration::STATUS_TODO:
134
                        // bold to-migrate!
135
                        $status = '<error>not executed</error>';
136
                        break;
137
                    case Migration::STATUS_SKIPPED:
138
                        $status = '<comment>skipped</comment>';
139
                        break;
140
                    case Migration::STATUS_PARTIALLY_DONE:
141
                        $status = '<comment>partially executed</comment>';
142
                        break;
143
                    case Migration::STATUS_SUSPENDED:
144
                        $status = '<comment>suspended</comment>';
145
                        break;
146
                    case Migration::STATUS_FAILED:
147
                        $status = '<error>failed</error>';
148
                        break;
149
                }
150
                $notes = array();
151
                if ($migration->executionError != '') {
152
                    $notes[] = "<error>{$migration->executionError}</error>";
153
                }
154
                if (!isset($value['definition'])) {
155
                    $notes[] = '<comment>The migration definition file can not be found any more</comment>';
156
                } else {
157
                    $migrationDefinition = $value['definition'];
158
                    if (md5($migrationDefinition->rawDefinition) != $migration->md5) {
159
                        $notes[] = '<comment>The migration definition file has now a different checksum</comment>';
160
                    }
161
                    if ($migrationDefinition->path != $migrationDefinition->path) {
162
                        $notes[] = '<comment>The migration definition file has now moved</comment>';
163
                    }
164
                }
165
                $notes = implode(' ', $notes);
166
                $data[] = array(
167
                    $i++,
168
                    $migration->name,
169
                    $status,
0 ignored issues
show
Bug introduced by
The variable $status does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
170
                    ($migration->executionDate != null ? date("Y-m-d H:i:s", $migration->executionDate) : ''),
171
                    $notes
172
                );
173
            }
174
        }
175
176
        if ($input->getOption('summary')) {
177
            $output->writeln("\n <info>==</info> Migrations Summary\n");
178
            // do not print info about the not yet supported case
179
            unset($summary[Migration::STATUS_PARTIALLY_DONE]);
180
            $data = $summary;
181
            $headers = array('Status', 'Count');
182
        } else {
183
            $headers = array('#', 'Migration', 'Status', 'Executed on', 'Notes');
184
        }
185
186
        $table = new Table($output);
187
        $table
188
            ->setHeaders($headers)
189
            ->setRows($data);
190
        $table->render();
191
    }
192
}
193