Completed
Push — master ( cdb33e...7ab492 )
by Mike
03:49
created

StatusCommand::showVersions()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 14.4166

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
ccs 2
cts 15
cp 0.1333
rs 9.2
cc 4
eloc 13
nc 5
nop 3
crap 14.4166
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\AbstractFileConfiguration;
23
use Doctrine\DBAL\Migrations\Configuration\Configuration;
24
use Doctrine\DBAL\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
29
/**
30
 * Command to view the status of a set of migrations.
31
 *
32
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
33
 * @link    www.doctrine-project.org
34
 * @since   2.0
35
 * @author  Jonathan Wage <[email protected]>
36
 */
37
class StatusCommand extends AbstractCommand
38
{
39 9
    protected function configure()
40
    {
41 9
        $this
42 9
            ->setName('migrations:status')
43 9
            ->setDescription('View the status of a set of migrations.')
44 9
            ->addOption('show-versions', null, InputOption::VALUE_NONE, 'This will display a list of all available migrations and their status')
45 9
            ->setHelp(<<<EOT
46
The <info>%command.name%</info> command outputs the status of a set of migrations:
47
48
    <info>%command.full_name%</info>
49
50
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
51
52
    <info>%command.full_name% --show-versions</info>
53
EOT
54 9
        );
55
56 9
        parent::configure();
57 9
    }
58
59 7
    public function execute(InputInterface $input, OutputInterface $output)
60
    {
61 7
        $configuration = $this->getMigrationConfiguration($input, $output);
62
63 7
        $infos = new MigrationStatusInfosHelper($configuration);
64
65 7
        $output->writeln("\n <info>==</info> Configuration\n");
66 7
        foreach ($infos->getMigrationsInfos() as $name => $value) {
67 7
            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...
68 7
                $value = $value > 0 ? '<question>' . $value . '</question>' : 0;
69 7
            }
70 7
            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...
71 7
                $value = $value > 0 ? '<error>' . $value . '</error>' : 0;
72 7
            }
73 7
            $this->writeStatusInfosLineAligned($output, $name, $value);
74 7
        }
75
76 7
        if ($input->getOption('show-versions')) {
77
            if ($migrations = $configuration->getMigrations()) {
78
                $output->writeln("\n <info>==</info> Available Migration Versions\n");
79
80
                $this->showVersions($migrations, $configuration, $output);
81
            }
82
83
            if (count($infos->getExecutedUnavailableMigrations())) {
84
                $output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
85
                foreach ($infos->getExecutedUnavailableMigrations() as $executedUnavailableMigration) {
86
                    $output->writeln('    <comment>>></comment> ' . $configuration->getDateTime($executedUnavailableMigration) .
87
                        ' (<comment>' . $executedUnavailableMigration . '</comment>)');
88
                }
89
            }
90
        }
91 7
    }
92
93 7
    private function writeStatusInfosLineAligned(OutputInterface $output, $title, $value)
94
    {
95 7
        $output->writeln('    <comment>>></comment> ' . $title . ': ' . str_repeat(' ', 50 - strlen($title)) . $value);
96 7
    }
97
98 1
    private function showVersions($migrations, Configuration $configuration, OutputInterface $output)
99
    {
100
        $migratedVersions = $configuration->getMigratedVersions();
101
102
        foreach($migrations as $version) {
103
            $isMigrated = in_array($version->getVersion(), $migratedVersions);
104
            $status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
105
106
            $migrationDescription = $version->getMigration()->getDescription()
107
                ? str_repeat(' ', 5) . $version->getMigration()->getDescription()
108
                : '';
109
110
            $formattedVersion = $configuration->getDateTime($version->getVersion());
111
112 1
            $output->writeln('    <comment>>></comment> ' . $formattedVersion .
113
                ' (<comment>' . $version->getVersion() . '</comment>)' .
114
                str_repeat(' ', 49 - strlen($formattedVersion) - strlen($version->getVersion()))  .
115
                $status  . $migrationDescription);
116
        }
117
    }
118
}
119