Completed
Push — master ( 50e0e2...b82482 )
by Rougin
02:35
created

RollbackCommand::isValidVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 2
crap 4
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(true);
43 6
        $version = $input->getArgument('version');
44
45 6
        $this->isValidVersion($migrations, $version);
46
47 3
        $migration = $migrations[count($migrations) - 1];
48 3
        $fileName  = $filenames[count($migrations) - 1];
49
50 3
        empty($version) || $migration = $version;
51
52
        // Enable migration and change the current version to a latest one
53 3
        $this->toggleMigration(true);
54 3
        $this->changeVersion($current, $migration);
55
56 3
        $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...
57 3
        $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...
58
59 3
        $this->toggleMigration();
60
61 3
        $message = "Database is reverted back to version $migration ($fileName)";
62
63 3
        return $output->writeln('<info>' . $message . '</info>');
64
    }
65
66
    /**
67
     * Checks if the version is valid to be rolled back.
68
     *
69
     * @param  array   $migrations
70
     * @param  string  $version
71
     * @return boolean
72
     */
73 6
    protected function isValidVersion(array $migrations, $version)
74
    {
75 6
        foreach ($migrations as $migration) {
76 6
            if ($migration == $version || empty($version)) {
77 3
                return true;
78
            }
79 3
        }
80
81 3
        throw new \InvalidArgumentException('Cannot rollback to version ' . $version);
82
    }
83
}
84