Completed
Pull Request — master (#665)
by Jonathan
02:09
created

StatusCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 66
cts 66
cp 1
rs 10
c 0
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeStatusInfosLineAligned() 0 7 1
A configure() 0 23 1
B showVersions() 0 28 4
C execute() 0 47 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\Version;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use function count;
12
use function in_array;
13
use function max;
14
use function sprintf;
15
use function str_repeat;
16
use function strlen;
17
18
class StatusCommand extends AbstractCommand
19
{
20 11
    protected function configure() : void
21
    {
22
        $this
23 11
            ->setName('migrations:status')
24 11
            ->setDescription('View the status of a set of migrations.')
25 11
            ->addOption(
26 11
                'show-versions',
27 11
                null,
28 11
                InputOption::VALUE_NONE,
29 11
                'This will display a list of all available migrations and their status'
30
            )
31 11
            ->setHelp(<<<EOT
32 11
The <info>%command.name%</info> command outputs the status of a set of migrations:
33
34
    <info>%command.full_name%</info>
35
36
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
37
38
    <info>%command.full_name% --show-versions</info>
39
EOT
40
        );
41
42 11
        parent::configure();
43 11
    }
44
45 9
    public function execute(InputInterface $input, OutputInterface $output) : void
46
    {
47 9
        $output->writeln("\n <info>==</info> Configuration\n");
48
49 9
        $infos = $this->dependencyFactory->getMigrationStatusInfosHelper();
50
51 9
        foreach ($infos->getMigrationsInfos() as $name => $value) {
52 9
            $string = (string) $value;
53
54 9
            if ($name === 'New Migrations') {
55 8
                $string = $value > 0 ? '<question>' . $value . '</question>' : '0';
56
            }
57
58 9
            if ($name === 'Executed Unavailable Migrations') {
59 8
                $string = $value > 0 ? '<error>' . $value . '</error>' : '0';
60
            }
61
62 9
            $this->writeStatusInfosLineAligned($output, $name, $string);
63
        }
64
65 9
        if ($input->getOption('show-versions') === false) {
66 7
            return;
67
        }
68
69 2
        $versions                      = $this->migrationRepository->getMigrations();
70 2
        $executedUnavailableMigrations = $this->migrationRepository->getExecutedUnavailableMigrations();
71
72 2
        if (count($versions) !== 0) {
73 2
            $output->writeln("\n <info>==</info> Available Migration Versions\n");
74
75 2
            $this->showVersions($versions, $output);
76
        }
77
78 2
        if (count($executedUnavailableMigrations) === 0) {
79 1
            return;
80
        }
81
82 1
        $output->writeln(
83 1
            "\n <info>==</info> Previously Executed Unavailable Migration Versions\n"
84
        );
85
86 1
        foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
87 1
            $output->writeln(
88 1
                sprintf(
89 1
                    '    <comment>>></comment> %s (<comment>%s</comment>)',
90 1
                    $this->configuration->getDateTime($executedUnavailableMigration),
91 1
                    $executedUnavailableMigration
92
                )
93
            );
94
        }
95 1
    }
96
97 9
    private function writeStatusInfosLineAligned(OutputInterface $output, string $title, ?string $value) : void
98
    {
99 9
        $output->writeln(sprintf(
100 9
            '    <comment>>></comment> %s: %s%s',
101 9
            $title,
102 9
            str_repeat(' ', 50 - strlen($title)),
103 9
            $value
104
        ));
105 9
    }
106
107
    /**
108
     * @param Version[] $versions
109
     */
110 2
    private function showVersions(
111
        array $versions,
112
        OutputInterface $output
113
    ) : void {
114 2
        $migratedVersions = $this->migrationRepository->getMigratedVersions();
115
116 2
        foreach ($versions as $version) {
117 2
            $versionName = $version->getVersion();
118
119 2
            $isMigrated = in_array($versionName, $migratedVersions, true);
120 2
            $status     = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
121
122 2
            $migration   = $version->getMigration();
123 2
            $description = $migration->getDescription();
124
125 2
            $migrationDescription = $description !== ''
126 1
                ? str_repeat(' ', 5) . $description
127 2
                : '';
128
129 2
            $formattedVersion = $version->getDateTime();
130
131 2
            $output->writeln(sprintf(
132 2
                '    <comment>>></comment> %s (<comment>%s</comment>)%s%s%s',
133 2
                $formattedVersion,
134 2
                $versionName,
135 2
                str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($versionName))),
136 2
                $status,
137 2
                $migrationDescription
138
            ));
139
        }
140 2
    }
141
}
142