Completed
Push — master ( d8474d...556100 )
by Rougin
05:42
created

RollbackCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 8
c 4
b 1
f 1
lcom 1
cbo 5
dl 0
loc 80
ccs 39
cts 39
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B execute() 0 54 7
1
<?php
2
3
namespace Rougin\Refinery\Commands;
4
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
use Rougin\SparkPlug\Instance;
11
use Rougin\Refinery\Common\MigrationHelper;
12
13
/**
14
 * Rollback Command
15
 *
16
 * Returns to a previous/specified migration
17
 * 
18
 * @package Refinery
19
 * @author  Rougin Royce Gutib <[email protected]>
20
 */
21
class RollbackCommand extends AbstractCommand
22
{
23
    /**
24
     * Sets the configurations of the specified command.
25
     *
26
     * @return void
27
     */
28 12
    protected function configure()
29
    {
30 12
        $this->setName('rollback')
31 12
            ->setDescription('Returns to a previous/specified migration')
32 12
            ->addArgument(
33 12
                'version',
34 12
                InputArgument::OPTIONAL,
35
                'Specified version of the migration'
36 12
            );
37 12
    }
38
39
    /**
40
     * Executes the command.
41
     * 
42
     * @param  InputInterface  $input
43
     * @param  OutputInterface $output
44
     * @return object|OutputInterface
45
     */
46 9
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 9
        $migration = file_get_contents(APPPATH . '/config/migration.php');
49 9
        $current = MigrationHelper::getLatestVersion($migration);
50 9
        $migrationsPath = APPPATH . 'migrations';
51
52 9
        list($filenames, $migrations) = MigrationHelper::getMigrations($migrationsPath);
53
54
        // Might get the latest or the specified version or revert back
55 9
        $end = count($migrations) - 1;
56
57 9
        if (intval($current) <= 0) {
58 3
            $message = 'There\'s nothing to be rollbacked at.';
59
60 3
            return $output->writeln('<error>' . $message . '</error>');
61
        }
62
63 6
        $version = $input->getArgument('version');
64 6
        $versionFound = false;
65
66 6
        foreach ($migrations as $migration) {
67 6
            if ($migration == $version || empty($version)) {
68 6
                $versionFound = true;
69
70 6
                break;
71
            }
72 6
        }
73
74 6
        if ( ! $versionFound) {
75 3
            $message = "Cannot rollback to version $version.";;
76
77 3
            return $output->writeln('<error>' . $message . '</error>');
78
        }
79
80 6
        $latest = $migrations[$end];
81 6
        $latestFile = $filenames[$end];
82
83 6
        if ($version) {
84 3
            $latest = $version;
85 3
        }
86
87
        // Enable migration and change the current version to a latest one
88 6
        MigrationHelper::toggleMigration(true);
89 6
        MigrationHelper::changeVersion($current, $latest);
90
91 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...
92 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...
93
94 6
        MigrationHelper::toggleMigration();
95
96 6
        $message = "Database is reverted back to version $latest ($latestFile)";
97
98 6
        return $output->writeln('<info>' . $message . '</info>');
99
    }
100
}
101