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

MigrateCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 5
c 6
b 1
f 1
lcom 1
cbo 4
dl 0
loc 62
ccs 29
cts 29
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 0 40 4
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
 * Migrate Command
15
 *
16
 * Migrates the list of migrations found in "application/migrations" directory.
17
 * 
18
 * @package Refinery
19
 * @author  Rougin Royce Gutib <[email protected]>
20
 */
21
class MigrateCommand extends AbstractCommand
22
{
23
    /**
24
     * Sets the configurations of the specified command.
25
     *
26
     * @return void
27
     */
28 15
    protected function configure()
29
    {
30 15
        $this
31 15
            ->setName('migrate')
32 15
            ->setDescription('Migrates the database');
33 15
    }
34
35
    /**
36
     * Executes the command.
37
     * 
38
     * @param  InputInterface  $input
39
     * @param  OutputInterface $output
40
     * @return object|OutputInterface
41
     */
42 9
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44 9
        $migration = file_get_contents(APPPATH . '/config/migration.php');
45 9
        $current = MigrationHelper::getLatestVersion($migration);
46 9
        $migrationsPath = APPPATH . 'migrations';
47
48 9
        list($filenames, $migrations) = MigrationHelper::getMigrations($migrationsPath);
49
50 9
        $end = count($migrations) - 1;
51 9
        $latest = $migrations[$end];
52
53
        // Enable migration and change the current version to a latest one
54 9
        MigrationHelper::toggleMigration(true);
55 9
        MigrationHelper::changeVersion($current, $latest);
56
57 9
        $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...
58 9
        $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...
59
60 9
        MigrationHelper::toggleMigration();
61
62
        // Show messages of migrated files
63 9
        if ($current == $latest) {
64 6
            $message = 'Database is up to date.';
65
66 6
            return $output->writeln('<info>' . $message . '</info>');
67
        }
68
69 9
        $migrationsCount = count($migrations);
70
71 9
        for ($counter = 0; $counter < $migrationsCount; $counter++) {
72 9
            if ($current >= $migrations[$counter]) {
73 3
                continue;
74
            }
75
76 9
            $filename = $filenames[$counter];
77 9
            $message = "$filename has been migrated to the database";
78
79 9
            $output->writeln('<info>' . $message . '</info>');
80 9
        }
81 9
    }
82
}
83