Completed
Push — dev ( b4f2c3...b3004a )
by James Ekow Abaka
04:06
created

Migrations::getRunMirations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace yentu;
4
5
use ntentan\config\Config;
6
7
8
class Migrations
9
{
10
    private $manipulatorFactory;
11
    private $config;
12
13
    public function __construct(DatabaseManipulatorFactory $manipulatorFactory, Config $config)
14
    {
15
        $this->manipulatorFactory = $manipulatorFactory;
16
        $this->config = $config;
17
    }
18
19
    private function getMigrationPathsInfo()
20
    {
21
        $variables = $this->config->get('variables', []);
22
        $otherMigrations = $this->config->get('other_migrations', []);
23
        return array_merge(
24
            array(
25
                array(
26
                    'home' => 'yentu/migrations', //$this->yentu->getPath('migrations'),
27
                    'variables' => $variables
28
                )
29
            ), $otherMigrations
30
        );
31
    }
32
33
    /**
34
     * Return an array of all migrations available.
35
     *
36
     * @param string $path
37
     * @return array
38
     */
39
    public function getMigrations($path)
40
    {
41
        if (!file_exists($path))
42
            return [];
43
        $migrationFiles = scandir($path, 0);
44
        $migrations = array();
45
        foreach ($migrationFiles as $migration) {
46
            $details = $this->getMigrationDetails($migration);
47
            if ($details === false)
48
                continue;
49
            $migrations[$details['timestamp']] = $details;
50
            unset($migrations[$details['timestamp']][0]);
51
            unset($migrations[$details['timestamp']][1]);
52
            unset($migrations[$details['timestamp']][2]);
53
        }
54
55
        return $migrations;
56
    }
57
58
    /**
59
     * Return the details of a migration extracted from the file name.
60
     * This method uses a regular expression to extract the timestamp and
61
     * migration name from the migration script.
62
     *
63
     * @param string $migration
64
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,string>|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
     */
66
    private function getMigrationDetails($migration)
67
    {
68
        if (preg_match("/^(?<timestamp>[0-9]{14})\_(?<migration>[a-z][a-z0-9\_]*)\.php$/", $migration, $details)) {
69
            $details['file'] = $migration;
70
        } else {
71
            $details = false;
72
        }
73
        return $details;
74
    }
75
    /**
76
     * Returns an array of all migrations, in all configured migrations
77
     * directories.
78
     * @return array
79
     */
80
    public function getAllMigrations()
81
    {
82
        $migrations = array();
83
        foreach ($this->getMigrationPathsInfo() as $migration) {
84
            $migrations = $migrations + $this->getMigrations($migration['home']);
85
        }
86
        return $migrations;
87
    }
88
89
90
    /**
91
     * Returns an array of all migrations that have been run on the database.
92
     * The information returned includes the timestamp, the name of the migration
93
     * and the default schema on which it was run.
94
     * @return array
95
     */
96
    public function getRunMirations()
97
    {
98
        $db = $this->manipulatorFactory->createManipulator();
99
        $runMigrations = $db->query("SELECT DISTINCT version, migration, default_schema FROM yentu_history ORDER BY version");
100
        $migrations = array();
101
        foreach ($runMigrations as $migration) {
102
            $migrations[$migration['version']] = array(
103
                'timestamp' => $migration['version'],
104
                'migration' => $migration['migration'],
105
                'default_schema' => $migration['default_schema']
106
            );
107
        }
108
109
        return $migrations;
110
    }
111
}