Passed
Pull Request — master (#739)
by Michael
02:38
created

StatusCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 24
ccs 12
cts 12
cp 1
crap 1
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use DateTimeImmutable;
8
use Doctrine\Migrations\Version\Version;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use function assert;
13
use function count;
14
use function is_string;
15
use function max;
16
use function sprintf;
17
use function str_repeat;
18
use function strlen;
19
20
/**
21
 * The StatusCommand class is responsible for outputting what the current state is of all your migrations. It shows
22
 * what your current version is, how many new versions you have to execute, etc. and details about each of your migrations.
23
 */
24
class StatusCommand extends AbstractCommand
25
{
26 13
    protected function configure() : void
27
    {
28
        $this
29 13
            ->setName('migrations:status')
30 13
            ->setAliases(['status'])
31 13
            ->setDescription('View the status of a set of migrations.')
32 13
            ->addOption(
33 13
                'show-versions',
34 13
                null,
35 13
                InputOption::VALUE_NONE,
36 13
                'This will display a list of all available migrations and their status'
37
            )
38 13
            ->setHelp(<<<EOT
39 13
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 13
        parent::configure();
50 13
    }
51
52 9
    public function execute(InputInterface $input, OutputInterface $output) : ?int
53
    {
54 9
        $output->writeln("\n <info>==</info> Configuration\n");
55
56 9
        $infos = $this->dependencyFactory->getMigrationStatusInfosHelper();
57
58 9
        foreach ($infos->getMigrationsInfos() as $name => $value) {
59 9
            assert(is_string($name));
60
61 9
            $string = (string) $value;
62
63 9
            if ($name === 'New Migrations') {
64 8
                $string = $value > 0 ? '<question>' . $value . '</question>' : '0';
65
            }
66
67 9
            if ($name === 'Executed Unavailable Migrations') {
68 8
                $string = $value > 0 ? '<error>' . $value . '</error>' : '0';
69
            }
70
71 9
            $this->writeStatusInfosLineAligned($output, $name, $string);
72
        }
73
74 9
        if ($input->getOption('show-versions') === false) {
75 7
            return 0;
76
        }
77
78 2
        $versions                      = $this->migrationRepository->getMigrations();
79 2
        $executedUnavailableMigrations = $this->migrationRepository->getExecutedUnavailableMigrations();
80
81 2
        if (count($versions) !== 0) {
82 2
            $output->writeln("\n <info>==</info> Available Migration Versions\n");
83
84 2
            $this->showVersions($versions, $output);
85
        }
86
87 2
        if (count($executedUnavailableMigrations) === 0) {
88 1
            return 0;
89
        }
90
91 1
        $output->writeln(
92 1
            "\n <info>==</info> Previously Executed Unavailable Migration Versions\n"
93
        );
94
95 1
        foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
96 1
            $output->writeln(
97 1
                sprintf(
98 1
                    '    <comment>>></comment> %s (<comment>%s</comment>)',
99 1
                    $this->configuration->getDateTime($executedUnavailableMigration),
100 1
                    $executedUnavailableMigration
101
                )
102
            );
103
        }
104
105 1
        return 0;
106
    }
107
108 9
    private function writeStatusInfosLineAligned(OutputInterface $output, string $title, ?string $value) : void
109
    {
110 9
        $output->writeln(sprintf(
111 9
            '    <comment>>></comment> %s: %s%s',
112 9
            $title,
113 9
            str_repeat(' ', 50 - strlen($title)),
114 9
            $value
115
        ));
116 9
    }
117
118
    /**
119
     * @param Version[] $versions
120
     */
121 2
    private function showVersions(
122
        array $versions,
123
        OutputInterface $output
124
    ) : void {
125 2
        foreach ($versions as $version) {
126 2
            $executedAt = $version->getExecutedAt();
127
128 2
            $status = $version->isMigrated() ? '<info>migrated</info>' : '<error>not migrated</error>';
129
130 2
            $executedAtStatus = $executedAt instanceof DateTimeImmutable
131 2
                ? sprintf(' (executed at %s)', $executedAt->format('Y-m-d H:i:s'))
132 2
                : '';
133
134 2
            $migration   = $version->getMigration();
135 2
            $description = $migration->getDescription();
136
137 2
            $migrationDescription = $description !== ''
138 1
                ? str_repeat(' ', 5) . $description
139 2
                : '';
140
141 2
            $versionName      = $version->getVersion();
142 2
            $formattedVersion = $version->getDateTime();
143
144 2
            $output->writeln(sprintf(
145 2
                '    <comment>>></comment> %s (<comment>%s</comment>)%s%s%s%s',
146 2
                $formattedVersion,
147 2
                $versionName,
148 2
                str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($versionName))),
149 2
                $status,
150 2
                $executedAtStatus,
151 2
                $migrationDescription
152
            ));
153
        }
154 2
    }
155
}
156