Completed
Push — master ( 941231...16a1cd )
by Jonathan
04:11
created

UpToDateCommand::execute()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 4
nop 2
dl 0
loc 33
ccs 20
cts 20
cp 1
crap 7
rs 8.8333
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 Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use function count;
11
use function sprintf;
12
13
/**
14
 * The UpToDateCommand class outputs if your database is up to date or if there are new migrations
15
 * that need to be executed.
16
 */
17
class UpToDateCommand extends AbstractCommand
18
{
19 6
    protected function configure() : void
20
    {
21
        $this
22 6
            ->setName('migrations:up-to-date')
23 6
            ->setAliases(['up-to-date'])
24 6
            ->setDescription('Tells you if your schema is up-to-date.')
25 6
            ->addOption('fail-on-unregistered', 'u', InputOption::VALUE_NONE, 'Whether to fail when there are unregistered extra migrations found')
26 6
            ->setHelp(<<<EOT
27 6
The <info>%command.name%</info> command tells you if your schema is up-to-date:
28
29
    <info>%command.full_name%</info>
30
EOT
31
            );
32
33 6
        parent::configure();
34 6
    }
35
36 6
    public function execute(InputInterface $input, OutputInterface $output) : ?int
37
    {
38 6
        $migrations          = count($this->migrationRepository->getMigrations());
39 6
        $migratedVersions    = count($this->migrationRepository->getMigratedVersions());
40 6
        $availableMigrations = $migrations - $migratedVersions;
41
42 6
        if ($availableMigrations === 0) {
43 2
            $output->writeln('<comment>Up-to-date! No migrations to execute.</comment>');
44
45 2
            return 0;
46
        }
47
48 4
        if ($availableMigrations > 0) {
49 2
            $output->writeln(sprintf(
50 2
                '<error>Out-of-date! %u migration%s available to execute.</error>',
51 2
                $availableMigrations,
52 2
                $availableMigrations > 1 ? 's are' : ' is'
53
            ));
54
55 2
            return 1;
56
        }
57
58
        // negative number means that there are unregistered migrations in the database
59
60 2
        $extraMigrations = -$availableMigrations;
61 2
        $output->writeln(sprintf(
62 2
            '<error>You have %1$u previously executed migration%3$s in the database that %2$s registered migration%3$s.</error>',
63 2
            $extraMigrations,
64 2
            $extraMigrations > 1 ? 'are not' : 'is not a',
65 2
            $extraMigrations > 1 ? 's' : ''
66
        ));
67
68 2
        return $input->getOption('fail-on-unregistered') === true ? 2 : 0;
69
    }
70
}
71