Passed
Pull Request — master (#47)
by Simon
04:32
created

ExecuteCommand::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    protected static $defaultName = 'mongodb:migrations:execute';
27
28 4
    protected function configure()
29
    {
30
        $this
31 4
            ->setDescription('Execute a single migration version up or down manually.')
32 4
            ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null)
33 4
            ->addOption('up', null, InputOption::VALUE_NONE, 'Execute the migration up.')
34 4
            ->addOption('down', null, InputOption::VALUE_NONE, 'Execute the migration down.')
35 4
            ->addOption('replay', null, InputOption::VALUE_NONE, 'Replay an \'up\' migration and avoid the duplicate exception.')
36 4
            ->setHelp(<<<'EOT'
37 4
The <info>%command.name%</info> command executes a single migration version up or down manually:
38
39
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
40
41
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
42
43
    <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
44
45
Or you can also execute the migration without a warning message which you need to interact with:
46
47
    <info>%command.full_name% --no-interaction</info>
48
EOT
49
        );
50
51 4
        parent::configure();
52 4
    }
53
54
    /**
55
     * @param \Symfony\Component\Console\Input\InputInterface
56
     * @param \Symfony\Component\Console\Output\OutputInterface
57
     */
58 4
    public function execute(InputInterface $input, OutputInterface $output)
59
    {
60 4
        $version = $input->getArgument('version');
61 4
        $direction = $input->getOption('down') ? 'down' : 'up';
62 4
        $replay = $input->getOption('replay');
63
64 4
        $configuration = $this->getMigrationConfiguration($input, $output);
65 4
        $version = $configuration->getVersion($version);
0 ignored issues
show
Bug introduced by
It seems like $version can also be of type string[]; however, parameter $version of AntiMattr\MongoDB\Migrat...iguration::getVersion() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $version = $configuration->getVersion(/** @scrutinizer ignore-type */ $version);
Loading history...
66
67 4
        if (!$input->isInteractive()) {
68 2
            $version->execute($direction, $replay);
69
        } else {
70 2
            $question = new ConfirmationQuestion(
71 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> ',
72 2
                false
73
            );
74
75
            $confirmation = $this
76 2
                ->getHelper('question')
77 2
                ->ask($input, $output, $question);
78
79 2
            if (true === $confirmation) {
80 1
                $version->execute($direction, $replay);
81
            } else {
82 1
                $output->writeln('<error>Migration cancelled!</error>');
83
            }
84
        }
85 4
    }
86
}
87