ExecuteHandler::handle()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 32
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 32
rs 8.439
cc 6
eloc 24
nc 12
nop 1
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Baleen\Cli\CommandBus\Timeline;
22
23
use Baleen\Migrations\Migration\Options;
24
use Baleen\Migrations\Version;
25
use Symfony\Component\Console\Question\ConfirmationQuestion;
26
27
/**
28
 * Class ExecuteHandler.
29
 *
30
 * @author Gabriel Somoza <[email protected]>
31
 */
32
class ExecuteHandler
33
{
34
    public function handle(ExecuteMessage $command)
35
    {
36
        $input = $command->getInput();
37
        $output = $command->getOutput();
38
        $version = (string) $input->getArgument(ExecuteMessage::ARG_VERSION);
39
40
        $direction = $input->getArgument(ExecuteMessage::ARG_DIRECTION) == Options::DIRECTION_DOWN ?
41
            Options::DIRECTION_DOWN :
42
            Options::DIRECTION_UP;
43
        $dryRun = (bool) $input->getOption(ExecuteMessage::OPT_DRY_RUN);
44
        $forced = true; // because we're executing a single migration
45
46
        $options = new Options($direction, $forced, $dryRun);
47
48
        $canExecute = true;
49
        if ($input->isInteractive()) {
50
            $output->writeln('<error>WARNING!</error> You are about to manually execute a database migration that '.
51
                'could result in schema changes and data lost.');
52
            $question = sprintf('Are you sure you wish to migrate "%s" (y/n)? ', $direction);
53
            $canExecute = $command->getCliCommand()
54
                ->getHelper('question')
55
                ->ask($input, $output, new ConfirmationQuestion($question));
56
        }
57
        if ($canExecute) {
58
            $result = $command->getTimeline()->runSingle($version, $options);
59
            if ($result && !$options->isDryRun()) {
60
                $version = $result;
61
                $command->getStorage()->update($version);
62
            }
63
            $output->writeln("Version <info>$version</info> migrated <info>$direction</info> successfully.");
64
        }
65
    }
66
}
67