Completed
Push — master ( e15303...474346 )
by Rougin
02:06
created

RevertCommand::version()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Rougin\Refinery\Console;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Revert Command
11
 *
12
 * @package Refinery
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class RevertCommand extends ChangeCommand
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $done = 'Rolled back: ';
21
22
    /**
23
     * @var string
24
     */
25
    protected $pending = 'Rolling back:';
26
27
    /**
28
     * @var boolean
29
     */
30
    protected $reversed = true;
31
32
    /**
33
     * Configures the current command.
34
     *
35
     * @return void
36
     */
37 6
    protected function configure()
38
    {
39 6
        $this->setName('rollback')->setDescription('Returns to a previous or specified version');
40
41 6
        $this->addArgument('version', InputArgument::OPTIONAL, 'Version number of the migration');
42 6
    }
43
44
    /**
45
     * Executes the command.
46
     *
47
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
48
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
49
     * @return void
50
     */
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        list($current, $version) = $this->version($input);
54
55
        if ($current !== '0' && $current !== '000') {
56
            $result = $this->rollback($current, (string) $version);
57
58
            return $this->migrate($output, $result[0], $result[1]);
59
        }
60
61
        $text = (string) 'There is nothing to migrate.';
62
63
        $output->writeln('<info>' . $text . '</info>');
64
    }
65
66
    /**
67
     * Performs a rollback to a specified migration.
68
     *
69
     * @param  string $current
70
     * @param  string $version
71
     * @return string|boolean
72
     */
73
    protected function rollback($current, $version)
74
    {
75
        list($migrations, $versions) = $this->migrations();
76
77
        $index = array_search($version, (array) $versions);
78
79
        $file = $version === $current ? $current : $version;
80
81
        $filename = (string) basename($migrations[$file]);
82
83
        $index = $version === $current ? $index + 1 : $index + 0;
84
85
        return array($filename, $versions[(integer) $index]);
86
    }
87
88
    /**
89
     * Returns the current and user-specified versions.
90
     *
91
     * @param  \Symfony\Component\Console\Input\InputInterface $input
92
     * @return array
93
     */
94
    protected function version(InputInterface $input)
95
    {
96
        $version = $input->getArgument('version');
97
98
        $current = $this->manager->current();
99
100
        is_null($version) && $version = $current;
101
102
        return array($current, $version);
103
    }
104
}
105