UpToDateCommand::getSortedVersions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 19
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\ExecutedMigrationsList;
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
final 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
    protected 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
            $this->io->success('Up-to-date! No migrations to execute.');
59
60 2
            return 0;
61
        }
62
63 5
        $exitCode = 0;
64 5
        if ($newMigrationsCount > 0) {
65 3
            $this->io->error(sprintf(
66 3
                'Out-of-date! %u migration%s available to execute.',
67 3
                $newMigrationsCount,
68 3
                $newMigrationsCount > 1 ? 's are' : ' is'
69
            ));
70 3
            $exitCode = 1;
71
        }
72
73 5
        if ($executedUnavailableMigrationsCount > 0) {
74 3
            $this->io->error(sprintf(
75 3
                'You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.',
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 1
            $this->io->newLine();
90
        }
91
92 5
        return $exitCode;
93
    }
94
95
    /**
96
     * @return Version[]
97
     */
98 1 View Code Duplication
    private function getSortedVersions(AvailableMigrationsList $newMigrations, ExecutedMigrationsList $executedUnavailableMigrations) : array
99
    {
100
        $executedUnavailableVersion = array_map(static function (ExecutedMigration $executedMigration) : Version {
101 1
            return $executedMigration->getVersion();
102 1
        }, $executedUnavailableMigrations->getItems());
103
104
        $newVersions = array_map(static function (AvailableMigration $availableMigration) : Version {
105 1
            return $availableMigration->getVersion();
106 1
        }, $newMigrations->getItems());
107
108 1
        $versions = array_unique(array_merge($executedUnavailableVersion, $newVersions));
109
110 1
        $comparator = $this->getDependencyFactory()->getVersionComparator();
111
        uasort($versions, static function (Version $a, Version $b) use ($comparator) : int {
112 1
            return $comparator->compare($a, $b);
113 1
        });
114
115 1
        return $versions;
116
    }
117
}
118