Passed
Pull Request — master (#47)
by Simon
04:32
created

StatusCommand::execute()   D

Complexity

Conditions 14
Paths 224

Size

Total Lines 99
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 14.0176

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 69
c 4
b 0
f 0
nc 224
nop 2
dl 0
loc 99
ccs 64
cts 67
cp 0.9552
crap 14.0176
rs 4.783

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
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command;
13
14
use AntiMattr\MongoDB\Migrations\Migration;
15
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Helper\Table;
20
21
/**
22
 * @author Matthew Fitzgerald <[email protected]>
23
 */
24
class StatusCommand extends AbstractCommand
25
{
26
    protected static $defaultName = 'mongodb:migrations:status';
27
28 2
    protected function configure()
29
    {
30
        $this
31 2
            ->setDescription('View the status of a set of migrations.')
32 2
            ->addOption(
33 2
                'show-versions',
34 2
                null,
35 2
                InputOption::VALUE_NONE,
36 2
                'This will display a list of all available migrations and their status'
37
            )
38 2
            ->setHelp(<<<'EOT'
39 2
The <info>%command.name%</info> command outputs the status of a set of migrations:
40
41
    <info>%command.full_name%</info>
42
43
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
44
45
    <info>%command.full_name% --show-versions</info>
46
EOT
47
        );
48
49 2
        parent::configure();
50 2
    }
51
52
    /**
53
     * @param \Symfony\Component\Console\Input\InputInterface
54
     * @param \Symfony\Component\Console\Output\OutputInterface
55
     */
56 2
    public function execute(InputInterface $input, OutputInterface $output)
57
    {
58 2
        $configuration = $this->getMigrationConfiguration($input, $output);
59 2
        $configMap = $configuration->getDetailsMap();
60
61
        // Format current version string
62 2
        $currentVersion = $configMap['current_version'];
63 2
        if ($currentVersion) {
64 2
            $currentVersionFormatted = sprintf(
65 2
                '%s (<comment>%s</comment>)',
66 2
                Configuration::formatVersion($currentVersion),
67 2
                $currentVersion
68
            );
69
        } else {
70
            $currentVersionFormatted = 0;
71
        }
72
73
        // Format latest version string
74 2
        $latestVersion = $configMap['latest_version'];
75 2
        if ($latestVersion) {
76 2
            $latestVersionFormatted = sprintf(
77 2
                '%s (<comment>%s</comment>)',
78 2
                Configuration::formatVersion($latestVersion),
79 2
                $latestVersion
80
            );
81
        } else {
82
            $latestVersionFormatted = 0;
83
        }
84
85 2
        $output->writeln("\n <info>==</info> Configuration\n");
86
87 2
        $numExecutedUnavailableMigrations = $configMap['num_executed_unavailable_migrations'];
88 2
        $numNewMigrations = $configMap['num_new_migrations'];
89
90
        $info = [
91 2
            'Name' => $configMap['name'],
92 2
            'Database Driver' => $configMap['database_driver'],
93 2
            'Database Name' => $configMap['migrations_database_name'],
94 2
            'Configuration Source' => $configuration->getFile() ?: 'manually configured',
95 2
            'Version Collection Name' => $configMap['migrations_collection_name'],
96 2
            'Migrations Namespace' => $configMap['migrations_namespace'],
97 2
            'Migrations Directory' => $configMap['migrations_directory'],
98 2
            'Current Version' => $currentVersionFormatted,
99 2
            'Latest Version' => $latestVersionFormatted,
100 2
            'Executed Migrations' => $configMap['num_executed_migrations'],
101 2
            'Executed Unavailable Migrations' => $numExecutedUnavailableMigrations > 0 ? '<error>' . $numExecutedUnavailableMigrations . '</error>' : 0,
102 2
            'Available Migrations' => $configMap['num_available_migrations'],
103 2
            'New Migrations' => $numNewMigrations > 0 ? '<question>' . $numNewMigrations . '</question>' : 0,
104
        ];
105
106 2
        foreach ($info as $name => $value) {
107 2
            $this->writeInfoLine($output, $name, $value);
108
        }
109
110 2
        if ($input->getOption('show-versions')) {
111 1
            if ($migrations = $configuration->getMigrations()) {
112 1
                $output->writeln("\n <info>==</info> Available Migration Versions\n");
113 1
                $rows = [];
114 1
                $migratedVersions = $configuration->getMigratedVersions();
115
116 1
                foreach ($migrations as $version) {
117 1
                    $isMigrated = in_array($version->getVersion(), $migratedVersions);
118 1
                    $status = '<error>not migrated</error>';
119
120 1
                    if ($isMigrated) {
121 1
                        $ts = $configuration->getMigratedTimestamp(
122 1
                            $version->getVersion()
123
                        );
124
125 1
                        $status = sprintf(
126 1
                            '<info>%s</info>',
127 1
                            date('Y-m-d H:i', $ts)
128
                        );
129
                    }
130
131 1
                    $versionTxt = sprintf('<comment>%s</comment>', $version->getVersion());
132 1
                    $desc = $version->getMigration()->getDescription();
133 1
                    if (strlen($desc) > 80) {
134
                        $desc = substr($desc, 0, 78) . '...';
135
                    }
136
137 1
                    $rows[] = [$versionTxt, $status, $desc];
138
                }
139
140 1
                $table = new Table($output);
141 1
                $table->setHeaders(['Version', 'Date Migrated', 'Description'])
142 1
                      ->setRows($rows)
143 1
                      ->render();
144
            }
145
146 1
            $executedUnavailableMigrations = $configuration->getUnavailableMigratedVersions();
147 1
            if (!empty($executedUnavailableMigrations)) {
148 1
                $output->writeln("\n <info>==</info> Previously Executed Unavailable Migration Versions\n");
149 1
                foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
150 1
                    $output->writeln(
151 1
                        sprintf(
152 1
                            '    <comment>>></comment> %s (<comment>%s</comment>)',
153 1
                            Configuration::formatVersion($executedUnavailableMigration),
154 1
                            $executedUnavailableMigration
155
                        )
156
                    );
157
                }
158
            }
159
        }
160 2
    }
161
162
    /**
163
     * @param string $name
164
     * @param string $value
165
     */
166
    protected function writeInfoLine(OutputInterface $output, $name, $value)
167
    {
168
        $whitespace = str_repeat(' ', 35 - strlen($name));
169
        $output->writeln(
170
            sprintf(
171
                '    <comment>>></comment> %s: %s%s',
172
                $name,
173
                $whitespace,
174
                $value
175
            )
176
        );
177
    }
178
}
179