Completed
Push — master ( 178c75...b82d68 )
by Luís
11s
created

StatusCommand::execute()   D

Complexity

Conditions 10
Paths 70

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 10.7998

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 16
cts 20
cp 0.8
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 19
nc 70
nop 2
crap 10.7998

How to fix   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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Migrations\Tools\Console\Command;
21
22
use Doctrine\DBAL\Migrations\Configuration\Configuration;
23
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
28
/**
29
 * Command to view the status of a set of migrations.
30
 *
31
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
32
 * @link    www.doctrine-project.org
33
 * @since   2.0
34
 * @author  Jonathan Wage <[email protected]>
35
 */
36
class StatusCommand extends AbstractCommand
37
{
38 11
    protected function configure()
39
    {
40
        $this
41 11
            ->setName('migrations:status')
42 11
            ->setDescription('View the status of a set of migrations.')
43 11
            ->addOption('show-versions', null, InputOption::VALUE_NONE, 'This will display a list of all available migrations and their status')
44 11
            ->setHelp(<<<EOT
45 11
The <info>%command.name%</info> command outputs the status of a set of migrations:
46
47
    <info>%command.full_name%</info>
48
49
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
50
51
    <info>%command.full_name% --show-versions</info>
52
EOT
53
        );
54
55 11
        parent::configure();
56 11
    }
57
58 9
    public function execute(InputInterface $input, OutputInterface $output)
59
    {
60 9
        $configuration = $this->getMigrationConfiguration($input, $output);
61
62 9
        $infos = new MigrationStatusInfosHelper($configuration);
63
64 9
        $output->writeln("\n <info>==</info> Configuration\n");
65 9
        foreach ($infos->getMigrationsInfos() as $name => $value) {
66 9
            if ($name == 'New Migrations') {
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...
67 9
                $value = $value > 0 ? '<question>' . $value . '</question>' : 0;
68
            }
69 9
            if($name == 'Executed Unavailable Migrations') {
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...
70 9
                $value = $value > 0 ? '<error>' . $value . '</error>' : 0;
71
            }
72 9
            $this->writeStatusInfosLineAligned($output, $name, $value);
73
        }
74
75 9
        if ($input->getOption('show-versions')) {
76 1
            if ($migrations = $configuration->getMigrations()) {
77 1
                $output->writeln("\n <info>==</info> Available Migration Versions\n");
78
79 1
                $this->showVersions($migrations, $configuration, $output);
80
            }
81
82 1
            if (count($infos->getExecutedUnavailableMigrations())) {
83
                $output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
84
                foreach ($infos->getExecutedUnavailableMigrations() as $executedUnavailableMigration) {
85
                    $output->writeln('    <comment>>></comment> ' . $configuration->getDateTime($executedUnavailableMigration) .
86
                        ' (<comment>' . $executedUnavailableMigration . '</comment>)');
87
                }
88
            }
89
        }
90 9
    }
91
92 9
    private function writeStatusInfosLineAligned(OutputInterface $output, $title, $value)
93
    {
94 9
        $output->writeln('    <comment>>></comment> ' . $title . ': ' . str_repeat(' ', 50 - strlen($title)) . $value);
95 9
    }
96
97 1
    private function showVersions($migrations, Configuration $configuration, OutputInterface $output)
98
    {
99 1
        $migratedVersions = $configuration->getMigratedVersions();
100
101 1
        foreach($migrations as $version) {
102 1
            $isMigrated = in_array($version->getVersion(), $migratedVersions);
103 1
            $status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
104
105 1
            $migrationDescription = $version->getMigration()->getDescription()
106
                ? str_repeat(' ', 5) . $version->getMigration()->getDescription()
107 1
                : '';
108
109 1
            $formattedVersion = $configuration->getDateTime($version->getVersion());
110
111 1
            $output->writeln('    <comment>>></comment> ' . $formattedVersion .
112 1
                ' (<comment>' . $version->getVersion() . '</comment>)' .
113 1
                str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($version->getVersion())))  .
114 1
                $status  . $migrationDescription);
115
        }
116 1
    }
117
}
118