Completed
Push — master ( 55f34b...006836 )
by Catalin
08:33 queued 05:38
created

StatusCommand::execute()   C

Complexity

Conditions 14
Paths 160

Size

Total Lines 106
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 14.0193

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 14
eloc 70
c 4
b 0
f 0
nc 160
nop 2
dl 0
loc 106
ccs 62
cts 65
cp 0.9538
crap 14.0193
rs 5.2678

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