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

ExecuteCommand::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 2
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
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 Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
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 ExecuteCommand extends AbstractCommand
25
{
26
    const NAME = 'mongodb:migrations:execute';
27
28 4
    protected function configure()
29
    {
30
        $this
31 4
            ->setName($this->getName())
32 4
            ->setDescription('Execute a single migration version up or down manually.')
33 4
            ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null)
34 4
            ->addOption('up', null, InputOption::VALUE_NONE, 'Execute the migration up.')
35 4
            ->addOption('down', null, InputOption::VALUE_NONE, 'Execute the migration down.')
36 4
            ->addOption('replay', null, InputOption::VALUE_NONE, 'Replay an \'up\' migration and avoid the duplicate exception.')
37 4
            ->setHelp(<<<'EOT'
38 4
The <info>%command.name%</info> command executes a single migration version up or down manually:
39
40
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
41
42
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
43
44
    <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
45
46
Or you can also execute the migration without a warning message which you need to interact with:
47
48
    <info>%command.full_name% --no-interaction</info>
49
EOT
50
        );
51
52 4
        parent::configure();
53 4
    }
54
55
    /**
56
     * @param Symfony\Component\Console\Input\InputInterface
57
     * @param Symfony\Component\Console\Output\OutputInterface
58
     */
59 4
    public function execute(InputInterface $input, OutputInterface $output)
60
    {
61 4
        $version = $input->getArgument('version');
62 4
        $direction = $input->getOption('down') ? 'down' : 'up';
63 4
        $replay = $input->getOption('replay');
64
65 4
        $configuration = $this->getMigrationConfiguration($input, $output);
66 4
        $version = $configuration->getVersion($version);
67
68 4
        if (!$input->isInteractive()) {
69 2
            $version->execute($direction, $replay);
70
        } else {
71 2
            $question = new ConfirmationQuestion(
72 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> ',
73 2
                false
74
            );
75
76
            $confirmation = $this
77 2
                ->getHelper('question')
78 2
                ->ask($input, $output, $question);
79
80 2
            if (true === $confirmation) {
81 1
                $version->execute($direction, $replay);
82
            } else {
83 1
                $output->writeln('<error>Migration cancelled!</error>');
84
            }
85
        }
86 4
    }
87
88 4
    public function getName()
89
    {
90 4
        return self::NAME;
91
    }
92
}
93