Completed
Pull Request — master (#908)
by Asmir
03:43 queued 38s
created

ListCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 1
rs 9.9666
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use DateTimeInterface;
8
use Doctrine\Migrations\Metadata\AvailableMigration;
9
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
10
use Doctrine\Migrations\Metadata\ExecutedMigration;
11
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
12
use Doctrine\Migrations\Version\Version;
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Helper\TableCell;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use function array_map;
18
use function array_merge;
19
use function array_unique;
20
use function uasort;
21
22
/**
23
 * The StatusCommand class is responsible for outputting a list of all available migrations and their status.
24
 */
25
class ListCommand extends DoctrineCommand
26
{
27
    /** @var string */
28
    protected static $defaultName = 'migrations:list';
29
30 1
    protected function configure() : void
31
    {
32
        $this
33 1
            ->setAliases(['list-migrations'])
34 1
            ->setDescription('Display a list of all available migrations and their status.')
35 1
            ->setHelp(<<<EOT
36 1
The <info>%command.name%</info> command outputs a list of all available migrations and their status:
37
38
    <info>%command.full_name%</info>
39
EOT
40
            );
41
42 1
        parent::configure();
43 1
    }
44
45 1
    public function execute(InputInterface $input, OutputInterface $output) : ?int
46
    {
47 1
        $storage       = $this->getDependencyFactory()->getMetadataStorage();
48 1
        $migrationRepo = $this->getDependencyFactory()->getMigrationRepository();
49
50 1
        $availableMigrations = $migrationRepo->getMigrations();
51 1
        $executedMigrations  = $storage->getExecutedMigrations();
52
53 1
        $this->showVersions($availableMigrations, $executedMigrations, $output);
54
55 1
        return 0;
56
    }
57
58 1
    private function showVersions(
59
        AvailableMigrationsList $availableMigrationsSet,
60
        ExecutedMigrationsSet $executedMigrationsSet,
61
        OutputInterface $output
62
    ) : void {
63 1
        $versions = $this->getSortedVersions($availableMigrationsSet, $executedMigrationsSet);
64
65 1
        $table = new Table($output);
66 1
        $table->setHeaders(
67
            [
68 1
                [new TableCell('Migration Versions', ['colspan' => 4])],
69
                ['Migration', 'Status', 'Migrated At', 'Execution Time', 'Description'],
70
            ]
71
        );
72
73 1
        foreach ($versions as $version) {
74 1
            $description   = null;
75 1
            $executedAt    = null;
76 1
            $executionTime = null;
77
78 1
            if ($executedMigrationsSet->hasMigration($version)) {
79 1
                $executedMigration = $executedMigrationsSet->getMigration($version);
80 1
                $executionTime     = $executedMigration->getExecutionTime();
81 1
                $executedAt        = $executedMigration->getExecutedAt() instanceof DateTimeInterface
82 1
                    ? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s')
83 1
                    : null;
84
            }
85
86 1
            if ($availableMigrationsSet->hasMigration($version)) {
87 1
                $description = $availableMigrationsSet->getMigration($version)->getMigration()->getDescription();
88
            }
89
90 1
            if ($executedMigrationsSet->hasMigration($version) && $availableMigrationsSet->hasMigration($version)) {
91 1
                $status = '<info>migrated</info>';
92 1
            } elseif ($executedMigrationsSet->hasMigration($version)) {
93 1
                $status = '<error>migrated, not available</error>';
94
            } else {
95 1
                $status = '<comment>not migrated</comment>';
96
            }
97
98 1
            $table->addRow([
99 1
                (string) $version,
100 1
                $status,
101 1
                (string) $executedAt,
102 1
                $executionTime !== null ? $executionTime . 's': '',
103 1
                $description,
104
            ]);
105
        }
106
107 1
        $table->render();
108 1
    }
109
110
    /**
111
     * @return Version[]
112
     */
113 1
    private function getSortedVersions(AvailableMigrationsList $availableMigrationsSet, ExecutedMigrationsSet $executedMigrationsSet) : array
114
    {
115
        $availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version {
116 1
            return $availableMigration->getVersion();
117 1
        }, $availableMigrationsSet->getItems());
118
119
        $executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version {
120 1
            return $executedMigration->getVersion();
121 1
        }, $executedMigrationsSet->getItems());
122
123 1
        $versions = array_unique(array_merge($availableVersions, $executedVersions));
124
125 1
        $comparator = $this->getDependencyFactory()->getVersionComparator();
126
        uasort($versions, static function (Version $a, Version $b) use ($comparator) : int {
127 1
            return $comparator->compare($a, $b);
128 1
        });
129
130 1
        return $versions;
131
    }
132
}
133