Passed
Pull Request — master (#41)
by
unknown
02:54
created

StatusCommand::execute()   D

Complexity

Conditions 14
Paths 224

Size

Total Lines 99
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 37.4139

Importance

Changes 0
Metric Value
cc 14
eloc 69
nc 224
nop 2
dl 0
loc 99
ccs 34
cts 67
cp 0.5074
crap 37.4139
rs 4.783
c 0
b 0
f 0

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