Completed
Push — master ( d8679a...93927c )
by Asmir
30s queued 12s
created

UpToDateCommand::getSortedVersions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 1
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Tools\Console\Command;
6
7
use Doctrine\Migrations\Metadata\AvailableMigration;
8
use Doctrine\Migrations\Metadata\AvailableMigrationsList;
9
use Doctrine\Migrations\Metadata\ExecutedMigration;
10
use Doctrine\Migrations\Metadata\ExecutedMigrationsSet;
11
use Doctrine\Migrations\Version\Version;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use function array_map;
16
use function array_merge;
17
use function array_unique;
18
use function count;
19
use function sprintf;
20
use function uasort;
21
22
/**
23
 * The UpToDateCommand class outputs if your database is up to date or if there are new migrations
24
 * that need to be executed.
25
 */
26
class UpToDateCommand extends DoctrineCommand
27
{
28
    /** @var string */
29
    protected static $defaultName = 'migrations:up-to-date';
30
31 7
    protected function configure() : void
32
    {
33
        $this
34 7
            ->setAliases(['up-to-date'])
35 7
            ->setDescription('Tells you if your schema is up-to-date.')
36 7
            ->addOption('fail-on-unregistered', 'u', InputOption::VALUE_NONE, 'Whether to fail when there are unregistered extra migrations found')
37 7
            ->addOption('list-migrations', 'l', InputOption::VALUE_NONE, 'Show a list of missing or not migrated versions.')
38 7
            ->setHelp(<<<EOT
39 7
The <info>%command.name%</info> command tells you if your schema is up-to-date:
40
41
    <info>%command.full_name%</info>
42
EOT
43
            );
44
45 7
        parent::configure();
46 7
    }
47
48 7
    public function execute(InputInterface $input, OutputInterface $output) : ?int
49
    {
50 7
        $statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator();
51
52 7
        $executedUnavailableMigrations      = $statusCalculator->getExecutedUnavailableMigrations();
53 7
        $newMigrations                      = $statusCalculator->getNewMigrations();
54 7
        $newMigrationsCount                 = count($newMigrations);
55 7
        $executedUnavailableMigrationsCount = count($executedUnavailableMigrations);
56
57 7
        if ($newMigrationsCount === 0 && $executedUnavailableMigrationsCount === 0) {
58 2
            $output->writeln('<comment>Up-to-date! No migrations to execute.</comment>');
59
60 2
            return 0;
61
        }
62
63 5
        $exitCode = 0;
64 5
        if ($newMigrationsCount > 0) {
65 3
            $output->writeln(sprintf(
66 3
                '<error>Out-of-date! %u migration%s available to execute.</error>',
67 3
                $newMigrationsCount,
68 3
                $newMigrationsCount > 1 ? 's are' : ' is'
69
            ));
70 3
            $exitCode = 1;
71
        }
72
73 5
        if ($executedUnavailableMigrationsCount > 0) {
74 3
            $output->writeln(sprintf(
75 3
                '<error>You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.</error>',
76 3
                $executedUnavailableMigrationsCount,
77 3
                $executedUnavailableMigrationsCount > 1 ? 'are not' : 'is not a',
78 3
                $executedUnavailableMigrationsCount > 1 ? 's' : ''
79
            ));
80 3
            if ($input->getOption('fail-on-unregistered')) {
81 1
                $exitCode = 2;
82
            }
83
        }
84
85 5
        if ($input->getOption('list-migrations')) {
86 1
            $versions = $this->getSortedVersions($newMigrations, $executedUnavailableMigrations);
87 1
            $this->getDependencyFactory()->getMigrationStatusInfosHelper()->listVersions($versions, $output);
88
        }
89
90 5
        return $exitCode;
91
    }
92
93
    /**
94
     * @return Version[]
95
     */
96 1
    private function getSortedVersions(AvailableMigrationsList $newMigrations, ExecutedMigrationsSet $executedUnavailableMigrations) : array
97
    {
98
        $executedUnavailableVersion = array_map(static function (ExecutedMigration $executedMigration) : Version {
99 1
            return $executedMigration->getVersion();
100 1
        }, $executedUnavailableMigrations->getItems());
101
102
        $newVersions = array_map(static function (AvailableMigration $availableMigration) : Version {
103 1
            return $availableMigration->getVersion();
104 1
        }, $newMigrations->getItems());
105
106 1
        $versions = array_unique(array_merge($executedUnavailableVersion, $newVersions));
107
108 1
        $comparator = $this->getDependencyFactory()->getVersionComparator();
109
        uasort($versions, static function (Version $a, Version $b) use ($comparator) : int {
110 1
            return $comparator->compare($a, $b);
111 1
        });
112
113 1
        return $versions;
114
    }
115
}
116