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

MigrationHelper::getMigrations()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 8.5806
cc 4
eloc 18
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Rougin\Refinery\Common;
4
5
use FilesystemIterator;
6
use RecursiveIteratorIterator;
7
use RecursiveDirectoryIterator;
8
9
/**
10
 * Migration Helper
11
 *
12
 * Provides common methods used in other commands
13
 * 
14
 * @package Refinery
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class MigrationHelper
18
{
19
    /**
20
     * Changes the migration version.
21
     * 
22
     * @param  int $current
23
     * @param  int $timestamp
24
     * @return void
25
     */
26 9
    public static function changeVersion($current, $timestamp)
27
    {
28 9
        $path = APPPATH . '/config/migration.php';
29 9
        $migrationFile = file_get_contents($path);
30
31 9
        $currentVersion = '$config[\'migration_version\'] = ' . $current . ';';
32 9
        $newVersion = '$config[\'migration_version\'] = ' . $timestamp . ';';
33
34 9
        $migrationFile = str_replace(
35 9
            $currentVersion,
36 9
            $newVersion,
37
            $migrationFile
38 9
        );
39
40 9
        $file = fopen($path, 'wb');
41 9
        file_put_contents($path, $migrationFile);
42 9
        fclose($file);
43 9
    }
44
45
    /**
46
     * Gets the latest migration version
47
     * 
48
     * @return string
49
     */
50 12
    public static function getLatestVersion($file)
51
    {
52 12
        $pattern = '/\$config\[\'migration_version\'\] = (\d+);/';
53
54 12
        preg_match_all($pattern, $file, $match);
55
56 12
        return $match[1][0];
57
    }
58
59
    /**
60
     * Gets list of migrations from the specified directory.
61
     * 
62
     * @param  string $path
63
     * @return array
64
     */
65 12
    public static function getMigrations($path)
66
    {
67 12
        $filenames = [];
68 12
        $migrations = [];
69
70 12
        if ( ! is_dir($path)) {
71 3
            return [$filenames, $migrations];
72
        }
73
74 9
        $skipDots = FilesystemIterator::SKIP_DOTS;
75 9
        $selfFirst = RecursiveIteratorIterator::SELF_FIRST;
76
77
        // Searches a listing of migration files and sorts them after
78 9
        $directory = new RecursiveDirectoryIterator($path, $skipDots);
79 9
        $iterator = new RecursiveIteratorIterator($directory, $selfFirst);
80
81 9
        foreach ($iterator as $path) {
82 9
            $filenames[] = str_replace('.php', '', $path->getFilename());
83 9
            $migration = substr($path->getFilename(), 0, 14);
84
85 9
            if ( ! is_numeric($migration)) {
86 9
                $migration = substr($path->getFilename(), 0, 3);
87 9
            }
88
89 9
            $migrations[] = $migration;
90 9
        }
91
92 9
        sort($filenames);
93 9
        sort($migrations);
94
95 9
        return [$filenames, $migrations];
96
    }
97
98
    /**
99
     * Enables/disables the Migration Class.
100
     * 
101
     * @param  boolean $enabled
102
     * @return void
103
     */
104 9
    public static function toggleMigration($enabled = FALSE)
105
    {
106 9
        $path = APPPATH . '/config/migration.php';
107 9
        $migrationFile = file_get_contents($path);
108
109 9
        $search = '$config[\'migration_enabled\'] = TRUE;';
110 9
        $replace = '$config[\'migration_enabled\'] = FALSE;';
111
112 9
        if ($enabled) {
113 9
            $search = '$config[\'migration_enabled\'] = FALSE;';
114 9
            $replace = '$config[\'migration_enabled\'] = TRUE;';
115 9
        }
116
117 9
        $migrationFile = str_replace($search, $replace, $migrationFile);
118
119 9
        $file = fopen($path, 'wb');
120 9
        file_put_contents($path, $migrationFile);
121 9
        fclose($file);
122 9
    }
123
}
124