Completed
Push — master ( 8ace2b...790ad0 )
by
unknown
8s
created

MigrateCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 95.92%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 47
cts 49
cp 0.9592
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A getName() 0 3 1
A createMigration() 0 3 1
B execute() 0 64 7
1
<?php
2
3
/*
4
 * This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald.
5
 *
6
 * (c) 2014 Matthew Fitzgerald
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AntiMattr\MongoDB\Migrations\Tools\Console\Command;
13
14
use AntiMattr\MongoDB\Migrations\Migration;
15
use AntiMattr\MongoDB\Migrations\Configuration\Configuration;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Question\ConfirmationQuestion;
20
21
/**
22
 * @author Matthew Fitzgerald <[email protected]>
23
 */
24
class MigrateCommand extends AbstractCommand
25
{
26
    const NAME = 'mongodb:migrations:migrate';
27
28 4
    protected function configure()
29
    {
30
        $this
31 4
            ->setName($this->getName())
32 4
            ->setDescription('Execute a migration to a specified version or the latest available version.')
33 4
            ->addArgument('version', InputArgument::OPTIONAL, 'The version to migrate to.', null)
34 4
            ->setHelp(<<<'EOT'
35 4
The <info>%command.name%</info> command executes a migration to a specified version or the latest available version:
36
37
    <info>%command.full_name%</info>
38
39
You can optionally manually specify the version you wish to migrate to:
40
41
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
42
43
Or you can also execute the migration without a warning message which you need to interact with:
44
45
    <info>%command.full_name% --no-interaction</info>
46
47
EOT
48
        );
49
50 4
        parent::configure();
51 4
    }
52
53
    /**
54
     * @param Symfony\Component\Console\Input\InputInterface
55
     * @param Symfony\Component\Console\Output\OutputInterface
56
     */
57 4
    public function execute(InputInterface $input, OutputInterface $output)
58
    {
59 4
        $version = $input->getArgument('version');
60
61 4
        $configuration = $this->getMigrationConfiguration($input, $output);
62 4
        $migration = $this->createMigration($configuration);
63
64 4
        $this->outputHeader($configuration, $output);
65
66 4
        $noInteraction = !$input->isInteractive();
67
68 4
        $executedVersions = $configuration->getMigratedVersions();
69 4
        $availableVersions = $configuration->getAvailableVersions();
70 4
        $executedUnavailableVersions = array_diff($executedVersions, $availableVersions);
71
72 4
        if ($executedUnavailableVersions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $executedUnavailableVersions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
73 3
            $output->writeln(sprintf('<error>WARNING! You have %s previously executed migrations in the database that are not registered migrations.</error>', count($executedUnavailableVersions)));
74 3
            foreach ($executedUnavailableVersions as $executedUnavailableVersion) {
75 3
                $output->writeln(
76 3
                    sprintf(
77 3
                        '    <comment>>></comment> %s (<comment>%s</comment>)',
78 3
                        Configuration::formatVersion($executedUnavailableVersion),
79 3
                        $executedUnavailableVersion
80
                    )
81
                );
82
            }
83
84 3
            if (!$noInteraction) {
85 3
                $question = new ConfirmationQuestion(
86 3
                    '<question>Are you sure you wish to continue? (y/[n])</question> ',
87 3
                    false
88
                );
89
90
                $confirmation = $this
91 3
                    ->getHelper('question')
92 3
                    ->ask($input, $output, $question);
93
94 3
                if (!$confirmation) {
95 1
                    $output->writeln('<error>Migration cancelled!</error>');
96
97 1
                    return 1;
98
                }
99
            }
100
        }
101
102
        // warn the user if no dry run and interaction is on
103 3
        if (!$noInteraction) {
104 2
            $question = new ConfirmationQuestion(
105 2
                '<question>WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/[n])</question> ',
106 2
                false
107
            );
108
109
            $confirmation = $this
110 2
                ->getHelper('question')
111 2
                ->ask($input, $output, $question);
112
113 2
            if (!$confirmation) {
114 1
                $output->writeln('<error>Migration cancelled!</error>');
115
116 1
                return 1;
117
            }
118
        }
119
120 2
        $migration->migrate($version);
121 2
    }
122
123
    protected function createMigration(Configuration $configuration)
124
    {
125
        return new Migration($configuration);
126
    }
127
128 4
    public function getName()
129
    {
130 4
        return self::NAME;
131
    }
132
}
133