Completed
Push — master ( ead074...fd19fe )
by Jonathan
12s
created

StatusCommand::writeStatusInfosLineAligned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
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;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use function count;
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
            ->setAliases(['status'])
25 11
            ->setDescription('View the status of a set of migrations.')
26 11
            ->addOption(
27 11
                'show-versions',
28 11
                null,
29 11
                InputOption::VALUE_NONE,
30 11
                'This will display a list of all available migrations and their status'
31
            )
32 11
            ->setHelp(<<<EOT
33 11
The <info>%command.name%</info> command outputs the status of a set of migrations:
34
35
    <info>%command.full_name%</info>
36
37
You can output a list of all available migrations and their status with <comment>--show-versions</comment>:
38
39
    <info>%command.full_name% --show-versions</info>
40
EOT
41
        );
42
43 11
        parent::configure();
44 11
    }
45
46 9
    public function execute(InputInterface $input, OutputInterface $output) : void
47
    {
48 9
        $output->writeln("\n <info>==</info> Configuration\n");
49
50 9
        $infos = $this->dependencyFactory->getMigrationStatusInfosHelper();
51
52 9
        foreach ($infos->getMigrationsInfos() as $name => $value) {
53 9
            $string = (string) $value;
54
55 9
            if ($name === 'New Migrations') {
56 8
                $string = $value > 0 ? '<question>' . $value . '</question>' : '0';
57
            }
58
59 9
            if ($name === 'Executed Unavailable Migrations') {
60 8
                $string = $value > 0 ? '<error>' . $value . '</error>' : '0';
61
            }
62
63 9
            $this->writeStatusInfosLineAligned($output, $name, $string);
64
        }
65
66 9
        if ($input->getOption('show-versions') === false) {
67 7
            return;
68
        }
69
70 2
        $versions                      = $this->migrationRepository->getMigrations();
71 2
        $executedUnavailableMigrations = $this->migrationRepository->getExecutedUnavailableMigrations();
72
73 2
        if (count($versions) !== 0) {
74 2
            $output->writeln("\n <info>==</info> Available Migration Versions\n");
75
76 2
            $this->showVersions($versions, $output);
77
        }
78
79 2
        if (count($executedUnavailableMigrations) === 0) {
80 1
            return;
81
        }
82
83 1
        $output->writeln(
84 1
            "\n <info>==</info> Previously Executed Unavailable Migration Versions\n"
85
        );
86
87 1
        foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
88 1
            $output->writeln(
89 1
                sprintf(
90 1
                    '    <comment>>></comment> %s (<comment>%s</comment>)',
91 1
                    $this->configuration->getDateTime($executedUnavailableMigration),
92 1
                    $executedUnavailableMigration
93
                )
94
            );
95
        }
96 1
    }
97
98 9
    private function writeStatusInfosLineAligned(OutputInterface $output, string $title, ?string $value) : void
99
    {
100 9
        $output->writeln(sprintf(
101 9
            '    <comment>>></comment> %s: %s%s',
102 9
            $title,
103 9
            str_repeat(' ', 50 - strlen($title)),
104 9
            $value
105
        ));
106 9
    }
107
108
    /**
109
     * @param Version[] $versions
110
     */
111 2
    private function showVersions(
112
        array $versions,
113
        OutputInterface $output
114
    ) : void {
115 2
        foreach ($versions as $version) {
116 2
            $executedAt = $version->getExecutedAt();
117
118 2
            $status = $version->isMigrated() ? '<info>migrated</info>' : '<error>not migrated</error>';
119
120 2
            $executedAtStatus = $executedAt instanceof DateTimeImmutable
121 2
                ? sprintf(' (executed at %s)', $executedAt->format('Y-m-d H:i:s'))
122 2
                : '';
123
124 2
            $migration   = $version->getMigration();
125 2
            $description = $migration->getDescription();
126
127 2
            $migrationDescription = $description !== ''
128 1
                ? str_repeat(' ', 5) . $description
129 2
                : '';
130
131 2
            $versionName      = $version->getVersion();
132 2
            $formattedVersion = $version->getDateTime();
133
134 2
            $output->writeln(sprintf(
135 2
                '    <comment>>></comment> %s (<comment>%s</comment>)%s%s%s%s',
136 2
                $formattedVersion,
137 2
                $versionName,
138 2
                str_repeat(' ', max(1, 49 - strlen($formattedVersion) - strlen($versionName))),
139 2
                $status,
140 2
                $executedAtStatus,
141 2
                $migrationDescription
142
            ));
143
        }
144 2
    }
145
}
146