Completed
Push — master ( 65befb...50e0e2 )
by Rougin
02:28
created

RollbackCommand::execute()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 27
cts 27
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 10
nop 2
crap 7
1
<?php
2
3
namespace Rougin\Refinery\Commands;
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
 * Rollback Command
11
 *
12
 * Returns to a previous or specified migration.
13
 *
14
 * @package Refinery
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class RollbackCommand extends AbstractCommand
18
{
19
    /**
20
     * Sets the configurations of the specified command.
21
     *
22
     * @return void
23
     */
24 33
    protected function configure()
25
    {
26 33
        $this->setName('rollback')
27 33
            ->setDescription('Returns to a previous or specified migration')
28 33
            ->addArgument('version', InputArgument::OPTIONAL, 'Specified version of the migration');
29 33
    }
30
31
    /**
32
     * Executes the command.
33
     *
34
     * @param  \Symfony\Component\Console\Input\InputInterface   $input
35
     * @param  \Symfony\Component\Console\Output\OutputInterface $output
36
     * @return object|\Symfony\Component\Console\Output\OutputInterface
37
     */
38 9
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40 9
        list($filenames, $migrations) = $this->getMigrations(APPPATH . 'migrations');
41
42 9
        $current = $this->getLatestVersion();
43 9
        $end     = count($migrations) - 1;
44
45 9
        if (intval($current) <= 0) {
46 3
            return $output->writeln('<error>There\'s nothing to be rollbacked at.</error>');
47
        }
48
49 6
        $version = $input->getArgument('version');
50 6
        $found   = false;
51
52 6
        foreach ($migrations as $migration) {
53 6
            if ($migration == $version || empty($version)) {
54 6
                $found = true;
55
56 6
                break;
57
            }
58 6
        }
59
60 6
        if (! $found) {
61 3
            return $output->writeln('<error>Cannot rollback to version ' . $version . '.</error>');
62
        }
63
64 6
        $migration = $migrations[$end];
65 6
        $fileName  = $filenames[$end];
66
67 6
        if ($version) {
68 3
            $migration = $version;
69 3
        }
70
71
        // Enable migration and change the current version to a latest one
72 6
        $this->toggleMigration(true);
73 6
        $this->changeVersion($current, $migration);
74
75 6
        $this->codeigniter->load->library('migration');
0 ignored issues
show
Bug introduced by
The property load does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
76 6
        $this->codeigniter->migration->current();
0 ignored issues
show
Bug introduced by
The property migration does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
77
78 6
        $this->toggleMigration();
79
80 6
        $message = "Database is reverted back to version $migration ($fileName)";
81
82 6
        return $output->writeln('<info>' . $message . '</info>');
83
    }
84
}
85