Completed
Pull Request — master (#908)
by Asmir
02:35
created

ListCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 60
c 2
b 0
f 1
dl 0
loc 110
ccs 57
cts 57
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A getSortedVersions() 0 18 1
B showVersions() 0 50 9
A execute() 0 15 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
final 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();
0 ignored issues
show
Unused Code introduced by
The assignment to $availableMigrations is dead and can be removed.
Loading history...
51 1
        $executedMigrations  = $storage->getExecutedMigrations();
0 ignored issues
show
Unused Code introduced by
The assignment to $executedMigrations is dead and can be removed.
Loading history...
52
53 1
        $this->showVersions(
54 1
            $this->getDependencyFactory()->getMigrationRepository()->getMigrations(), // available migrations
55 1
            $this->getDependencyFactory()->getMetadataStorage()->getExecutedMigrations(), // executed migrations
56 1
            $output
57
        );
58
59 1
        return 0;
60
    }
61
62 1
    private function showVersions(
63
        AvailableMigrationsList $availableMigrationsSet,
64
        ExecutedMigrationsSet $executedMigrationsSet,
65
        OutputInterface $output
66
    ) : void {
67 1
        $versions = $this->getSortedVersions($availableMigrationsSet, $executedMigrationsSet);
0 ignored issues
show
Unused Code introduced by
The assignment to $versions is dead and can be removed.
Loading history...
68
69 1
        $table = new Table($output);
70 1
        $table->setHeaders(
71
            [
72 1
                [new TableCell('Migration Versions', ['colspan' => 4])],
73
                ['Migration', 'Status', 'Migrated At', 'Execution Time', 'Description'],
74
            ]
75
        );
76
77 1
        foreach ($this->getSortedVersions($availableMigrationsSet, $executedMigrationsSet) as $version) {
78 1
            $description   = null;
79 1
            $executedAt    = null;
80 1
            $executionTime = null;
81
82 1
            if ($executedMigrationsSet->hasMigration($version)) {
83 1
                $executedMigration = $executedMigrationsSet->getMigration($version);
84 1
                $executionTime     = $executedMigration->getExecutionTime();
85 1
                $executedAt        = $executedMigration->getExecutedAt() instanceof DateTimeInterface
86 1
                    ? $executedMigration->getExecutedAt()->format('Y-m-d H:i:s')
87 1
                    : null;
88
            }
89
90 1
            if ($availableMigrationsSet->hasMigration($version)) {
91 1
                $description = $availableMigrationsSet->getMigration($version)->getMigration()->getDescription();
92
            }
93
94 1
            if ($executedMigrationsSet->hasMigration($version) && $availableMigrationsSet->hasMigration($version)) {
95 1
                $status = '<info>migrated</info>';
96 1
            } elseif ($executedMigrationsSet->hasMigration($version)) {
97 1
                $status = '<error>migrated, not available</error>';
98
            } else {
99 1
                $status = '<comment>not migrated</comment>';
100
            }
101
102 1
            $table->addRow([
103 1
                (string) $version,
104 1
                $status,
105 1
                (string) $executedAt,
106 1
                $executionTime !== null ? $executionTime . 's': '',
107 1
                $description,
108
            ]);
109
        }
110
111 1
        $table->render();
112 1
    }
113
114
    /**
115
     * @return Version[]
116
     */
117 1
    private function getSortedVersions(AvailableMigrationsList $availableMigrationsSet, ExecutedMigrationsSet $executedMigrationsSet) : array
118
    {
119
        $availableVersions = array_map(static function (AvailableMigration $availableMigration) : Version {
120 1
            return $availableMigration->getVersion();
121 1
        }, $availableMigrationsSet->getItems());
122
123
        $executedVersions = array_map(static function (ExecutedMigration $executedMigration) : Version {
124 1
            return $executedMigration->getVersion();
125 1
        }, $executedMigrationsSet->getItems());
126
127 1
        $versions = array_unique(array_merge($availableVersions, $executedVersions));
128
129 1
        $comparator = $this->getDependencyFactory()->getVersionComparator();
130
        uasort($versions, static function (Version $a, Version $b) use ($comparator) : int {
131 1
            return $comparator->compare($a, $b);
132 1
        });
133
134 1
        return $versions;
135
    }
136
}
137