ExecuteCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
eloc 41
c 3
b 0
f 0
dl 0
loc 63
ccs 27
cts 27
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 29 4
A configure() 0 24 1
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 Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\ConfirmationQuestion;
19
20
/**
21
 * @author Matthew Fitzgerald <[email protected]>
22
 */
23
class ExecuteCommand extends AbstractCommand
24
{
25
    protected static $defaultName = 'mongodb:migrations:execute';
26
27 4
    protected function configure()
28
    {
29
        $this
30 4
            ->setDescription('Execute a single migration version up or down manually.')
31 4
            ->addArgument('version', InputArgument::REQUIRED, 'The version to execute.', null)
32 4
            ->addOption('up', null, InputOption::VALUE_NONE, 'Execute the migration up.')
33 4
            ->addOption('down', null, InputOption::VALUE_NONE, 'Execute the migration down.')
34 4
            ->addOption('replay', null, InputOption::VALUE_NONE, 'Replay an \'up\' migration and avoid the duplicate exception.')
35 4
            ->setHelp(<<<'EOT'
36 4
The <info>%command.name%</info> command executes a single migration version up or down manually:
37
38
    <info>%command.full_name% YYYYMMDDHHMMSS</info>
39
40
If no <comment>--up</comment> or <comment>--down</comment> option is specified it defaults to up:
41
42
    <info>%command.full_name% YYYYMMDDHHMMSS --down</info>
43
44
Or you can also execute the migration without a warning message which you need to interact with:
45
46
    <info>%command.full_name% --no-interaction</info>
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 4
        $direction = $input->getOption('down') ? 'down' : 'up';
61 4
        $replay = $input->getOption('replay');
62
63 4
        $configuration = $this->getMigrationConfiguration($input, $output);
64 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

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